Skip to content

Commit 8754817

Browse files
committed
feat: add solutions to lc problem: No.1105
No.1105.Filling Bookcase Shelves
1 parent a8329e6 commit 8754817

File tree

9 files changed

+196
-33
lines changed

9 files changed

+196
-33
lines changed

solution/1100-1199/1105.Filling Bookcase Shelves/README.md

+73-9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

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+
6068
<!-- tabs:start -->
6169

6270
### **Python3**
@@ -68,15 +76,13 @@ class Solution:
6876
def minHeightShelves(self, books: List[List[int]], shelfWidth: int) -> int:
6977
n = len(books)
7078
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
7581
for j in range(i - 1, 0, -1):
7682
w += books[j - 1][0]
7783
if w > shelfWidth:
7884
break
79-
h = max(books[j - 1][1], h)
85+
h = max(h, books[j - 1][1])
8086
dp[i] = min(dp[i], dp[j - 1] + h)
8187
return dp[n]
8288
```
@@ -90,11 +96,10 @@ class Solution {
9096
public int minHeightShelves(int[][] books, int shelfWidth) {
9197
int n = books.length;
9298
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) {
96100
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) {
98103
w += books[j - 1][0];
99104
if (w > shelfWidth) {
100105
break;
@@ -108,6 +113,65 @@ class Solution {
108113
}
109114
```
110115

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+
111175
### **...**
112176

