Mittel 0 gelöst 0% Erfolgsquote

Gradebook Top Student

Find the student with the highest average score.

Das lernst du

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

Problem

Übersetzung anzeigen

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.

Einschränkungen

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

Beispiele

Beispiel 1
Eingabe:
scores = [
    [80, 90],
    [95, 85],
    [70, 100]
]

Erwartete Ausgabe:
expected = 1
Eingabe:
std::vector<std::vector<long long>> scores = {
    {80, 90},
    {95, 85},
    {70, 100}
};

Erwartete Ausgabe:
long long expected = 1;
Eingabe:
long[][] scores = new long[][] {
    new long[] {80, 90},
    new long[] {95, 85},
    new long[] {70, 100}
};

Erwartete Ausgabe:
long expected = 1;

Erläuterung

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

Beispiel 2
Eingabe:
scores = [
    [60],
    [60, 60]
]

Erwartete Ausgabe:
expected = 0
Eingabe:
std::vector<std::vector<long long>> scores = {
    {60},
    {60, 60}
};

Erwartete Ausgabe:
long long expected = 0;
Eingabe:
long[][] scores = new long[][] {
    new long[] {60},
    new long[] {60, 60}
};

Erwartete Ausgabe:
long expected = 0;

Erläuterung

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

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

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

Hinweis 2

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

Hinweis 3

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

Erwartete Komplexität

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

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.