Trung bình 5 người đã giải Tỷ lệ chấp nhận 55.6%

Gradebook Top Student

Find the student with the highest average score.

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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ụ

Ví dụ 1
Đầ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.

Ví dụ 2
Đầ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

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

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.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 Н"Никита "Programmer" Python 633 ms - 4/5 Xem code
2 MSMichael Stoddart Python 692 ms - 3/5 Xem code
3 NBnico basil Python 720 ms - 4/5 Xem code
4 LMLaura Mahlow Python 757 ms - 4/5 Xem code
5 PKPornchai Kaewkrabil Go 1,031 ms - 4/5 Xem code

Học hỏi từ người khác

Lời giải cộng đồng

Cảnh báo: Có 5 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem code của người khác và nhận xét của AI. Đăng nhập và giải thử thách để tham gia thảo luận.