문제
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
예시
입력:
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.
입력:
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
한 번의 시험 점수가 아니라, 학생의 평균 점수로 비교하세요.
힌트 2
각 행의 평균을 계산하고 지금까지의 최고 인덱스를 유지합니다.
힌트 3
평균이 같다면 바꾸지 말고 더 앞선 학생을 유지하세요.
예상 복잡도
시간 O(total scores), 추가 공간 O(1)
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.