← Questions

Minimum daily reading pace to finish a textbook

0
AmazonCoding

A student must read a textbook of n chapters before an exam. Chapter i has pages[i] pages, and the chapters must be read in order.

The student picks a daily limit x. On each day they read from the current chapter until either the chapter ends or they have read x pages, whichever comes first. A day never continues into the next chapter: if a chapter ends partway through the day, the student stops for that day.

Given pages and the number of days days left before the exam, return the smallest positive integer x that lets the student finish every chapter within days days. Return -1 if no x works.

Implement:

def min_pages_per_day(pages: list[int], days: int) -> int: ...

Constraints

  • 1 <= n <= 10^5
  • 1 <= pages[i] <= 10^9
  • 1 <= days <= 10^9

Example 1

pages = [7, 2, 6], days = 6
output: 3

With x = 3 the student needs 3 + 1 + 2 = 6 days. With x = 2 they need 4 + 1 + 3 = 8 days, which is too many.

Example 2

pages = [4, 9], days = 1
output: -1

Each chapter takes at least one day, so two chapters cannot fit in one day.

Reconstructed from the reported question; details may differ from what interviewers ask.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding