Book Club Reading Plan
Your book club must finish the novel in days days. You know each chapter's page count. You read chapters in order, never stop mid-chapter, and read at least one chapter on each day you use.
You want a single daily page limit that is as low as possible while still letting the club finish by the deadline.
Your task
Write dailyPageLimit(chapters, days). Split chapters, in order, into at most days consecutive groups (each day reads one group). Return the smallest possible value of the largest group's page total.
Finishing early is fine, so using fewer than days days is allowed.
dailyPageLimit(chapters: int[], days: int) → int
Examples
chapters = [12, 30, 8, 25, 15, 10] days = 3
42
Read 12 + 30 = 42, then 8 + 25 = 33, then 15 + 10 = 25: the busiest day is 42. A limit of 41 needs 4 days: 12 | 30 + 8 | 25 + 15 | 10. The average, 100 / 3 ≈ 34, is not achievable.
chapters = [40, 10, 10, 10] days = 2
40
The 40-page chapter cannot be split, so the limit is at least 40. A limit of 40 works, because day 1 reads that chapter and day 2 reads the other 30 pages.
Constraints
- 1 ≤ chapters.length ≤ 10,000
- 1 ≤ chapters[i] ≤ 10,000
- 1 ≤ days ≤ 10,000
Start with brute force
Try every limit from the longest chapter upward with the greedy day count, stopping at the first that fits. Or try every way to cut the chapters into groups.
Trying every way to cut the chapters is exponential. Counting up through the limits costs O(n × S), and S can reach 100,000,000 pages. Binary search on the limit needs about 27 tries, and each try is one O(n) pass.
Hints
0 of 4 shown- Hint 1
Forget finding the best split. Given a limit, say 41 for the first example, could you quickly tell whether 3 days are enough?
- Hint 2
You can: read greedily. Add chapters to the current day until the next one would go over the limit, then start a new day. Reading as much as the limit allows each day never costs extra days.
- Hint 3
If a limit works, every larger limit works too, so you want the smallest limit that works. Think about which smallest and largest limits are worth trying.
- Hint 4
Binary search the limit between
max(chapters)(the longest chapter must fit) andsum(chapters)(everything in one day). For each middle limit, run the greedy day count. If it fits indays, keep it and search lower; otherwise search higher.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Finding the best split directly is hard, but checking whether a given limit works is easy, and once a limit works every bigger limit works too. So the solution is binary search on the answer, using that check.
Trace [12, 30, 8, 25, 15, 10] with 3 days. The limit lies between 30 (the longest chapter) and 100 (the total).
lo = 30,hi = 100,mid = 65. The greedy split is 12 + 30 + 8 = 50 and 25 + 15 + 10 = 50, which takes 2 days and fits, sohi = 65.- With
mid = 47the split is 12 + 30 = 42, 8 + 25 = 33 and 15 + 10 = 25, which takes 3 days and fits, sohi = 47. - With
mid = 38the split is 12, then 30 + 8 = 38, then 25, then 15 + 10, which takes 4 days and is too many, solo = 39. - With
mid = 43the split is the same as for 47, which takes 3 days and fits, sohi = 43. - With
mid = 41the split is 12, 30 + 8, 25 + 15 and 10, which takes 4 days, solo = 42. - With
mid = 42the split is 12 + 30, 8 + 25 and 15 + 10, which takes 3 days and fits, sohi = 42. Nowlo = hi = 42, and the answer is 42.
Start lo at the longest chapter, not at 1. Below that value the greedy check gives wrong answers, because it would put a 30-page chapter on a single day under a 20-page limit and count it as fitting.
function dailyPageLimit(chapters, days) {
// how many days does a given limit need, reading greedily?
const daysNeeded = (limit) => {
let count = 1, today = 0;
for (const pages of chapters) {
if (today + pages > limit) { count++; today = 0; } // start a new day
today += pages;
}
return count;
};
let lo = Math.max(...chapters);
let hi = chapters.reduce((a, b) => a + b, 0);
while (lo < hi) {
const mid = Math.floor((lo + hi) / 2);
if (daysNeeded(mid) <= days) hi = mid; // works: try lower
else lo = mid + 1; // too low
}
return lo;
}def daily_page_limit(chapters, days):
# how many days does a given limit need, reading greedily?
def days_needed(limit):
count, today = 1, 0
for pages in chapters:
if today + pages > limit:
count += 1 # start a new day
today = 0
today += pages
return count
lo, hi = max(chapters), sum(chapters)
while lo < hi:
mid = (lo + hi) // 2
if days_needed(mid) <= days:
hi = mid # works: try lower
else:
lo = mid + 1 # too low
return loRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.