Đề bài
Compute the average of the list, then keep only the values strictly greater than it, in their original order.
Implement above_average(numbers).
Input
numbers: The list of integers.
Output
Return the values above the average (an empty list for an empty input).
Ràng buộc
0 ≤ length ≤ 10,000; -1,000,000 ≤ each value ≤ 1,000,000
Ví dụ
Đầu vào:
numbers = [1, 2, 3, 4, 10]
Kết quả mong đợi:
expected = [10]
Đầu vào:
std::vector<long long> numbers = {1, 2, 3, 4, 10};
Kết quả mong đợi:
std::vector<long long> expected = {10};
Đầu vào:
long[] numbers = new long[] {1, 2, 3, 4, 10};
Kết quả mong đợi:
long[] expected = new long[] {10};
Giải thích
The average is 20 / 5 = 4; only 10 is greater than 4.
Đầu vào:
numbers = [5, 5, 5]
Kết quả mong đợi:
expected = []
Đầu vào:
std::vector<long long> numbers = {5, 5, 5};
Kết quả mong đợi:
std::vector<long long> expected = {};
Đầu vào:
long[] numbers = new long[] {5, 5, 5};
Kết quả mong đợi:
long[] expected = new long[] {};
Giải thích
Every value equals the average 5, so none is greater: [].
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
First find the list’s average, then keep values strictly above it.
Gợi ý 2
This usually requires one pass to measure the total and another to collect matches.
Gợi ý 3
If the list is empty, the answer should also be empty.
Độ phức tạp mong đợi
O(n) time, O(1) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.