586. Maximum Number of Books You Can Take
A 0-indexed integer array of length n is provided to you, with books[i] representing the number of books on the ith shelf of a bookshelf.
You are going to take books from a contiguous section of the bookshelf spanning from l to r where 0 <= l <= r < n. For each index i in the range l <= i < r, you must take strictly fewer books from shelf i than shelf i + 1.
Return the maximum number of books you can take from the bookshelf.
Example 1:
Input : books = [5, 2, 3, 7, 1, 5]
Output : 13
Explanation :
- Take 1 book from shelf 0.
- Take 2 books from shelf 1.
- Take 3 books from shelf 2.
- Take 7 books from shelf 3.
You have taken 13 books, so return 13.
It can be proven that 13 is the maximum number of books you can take.
Example 2:
Input : books = [5, 0, 3, 7, 0, 5]
Output : 10
Explanation :
- Take 3 books from shelf 2.
- Take 7 books from shelf 3.
You have taken 10 books, so return 10.
It can be proven that 10 is the maximum number of books you can take.
Now Your Turn!
Pick the correct output for the given inputInput : books = [1, 2, 3, 4, 5, 6, 0]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= books.length <= 105
- 0 <= books[i] <= 105