57
57
58
58
<!-- 这里可写通用的实现逻辑 -->
59
59
60
+ ** 方法一:动态规划**
61
+
62
+ 我们用 $dp[ i] $ 表示摆放前 $i$ 本书所需要的书架的最小高度,初始时 $dp[ 0] =0$。
63
+
64
+ 遍历每一本书 $books[ i-1] $,把这本书放在书架新的一层,那么有 $dp[ i] =dp[ i-1] +h$。我们还可以将这本书往前的书放在与这本书放在同一层,尽可能减少书架的高度,那么有 $dp[ i] =min(dp[ i] , dp[ j-1] +h)$。其中 $h$ 是最后一层的书的最大高度。
65
+
66
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 表示数组 ` books ` 的长度。
67
+
60
68
<!-- tabs:start -->
61
69
62
70
### ** Python3**
@@ -68,15 +76,13 @@ class Solution:
68
76
def minHeightShelves (self , books : List[List[int ]], shelfWidth : int ) -> int :
69
77
n = len (books)
70
78
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 ]
79
+ for i, (w, h) in enumerate (books, 1 ):
80
+ dp[i] = dp[i - 1 ] + h
75
81
for j in range (i - 1 , 0 , - 1 ):
76
82
w += books[j - 1 ][0 ]
77
83
if w > shelfWidth:
78
84
break
79
- h = max (books[j - 1 ][1 ], h )
85
+ h = max (h, books[j - 1 ][1 ])
80
86
dp[i] = min (dp[i], dp[j - 1 ] + h)
81
87
return dp[n]
82
88
```
@@ -90,11 +96,10 @@ class Solution {
90
96
public int minHeightShelves (int [][] books , int shelfWidth ) {
91
97
int n = books. length;
92
98
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 ];
99
+ for (int i = 1 ; i <= n; ++ i) {
96
100
int w = books[i - 1 ][0 ], h = books[i - 1 ][1 ];
97
- for (int j = i - 1 ; j > 0 ; j-- ) {
101
+ dp[i] = dp[i - 1 ] + h;
102
+ for (int j = i - 1 ; j > 0 ; -- j) {
98
103
w += books[j - 1 ][0 ];
99
104
if (w > shelfWidth) {
100
105
break ;
@@ -108,6 +113,65 @@ class Solution {
108
113
}
109
114
```
110
115
116
+ ### ** C++**
117
+
118
+ ``` cpp
119
+ class Solution {
120
+ public:
121
+ int minHeightShelves(vector<vector<int >>& books, int shelfWidth) {
122
+ int n = books.size();
123
+ vector<int > dp(n + 1);
124
+ for (int i = 1; i <= n; ++i) {
125
+ int w = books[ i - 1] [ 0 ] , h = books[ i - 1] [ 1 ] ;
126
+ dp[ i] = dp[ i - 1] + h;
127
+ for (int j = i - 1; j > 0; --j) {
128
+ w += books[ j - 1] [ 0 ] ;
129
+ if (w > shelfWidth) break;
130
+ h = max(h, books[ j - 1] [ 1 ] );
131
+ dp[ i] = min(dp[ i] , dp[ j - 1] + h);
132
+ }
133
+ }
134
+ return dp[ n] ;
135
+ }
136
+ };
137
+ ```
138
+
139
+ ### **Go**
140
+
141
+ ```go
142
+ func minHeightShelves(books [][]int, shelfWidth int) int {
143
+ n := len(books)
144
+ dp := make([]int, n+1)
145
+ for i := 1; i <= n; i++ {
146
+ w, h := books[i-1][0], books[i-1][1]
147
+ dp[i] = dp[i-1] + h
148
+ for j := i - 1; j > 0; j-- {
149
+ w += books[j-1][0]
150
+ if w > shelfWidth {
151
+ break
152
+ }
153
+ h = max(h, books[j-1][1])
154
+ dp[i] = min(dp[i], dp[j-1]+h)
155
+ }
156
+ }
157
+ return dp[n]
158
+ }
159
+
160
+ func max(a, b int) int {
161
+ if a > b {
162
+ return a
163
+ }
164
+ return b
165
+ }
166
+
167
+ func min(a, b int) int {
168
+ if a < b {
169
+ return a
170
+ }
171
+ return b
172
+ }
173
+ ```
174
+
111
175
### ** ...**
112
176
113
177
```
0 commit comments