簡単 2件解決済み 正答率66.7%

Above-Average Values

Keep the values that are greater than the average.

このチャレンジで学べること

  • Compute an average first
  • Filter values in a second pass
  • Keep the original order

問題

翻訳を表示

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).

制約

0 ≤ length ≤ 10,000; -1,000,000 ≤ each value ≤ 1,000,000

例

例 1
入力:
numbers = [1, 2, 3, 4, 10]

期待される出力:
expected = [10]
入力:
std::vector<long long> numbers = {1, 2, 3, 4, 10};

期待される出力:
std::vector<long long> expected = {10};
入力:
long[] numbers = new long[] {1, 2, 3, 4, 10};

期待される出力:
long[] expected = new long[] {10};

解説

The average is 20 / 5 = 4; only 10 is greater than 4.

例 2
入力:
numbers = [5, 5, 5]

期待される出力:
expected = []
入力:
std::vector<long long> numbers = {5, 5, 5};

期待される出力:
std::vector<long long> expected = {};
入力:
long[] numbers = new long[] {5, 5, 5};

期待される出力:
long[] expected = new long[] {};

解説

Every value equals the average 5, so none is greater: [].

解き方のヒント

まずは自分で考えてから、ヒントを1つずつ開いてみましょう。

ヒント 1

First find the list’s average, then keep values strictly above it.

ヒント 2

This usually requires one pass to measure the total and another to collect matches.

ヒント 3

If the list is empty, the answer should also be empty.

目安の計算量

O(n) time, O(1) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 RCRiccardo Corti Python 1,019 ms - 4/5 コードを見る
2 LMLaura Mahlow Python 1,100 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意: この下に解答が2件あります まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。