Maximum Number of Books You Can Take - Problem

Imagine you're in a grand library where books are arranged on shelves in a special way. You have an array books where books[i] represents the number of books on the i-th shelf.

Here's the challenge: you can select any contiguous section of shelves from position l to r, but there's a strict rule - for consecutive shelves in your selection, you must take strictly fewer books from each shelf than the next one. This creates an ascending pattern in the number of books you take.

Goal: Find the maximum total number of books you can collect while following this ascending constraint.

Example: If you have [8,5,2,7,9] and choose shelves 2-4 (values [2,7,9]), you could take [1,2,3] books respectively, totaling 6 books.

Input & Output

example_1.py โ€” Basic case
$ Input: [8,5,2,7,9]
โ€บ Output: 19
๐Ÿ’ก Note: We can choose the entire array [8,5,2,7,9] and take [5,1,2,7,9] books respectively (total = 24). Or we could choose subarray [7,9] and take [1,2] books (total = 3). The optimal is to choose [2,7,9] and take [1,2,3] books for a total of 6 books. Actually, the maximum is 19 by taking the subarray ending at index 4 optimally.
example_2.py โ€” Single element
$ Input: [7]
โ€บ Output: 7
๐Ÿ’ก Note: With only one shelf, we can take all 7 books from it since there's no constraint to satisfy.
example_3.py โ€” Decreasing sequence
$ Input: [5,4,3,2,1]
โ€บ Output: 5
๐Ÿ’ก Note: Since the array is decreasing, the best we can do is take all books from any single shelf. The maximum is 5 books from the first shelf.

Visualization

Tap to expand
Maximum Books: Building the Perfect Staircase123Books Taken: 1 + 2 + 3 = 6Monotonic Stack Process:1. For each position, remove invalid previous positions2. Find leftmost valid starting position3. Calculate arithmetic sum for optimal range4. Track maximum across all positions
Understanding the Visualization
1
Identify constraint pattern
For position i to be reachable from position j, we need books[j] - j โ‰ค books[i] - i
2
Use monotonic stack
Stack maintains positions in increasing order of books[i] - i value
3
Calculate arithmetic sum
For each valid range, compute sum using arithmetic sequence formula
Key Takeaway
๐ŸŽฏ Key Insight: The monotonic stack efficiently finds the optimal 'foundation' for each staircase endpoint, allowing us to calculate the maximum books in O(n) time using arithmetic sequence properties.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each element is pushed and popped from stack at most once

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space for the monotonic stack and dp array

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค books.length โ‰ค 105
  • 0 โ‰ค books[i] โ‰ค 105
  • The selected range must be contiguous
  • Books taken must form a strictly increasing sequence
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.9K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen