ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Gradebook Letter Counts

Turn a whole class's scores into letter grades and count how many students got each letter.

สิ่งที่คุณจะได้เรียนรู้

  • Map a score to a letter with ordered conditions
  • Tally results in a fixed-size array
  • Return counts in a defined order

โจทย์

ดูฉบับแปล

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

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
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].

ตัวอย่างที่ 2
อินพุต:
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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก