समस्या
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
Students को एक test से नहीं, average score से compare करो।
संकेत 2
हर row का average निकालो और अभी तक का best index संभालकर रखो।
संकेत 3
अगर averages equal हों, तो नया student replace मत करो; पहले वाले को ही रहने दो।
अपेक्षित जटिलता
समय O(total scores), अतिरिक्त स्थान O(1)
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।