File tree 4 files changed +105
-4
lines changed
solution/1100-1199/1105.Filling Bookcase Shelves
4 files changed +105
-4
lines changed Original file line number Diff line number Diff line change 64
64
<!-- 这里可写当前语言的特殊实现逻辑 -->
65
65
66
66
``` python
67
-
67
+ class Solution :
68
+ def minHeightShelves (self , books : List[List[int ]], shelfWidth : int ) -> int :
69
+ n = len (books)
70
+ dp = [0 ] * (n + 1 )
71
+ dp[1 ] = books[0 ][1 ]
72
+ for i in range (2 , n + 1 ):
73
+ dp[i] = books[i - 1 ][1 ] + dp[i - 1 ]
74
+ w, h = books[i - 1 ][0 ], books[i - 1 ][1 ]
75
+ for j in range (i - 1 , 0 , - 1 ):
76
+ w += books[j - 1 ][0 ]
77
+ if w > shelfWidth:
78
+ break
79
+ h = max (books[j - 1 ][1 ], h)
80
+ dp[i] = min (dp[i], dp[j - 1 ] + h)
81
+ return dp[n]
68
82
```
69
83
70
84
### ** Java**
71
85
72
86
<!-- 这里可写当前语言的特殊实现逻辑 -->
73
87
74
88
``` java
75
-
89
+ class Solution {
90
+ public int minHeightShelves (int [][] books , int shelfWidth ) {
91
+ int n = books. length;
92
+ int [] dp = new int [n + 1 ];
93
+ dp[1 ] = books[0 ][1 ];
94
+ for (int i = 2 ; i <= n; i++ ) {
95
+ dp[i] = dp[i - 1 ] + books[i - 1 ][1 ];
96
+ int w = books[i - 1 ][0 ], h = books[i - 1 ][1 ];
97
+ for (int j = i - 1 ; j > 0 ; j-- ) {
98
+ w += books[j - 1 ][0 ];
99
+ if (w > shelfWidth) {
100
+ break ;
101
+ }
102
+ h = Math . max(h, books[j - 1 ][1 ]);
103
+ dp[i] = Math . min(dp[i], dp[j - 1 ] + h);
104
+ }
105
+ }
106
+ return dp[n];
107
+ }
108
+ }
76
109
```
77
110
78
111
### ** ...**
Original file line number Diff line number Diff line change @@ -52,13 +52,46 @@ Notice that book number 2 does not have to be on the first shelf.
52
52
### ** Python3**
53
53
54
54
``` python
55
-
55
+ class Solution :
56
+ def minHeightShelves (self , books : List[List[int ]], shelfWidth : int ) -> int :
57
+ n = len (books)
58
+ dp = [0 ] * (n + 1 )
59
+ dp[1 ] = books[0 ][1 ]
60
+ for i in range (2 , n + 1 ):
61
+ dp[i] = books[i - 1 ][1 ] + dp[i - 1 ]
62
+ w, h = books[i - 1 ][0 ], books[i - 1 ][1 ]
63
+ for j in range (i - 1 , 0 , - 1 ):
64
+ w += books[j - 1 ][0 ]
65
+ if w > shelfWidth:
66
+ break
67
+ h = max (books[j - 1 ][1 ], h)
68
+ dp[i] = min (dp[i], dp[j - 1 ] + h)
69
+ return dp[n]
56
70
```
57
71
58
72
### ** Java**
59
73
60
74
``` java
61
-
75
+ class Solution {
76
+ public int minHeightShelves (int [][] books , int shelfWidth ) {
77
+ int n = books. length;
78
+ int [] dp = new int [n + 1 ];
79
+ dp[1 ] = books[0 ][1 ];
80
+ for (int i = 2 ; i <= n; i++ ) {
81
+ dp[i] = dp[i - 1 ] + books[i - 1 ][1 ];
82
+ int w = books[i - 1 ][0 ], h = books[i - 1 ][1 ];
83
+ for (int j = i - 1 ; j > 0 ; j-- ) {
84
+ w += books[j - 1 ][0 ];
85
+ if (w > shelfWidth) {
86
+ break ;
87
+ }
88
+ h = Math . max(h, books[j - 1 ][1 ]);
89
+ dp[i] = Math . min(dp[i], dp[j - 1 ] + h);
90
+ }
91
+ }
92
+ return dp[n];
93
+ }
94
+ }
62
95
```
63
96
64
97
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int minHeightShelves (int [][] books , int shelfWidth ) {
3
+ int n = books .length ;
4
+ int [] dp = new int [n + 1 ];
5
+ dp [1 ] = books [0 ][1 ];
6
+ for (int i = 2 ; i <= n ; i ++) {
7
+ dp [i ] = dp [i - 1 ] + books [i - 1 ][1 ];
8
+ int w = books [i - 1 ][0 ], h = books [i - 1 ][1 ];
9
+ for (int j = i - 1 ; j > 0 ; j --) {
10
+ w += books [j - 1 ][0 ];
11
+ if (w > shelfWidth ) {
12
+ break ;
13
+ }
14
+ h = Math .max (h , books [j - 1 ][1 ]);
15
+ dp [i ] = Math .min (dp [i ], dp [j - 1 ] + h );
16
+ }
17
+ }
18
+ return dp [n ];
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def minHeightShelves (self , books : List [List [int ]], shelfWidth : int ) -> int :
3
+ n = len (books )
4
+ dp = [0 ] * (n + 1 )
5
+ dp [1 ] = books [0 ][1 ]
6
+ for i in range (2 , n + 1 ):
7
+ dp [i ] = books [i - 1 ][1 ] + dp [i - 1 ]
8
+ w , h = books [i - 1 ][0 ], books [i - 1 ][1 ]
9
+ for j in range (i - 1 , 0 , - 1 ):
10
+ w += books [j - 1 ][0 ]
11
+ if w > shelfWidth :
12
+ break
13
+ h = max (books [j - 1 ][1 ], h )
14
+ dp [i ] = min (dp [i ], dp [j - 1 ] + h )
15
+ return dp [n ]
You can’t perform that action at this time.
0 commit comments