Skip to content

Commit 7efc5cf

Browse files
authored
feat: add solutions to lc problem: No.1105 (#791)
No.1105.Filling Bookcase Shelves
1 parent 59bf77a commit 7efc5cf

File tree

4 files changed

+105
-4
lines changed

4 files changed

+105
-4
lines changed

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

+35-2
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,48 @@
6464
<!-- 这里可写当前语言的特殊实现逻辑 -->
6565

6666
```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]
6882
```
6983

7084
### **Java**
7185

7286
<!-- 这里可写当前语言的特殊实现逻辑 -->
7387

7488
```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+
}
76109
```
77110

78111
### **...**

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

+35-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,46 @@ Notice that book number 2 does not have to be on the first shelf.
5252
### **Python3**
5353

5454
```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]
5670
```
5771

5872
### **Java**
5973

6074
```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+
}
6295
```
6396

6497
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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]

0 commit comments

Comments
 (0)