Min Pages Per Day

You have upcoming interviews and have selected specific chapters from BCtCI to read beforehand. Given an array, page_counts, where each element represents a chapter's page count, and the number of days, days, until your interview, determine the minimum number of pages you must read daily to finish on time. Assume that:

  • You must read all the pages of a chapter before moving on to another one.
  • If you finish a chapter on a given day, you practice for the rest of the day and don't start the next chapter until the next day.
  • len(page_counts) <= days.

Example:

  • Input: page_counts = [20, 15, 17, 10], days = 14
  • Output: 5
1def minPagesPerDay(page_counts: List[int], days: int) -> int:
2 def canFinish(speed):
3 d = 0
4 for pages in page_counts:
5 d += math.ceil(pages / speed)
6 return d <= days
7
8 l, r = 1, max(page_counts)
9 while l < r:
10 mid = (l + r) // 2
11 if canFinish(mid):
12 r = mid
13 else:
14 l = mid + 1
15 return l
20
0
l
15
1
17
2
10
3
days=14
Step 1 / 5
Step 1:
Search range [1, 20]. l=1, r=20. mid=10.
Pointers: l=1, mid=10, r=20
Focus: default
days=14