中級 5件解決済み 正答率55.6%

Gradebook Top Student

Find the student with the highest average score.

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

  • Average each row of scores
  • Find the best student
  • Break ties consistently

問題

翻訳を表示

Each row of scores holds one student's test scores. Compute each student's average with a helper function and return the row index of the student with the highest average. If several tie, return the smallest index.

Implement top_student(scores).

Input

scores: One row of scores per student; every row has at least one score.

Output

Return the index of the top student.

制約

1 ≤ students ≤ 1,000; 1 ≤ scores per student ≤ 100; 0 ≤ score ≤ 100

例

例 1
入力:
scores = [
    [80, 90],
    [95, 85],
    [70, 100]
]

期待される出力:
expected = 1
入力:
std::vector<std::vector<long long>> scores = {
    {80, 90},
    {95, 85},
    {70, 100}
};

期待される出力:
long long expected = 1;
入力:
long[][] scores = new long[][] {
    new long[] {80, 90},
    new long[] {95, 85},
    new long[] {70, 100}
};

期待される出力:
long expected = 1;

解説

Averages are 85, 90 and 85, so student 1 has the highest average.

例 2
入力:
scores = [
    [60],
    [60, 60]
]

期待される出力:
expected = 0
入力:
std::vector<std::vector<long long>> scores = {
    {60},
    {60, 60}
};

期待される出力:
long long expected = 0;
入力:
long[][] scores = new long[][] {
    new long[] {60},
    new long[] {60, 60}
};

期待される出力:
long expected = 0;

解説

Both averages are 60; the tie goes to the smaller index, 0.

解き方のヒント

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

ヒント 1

Compare students by their average score, not by a single test.

ヒント 2

Compute each row’s average and keep the best index seen so far.

ヒント 3

When averages tie, keep the earlier student instead of replacing it.

目安の計算量

O(total scores) time, O(1) extra space

参考資料

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

ランキング

全体ランキング

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

# コーダー 言語 時間 メモリ AI 評価 解答
1 Н"Никита "Programmer" Python 633 ms - 4/5 コードを見る
2 MSMichael Stoddart Python 692 ms - 3/5 コードを見る
3 NBnico basil Python 720 ms - 4/5 コードを見る
4 LMLaura Mahlow Python 757 ms - 4/5 コードを見る
5 PKPornchai Kaewkrabil Go 1,031 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

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