Đề bài
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.
Ràng buộc
1 ≤ students ≤ 1,000; 1 ≤ scores per student ≤ 100; 0 ≤ score ≤ 100
Ví dụ
Đầu vào:
scores = [
[80, 90],
[95, 85],
[70, 100]
]
Kết quả mong đợi:
expected = 1
Đầu vào:
std::vector<std::vector<long long>> scores = {
{80, 90},
{95, 85},
{70, 100}
};
Kết quả mong đợi:
long long expected = 1;
Đầu vào:
long[][] scores = new long[][] {
new long[] {80, 90},
new long[] {95, 85},
new long[] {70, 100}
};
Kết quả mong đợi:
long expected = 1;
Giải thích
Averages are 85, 90 and 85, so student 1 has the highest average.
Đầu vào:
scores = [
[60],
[60, 60]
]
Kết quả mong đợi:
expected = 0
Đầu vào:
std::vector<std::vector<long long>> scores = {
{60},
{60, 60}
};
Kết quả mong đợi:
long long expected = 0;
Đầu vào:
long[][] scores = new long[][] {
new long[] {60},
new long[] {60, 60}
};
Kết quả mong đợi:
long expected = 0;
Giải thích
Both averages are 60; the tie goes to the smaller index, 0.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
So sánh học sinh bằng điểm trung bình, không phải chỉ một bài kiểm tra.
Gợi ý 2
Tính trung bình mỗi dòng và giữ chỉ số tốt nhất đã thấy.
Gợi ý 3
Khi điểm trung bình bằng nhau, giữ học sinh đứng trước thay vì thay thế.
Độ phức tạp mong đợi
thời gian O(total scores), bộ nhớ phụ O(1)
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.