113177
```

solution/1100-1199/1105.Filling Bookcase Shelves/README_EN.md

+65-9
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,13 @@ class Solution:
5656
def minHeightShelves(self, books: List[List[int]], shelfWidth: int) -> int:
5757
n = len(books)
5858
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]
59+
for i, (w, h) in enumerate(books, 1):
60+
dp[i] = dp[i - 1] + h
6361
for j in range(i - 1, 0, -1):
6462
w += books[j - 1][0]
6563
if w > shelfWidth:
6664
break
67-
h = max(books[j - 1][1], h)
65+
h = max(h, books[j - 1][1])
6866
dp[i] = min(dp[i], dp[j - 1] + h)
6967
return dp[n]
7068
```
@@ -76,11 +74,10 @@ class Solution {
7674
public int minHeightShelves(int[][] books, int shelfWidth) {
7775
int n = books.length;
7876
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];
77+
for (int i = 1; i <= n; ++i) {
8278
int w = books[i - 1][0], h = books[i - 1][1];
83-
for (int j = i - 1; j > 0; j--) {
79+
dp[i] = dp[i - 1] + h;
80+
for (int j = i - 1; j > 0; --j) {
8481
w += books[j - 1][0];
8582
if (w > shelfWidth) {
8683
break;
@@ -94,6 +91,65 @@ class Solution {
9491
}
9592
```
9693

94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {
100+
int n = books.size();
101+
vector<int> dp(n + 1);
102+
for (int i = 1; i <= n; ++i) {
103+
int w = books[i - 1][0], h = books[i - 1][1];
104+
dp[i] = dp[i - 1] + h;
105+
for (int j = i - 1; j > 0; --j) {
106+
w += books[j - 1][0];
107+
if (w > shelfWidth) break;
108+
h = max(h, books[j - 1][1]);
109+
dp[i] = min(dp[i], dp[j - 1] + h);
110+
}
111+
}
112+
return dp[n];
113+
}
114+
};
115+
```
116+
117+
### **Go**
118+
119+
```go
120+
func minHeightShelves(books [][]int, shelfWidth int) int {
121+
n := len(books)
122+
dp := make([]int, n+1)
123+
for i := 1; i <= n; i++ {
124+
w, h := books[i-1][0], books[i-1][1]
125+
dp[i] = dp[i-1] + h
126+
for j := i - 1; j > 0; j-- {
127+
w += books[j-1][0]
128+
if w > shelfWidth {
129+
break
130+
}
131+
h = max(h, books[j-1][1])
132+
dp[i] = min(dp[i], dp[j-1]+h)
133+
}
134+
}
135+
return dp[n]
136+
}
137+
138+
func max(a, b int) int {
139+
if a > b {
140+
return a
141+
}
142+
return b
143+
}
144+
145+
func min(a, b int) int {
146+
if a < b {
147+
return a
148+
}
149+
return b
150+
}
151+
```
152+
97153
### **...**
98154

99155
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {
4+
int n = books.size();
5+
vector<int> dp(n + 1);
6+
for (int i = 1; i <= n; ++i) {
7+
int w = books[i - 1][0], h = books[i - 1][1];
8+
dp[i] = dp[i - 1] + h;
9+
for (int j = i - 1; j > 0; --j) {
10+
w += books[j - 1][0];
11+
if (w > shelfWidth) break;
12+
h = max(h, books[j - 1][1]);
13+
dp[i] = min(dp[i], dp[j - 1] + h);
14+
}
15+
}
16+
return dp[n];
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
func minHeightShelves(books [][]int, shelfWidth int) int {
2+
n := len(books)
3+
dp := make([]int, n+1)
4+
for i := 1; i <= n; i++ {
5+
w, h := books[i-1][0], books[i-1][1]
6+
dp[i] = dp[i-1] + h
7+
for j := i - 1; j > 0; j-- {
8+
w += books[j-1][0]
9+
if w > shelfWidth {
10+
break
11+
}
12+
h = max(h, books[j-1][1])
13+
dp[i] = min(dp[i], dp[j-1]+h)
14+
}
15+
}
16+
return dp[n]
17+
}
18+
19+
func max(a, b int) int {
20+
if a > b {
21+
return a
22+
}
23+
return b
24+
}
25+
26+
func min(a, b int) int {
27+
if a < b {
28+
return a
29+
}
30+
return b
31+
}

solution/1100-1199/1105.Filling Bookcase Shelves/Solution.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ class Solution {
22
public int minHeightShelves(int[][] books, int shelfWidth) {
33
int n = books.length;
44
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];
5+
for (int i = 1; i <= n; ++i) {
86
int w = books[i - 1][0], h = books[i - 1][1];
9-
for (int j = i - 1; j > 0; j--) {
7+
dp[i] = dp[i - 1] + h;
8+
for (int j = i - 1; j > 0; --j) {
109
w += books[j - 1][0];
1110
if (w > shelfWidth) {
1211
break;

solution/1100-1199/1105.Filling Bookcase Shelves/Solution.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ class Solution:
22
def minHeightShelves(self, books: List[List[int]], shelfWidth: int) -> int:
33
n = len(books)
44
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]
5+
for i, (w, h) in enumerate(books, 1):
6+
dp[i] = dp[i - 1] + h
97
for j in range(i - 1, 0, -1):
108
w += books[j - 1][0]
119
if w > shelfWidth:
1210
break
13-
h = max(books[j - 1][1], h)
11+
h = max(h, books[j - 1][1])
1412
dp[i] = min(dp[i], dp[j - 1] + h)
1513
return dp[n]

solution/1100-1199/1106.Parsing A Boolean Expression/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ class Solution:
109109
class Solution {
110110
public boolean parseBoolExpr(String expression) {
111111
Deque<Character> stk = new ArrayDeque<>();
112-
for (int i = 0; i < expression.length(); ++i) {
113-
char c = expression.charAt(i);
112+
for (char c : expression.toCharArray()) {
114113
if (c != '(' && c != ')' && c != ',') {
115114
stk.push(c);
116115
} else if (c == ')') {

solution/1100-1199/1106.Parsing A Boolean Expression/README_EN.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ class Solution:
9292
class Solution {
9393
public boolean parseBoolExpr(String expression) {
9494
Deque<Character> stk = new ArrayDeque<>();
95-
for (int i = 0; i < expression.length(); ++i) {
96-
char c = expression.charAt(i);
95+
for (char c : expression.toCharArray()) {
9796
if (c != '(' && c != ')' && c != ',') {
9897
stk.push(c);
9998
} else if (c == ')') {

solution/1100-1199/1106.Parsing A Boolean Expression/Solution.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution {
22
public boolean parseBoolExpr(String expression) {
33
Deque<Character> stk = new ArrayDeque<>();
4-
for (int i = 0; i < expression.length(); ++i) {
5-
char c = expression.charAt(i);
4+
for (char c : expression.toCharArray()) {
65
if (c != '(' && c != ')' && c != ',') {
76
stk.push(c);
87
} else if (c == ')') {

0 commit comments

Comments
 (0)