← Home

1619. Mean of Array After Removing Some Elements

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
greedyC++Markdown
161

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1619. Mean of Array After Removing Some Elements, the solution in this repository is mainly a greedy solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: greedy.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are trimMean.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 28 ms, faster than 11.60% of C++ online submissions for Mean of Array After Removing Some Elements.
02//Memory Usage: 10 MB, less than 99.83% of C++ online submissions for Mean of Array After Removing Some Elements.
03class Solution {
04public:
05    double trimMean(vector<int>& arr) {
06        int n = arr.size();
07        
08        int rem = n*5.0/100;
09        
10        sort(arr.begin(), arr.end());
11        
12        return (double)accumulate(arr.begin()+rem, arr.end()-rem, 0)/(n-2*rem);
13    }
14};
15
16//O(n) nth_element
17//https://leetcode.com/problems/mean-of-array-after-removing-some-elements/discuss/900529/C%2B%2B-O(n)-nth_element
18//Runtime: 20 ms, faster than 45.74% of C++ online submissions for Mean of Array After Removing Some Elements.
19//Memory Usage: 9.8 MB, less than 99.83% of C++ online submissions for Mean of Array After Removing Some Elements.
20class Solution {
21public:
22    double trimMean(vector<int>& arr) {
23        int n = arr.size();
24        
25        int rem = n*5.0/100;
26        
27        //[rem, n-rem*2, rem]
28        
29        //put the first "rem" elements at their right place
30        nth_element(arr.begin(), arr.begin()+rem, arr.end());
31        //put the second part(n-rem*2 elements) at their right place
32        nth_element(arr.begin()+rem, arr.end()-rem, arr.end());
33        
34        return accumulate(arr.begin()+rem, arr.end()-rem, 0)/(double)(n-rem*2);
35    }
36};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.