|
| 1 | +<h2><a href="https://leetcode.com/problems/minimum-average-of-smallest-and-largest-elements">Minimum Average of Smallest and Largest Elements</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> |
| 2 | + |
| 3 | +<p>You repeat the following procedure <code>n / 2</code> times:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>, from <code>nums</code>.</li> |
| 7 | + <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> |
| 11 | + |
| 12 | +<p> </p> |
| 13 | +<p><strong class="example">Example 1:</strong></p> |
| 14 | + |
| 15 | +<div class="example-block"> |
| 16 | +<p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> |
| 17 | + |
| 18 | +<p><strong>Output:</strong> <span class="example-io">5.5</span></p> |
| 19 | + |
| 20 | +<p><strong>Explanation:</strong></p> |
| 21 | + |
| 22 | +<table> |
| 23 | + <tbody> |
| 24 | + <tr> |
| 25 | + <th>step</th> |
| 26 | + <th>nums</th> |
| 27 | + <th>averages</th> |
| 28 | + </tr> |
| 29 | + <tr> |
| 30 | + <td>0</td> |
| 31 | + <td>[7,8,3,4,15,13,4,1]</td> |
| 32 | + <td>[]</td> |
| 33 | + </tr> |
| 34 | + <tr> |
| 35 | + <td>1</td> |
| 36 | + <td>[7,8,3,4,13,4]</td> |
| 37 | + <td>[8]</td> |
| 38 | + </tr> |
| 39 | + <tr> |
| 40 | + <td>2</td> |
| 41 | + <td>[7,8,4,4]</td> |
| 42 | + <td>[8,8]</td> |
| 43 | + </tr> |
| 44 | + <tr> |
| 45 | + <td>3</td> |
| 46 | + <td>[7,4]</td> |
| 47 | + <td>[8,8,6]</td> |
| 48 | + </tr> |
| 49 | + <tr> |
| 50 | + <td>4</td> |
| 51 | + <td>[]</td> |
| 52 | + <td>[8,8,6,5.5]</td> |
| 53 | + </tr> |
| 54 | + </tbody> |
| 55 | +</table> |
| 56 | +The smallest element of averages, 5.5, is returned.</div> |
| 57 | + |
| 58 | +<p><strong class="example">Example 2:</strong></p> |
| 59 | + |
| 60 | +<div class="example-block"> |
| 61 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> |
| 62 | + |
| 63 | +<p><strong>Output:</strong> <span class="example-io">5.5</span></p> |
| 64 | + |
| 65 | +<p><strong>Explanation:</strong></p> |
| 66 | + |
| 67 | +<table> |
| 68 | + <tbody> |
| 69 | + <tr> |
| 70 | + <th>step</th> |
| 71 | + <th>nums</th> |
| 72 | + <th>averages</th> |
| 73 | + </tr> |
| 74 | + <tr> |
| 75 | + <td>0</td> |
| 76 | + <td><span class="example-io">[1,9,8,3,10,5]</span></td> |
| 77 | + <td>[]</td> |
| 78 | + </tr> |
| 79 | + <tr> |
| 80 | + <td>1</td> |
| 81 | + <td><span class="example-io">[9,8,3,5]</span></td> |
| 82 | + <td>[5.5]</td> |
| 83 | + </tr> |
| 84 | + <tr> |
| 85 | + <td>2</td> |
| 86 | + <td><span class="example-io">[8,5]</span></td> |
| 87 | + <td>[5.5,6]</td> |
| 88 | + </tr> |
| 89 | + <tr> |
| 90 | + <td>3</td> |
| 91 | + <td>[]</td> |
| 92 | + <td>[5.5,6,6.5]</td> |
| 93 | + </tr> |
| 94 | + </tbody> |
| 95 | +</table> |
| 96 | +</div> |
| 97 | + |
| 98 | +<p><strong class="example">Example 3:</strong></p> |
| 99 | + |
| 100 | +<div class="example-block"> |
| 101 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> |
| 102 | + |
| 103 | +<p><strong>Output:</strong> <span class="example-io">5.0</span></p> |
| 104 | + |
| 105 | +<p><strong>Explanation:</strong></p> |
| 106 | + |
| 107 | +<table> |
| 108 | + <tbody> |
| 109 | + <tr> |
| 110 | + <th>step</th> |
| 111 | + <th>nums</th> |
| 112 | + <th>averages</th> |
| 113 | + </tr> |
| 114 | + <tr> |
| 115 | + <td>0</td> |
| 116 | + <td><span class="example-io">[1,2,3,7,8,9]</span></td> |
| 117 | + <td>[]</td> |
| 118 | + </tr> |
| 119 | + <tr> |
| 120 | + <td>1</td> |
| 121 | + <td><span class="example-io">[2,3,7,8]</span></td> |
| 122 | + <td>[5]</td> |
| 123 | + </tr> |
| 124 | + <tr> |
| 125 | + <td>2</td> |
| 126 | + <td><span class="example-io">[3,7]</span></td> |
| 127 | + <td>[5,5]</td> |
| 128 | + </tr> |
| 129 | + <tr> |
| 130 | + <td>3</td> |
| 131 | + <td><span class="example-io">[]</span></td> |
| 132 | + <td>[5,5,5]</td> |
| 133 | + </tr> |
| 134 | + </tbody> |
| 135 | +</table> |
| 136 | +</div> |
| 137 | + |
| 138 | +<p> </p> |
| 139 | +<p><strong>Constraints:</strong></p> |
| 140 | + |
| 141 | +<ul> |
| 142 | + <li><code>2 <= n == nums.length <= 50</code></li> |
| 143 | + <li><code>n</code> is even.</li> |
| 144 | + <li><code>1 <= nums[i] <= 50</code></li> |
| 145 | +</ul> |
0 commit comments