समस्या
At the end of the term a teacher wants a quick summary of the class: how many students earned each letter grade. The scale is 90 and above for A, 80 to 89 for B, 70 to 79 for C, 60 to 69 for D and below 60 for F.
Implement grade_letter_counts(scores).
Input
scores: Every student's final score.
Output
Return five counts in the order A, B, C, D, F. An empty class gives [0, 0, 0, 0, 0].
सीमाएँ
0 ≤ number of scores ≤ 10,000
0 ≤ score ≤ 100
उदाहरण
इनपुट:
scores = [95, 82, 67, 88, 45, 90]
अपेक्षित आउटपुट:
expected = [2, 2, 0, 1, 1]
इनपुट:
std::vector<long long> scores = {95, 82, 67, 88, 45, 90};
अपेक्षित आउटपुट:
std::vector<long long> expected = {2, 2, 0, 1, 1};
इनपुट:
long[] scores = new long[] {95, 82, 67, 88, 45, 90};
अपेक्षित आउटपुट:
long[] expected = new long[] {2, 2, 0, 1, 1};
व्याख्या
95 and 90 are A, 82 and 88 are B, 67 is D and 45 is F: [2, 2, 0, 1, 1].
इनपुट:
scores = [79, 80, 59, 60]
अपेक्षित आउटपुट:
expected = [0, 1, 1, 1, 1]
इनपुट:
std::vector<long long> scores = {79, 80, 59, 60};
अपेक्षित आउटपुट:
std::vector<long long> expected = {0, 1, 1, 1, 1};
इनपुट:
long[] scores = new long[] {79, 80, 59, 60};
अपेक्षित आउटपुट:
long[] expected = new long[] {0, 1, 1, 1, 1};
व्याख्या
Each score sits on a cutoff: 79 is C, 80 is B, 59 is F and 60 is D: [0, 1, 1, 1, 1].
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
Check the highest cutoff first so each score falls into exactly one branch.
संकेत 2
Use an array of five counters, one per letter.
संकेत 3
An empty class just returns the counters untouched.
अपेक्षित जटिलता
O(n) time, O(1) extra space
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।