Tutte le sfide
Media 5 risolte 55.6% di accettazione

Gradebook Top Student

Find the student with the highest average score.

Cosa imparerai

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

Problema

Mostra traduzione

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.

Vincoli

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

Esempi

Esempio 1
Input:
scores = [
    [80, 90],
    [95, 85],
    [70, 100]
]

Output atteso:
expected = 1
Input:
std::vector<std::vector<long long>> scores = {
    {80, 90},
    {95, 85},
    {70, 100}
};

Output atteso:
long long expected = 1;
Input:
long[][] scores = new long[][] {
    new long[] {80, 90},
    new long[] {95, 85},
    new long[] {70, 100}
};

Output atteso:
long expected = 1;

Spiegazione

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

Esempio 2
Input:
scores = [
    [60],
    [60, 60]
]

Output atteso:
expected = 0
Input:
std::vector<std::vector<long long>> scores = {
    {60},
    {60, 60}
};

Output atteso:
long long expected = 0;
Input:
long[][] scores = new long[][] {
    new long[] {60},
    new long[] {60, 60}
};

Output atteso:
long expected = 0;

Spiegazione

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

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

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

Suggerimento 2

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

Suggerimento 3

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

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 Н"Никита "Programmer" Python 633 ms - 4/5 Vedi il codice
2 MSMichael Stoddart Python 692 ms - 3/5 Vedi il codice
3 NBnico basil Python 720 ms - 4/5 Vedi il codice
4 LMLaura Mahlow Python 757 ms - 4/5 Vedi il codice
5 PKPornchai Kaewkrabil Go 1,031 ms - 4/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 5 soluzioni complete sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le revisioni dell'IA. Accedi e risolvi la sfida per partecipare alla discussione.