1105. Filling Bookcase Shelves Medium

@problem@discussion
#Array#Dynamic Programming



1/**
2 * [1105] Filling Bookcase Shelves
3 *
4 * You are given an array books where books[i] = [thicknessi, heighti] indicates the thickness and height of the i^th book. You are also given an integer shelfWidth.
5 * We want to place these books in order onto bookcase shelves that have a total width shelfWidth.
6 * We choose some of the books to place on this shelf such that the sum of their thickness is less than or equal to shelfWidth, then build another level of the shelf of the bookcase so that the total height of the bookcase has increased by the maximum height of the books we just put down. We repeat this process until there are no more books to place.
7 * Note that at each step of the above process, the order of the books we place is the same order as the given sequence of books.
8 * 
9 * 	For example, if we have an ordered list of 5 books, we might place the first and second book onto the first shelf, the third book on the second shelf, and the fourth and fifth book on the last shelf.
10 * 
11 * Return the minimum possible height that the total bookshelf can be after placing shelves in this manner.
12 *  
13 * Example 1:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2019/06/24/shelves.png" style="height: 500px; width: 337px;" />
15 * Input: books = [[1,1],[2,3],[2,3],[1,1],[1,1],[1,1],[1,2]], shelf_width = 4
16 * Output: 6
17 * Explanation:
18 * The sum of the heights of the 3 shelves is 1 + 3 + 2 = 6.
19 * Notice that book number 2 does not have to be on the first shelf.
20 * 
21 * Example 2:
22 * 
23 * Input: books = [[1,3],[2,4],[3,2]], shelfWidth = 6
24 * Output: 4
25 * 
26 *  
27 * Constraints:
28 * 
29 * 	1 <= books.length <= 1000
30 * 	1 <= thicknessi <= shelfWidth <= 1000
31 * 	1 <= heighti <= 1000
32 * 
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/filling-bookcase-shelves/
37// discuss: https://leetcode.com/problems/filling-bookcase-shelves/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42    pub fn min_height_shelves(books: Vec<Vec<i32>>, shelf_width: i32) -> i32 {
43        0
44    }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_1105() {
55    }
56}
57


Back
© 2025 bowen.ge All Rights Reserved.