Two questions, and they were unbelievably easy.
Q1: There are N students who took an exam, and the K-th student's score is results[K]. Scores are classified by these rules:
- Failed: score below 50%.
- Passed with Honors: score above 80%.
- Passed without Honors: everything else, i.e. 50% to 80% inclusive.
Count: how many students failed? How many students passed without honors? How many students passed with honors? Given an array results containing the scores of all N students, return an array of length 3. The returned array must be in this order: [number failed, number passed without honors, number passed with honors]
Cases:
- results = [50, 80, 81, 99, 30, 77, 67], return [1, 4, 2]
- results = [30, 30, 49], return [3, 0, 0]
- results = [0, 100], return [1, 0, 1]
N is an integer within the range [1..100].
Q2: Suppose you have to read all the chapters of a book in a fixed order, and each chapter has to be finished completely in a single evening, you can't split it across days. Calculate how many evenings it takes to finish the whole book.
The book has M chapters in total. Reading chapter K takes chapter[K] minutes. Alex plans to read the book over the next N evenings. On evening J, Alex has set aside evening[J] minutes for reading. Reading has to follow these rules:
- Read in chapter order: start from the first chapter and go through to the last, no skipping.
- Read as much as possible each day: each evening, Alex wants to read as many complete chapters as possible within the available time.
- No partial chapters: Alex can only start a chapter if the remaining time is enough to finish it completely.
- If there isn't enough time, wait for the next evening: if the time left tonight is not enough to finish the next chapter, Alex has to wait until the next evening to start that chapter.
Question: how many evenings does Alex need to finish the whole book?
Write a function that, given two arrays:
chapter (of M integers)
evening (of N integers)
returns the minimum number of evenings needed to finish reading the book. If the book cannot be finished during the planned N evenings, the function should return -1.
- chapter = [3, 2, 1], evening = [2, 5, 3, 3], return 3
- chapter = [5, 8, 5], evening = [9, 4, 5, 3, 8], return -1
Discussion
Loading comments…