Skip to content

feat: add solutions to lc problem: No.1105 #791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions solution/1100-1199/1105.Filling Bookcase Shelves/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,48 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def minHeightShelves(self, books: List[List[int]], shelfWidth: int) -> int:
n = len(books)
dp = [0] * (n + 1)
dp[1] = books[0][1]
for i in range(2, n + 1):
dp[i] = books[i - 1][1] + dp[i - 1]
w, h = books[i - 1][0], books[i - 1][1]
for j in range(i - 1, 0, -1):
w += books[j - 1][0]
if w > shelfWidth:
break
h = max(books[j - 1][1], h)
dp[i] = min(dp[i], dp[j - 1] + h)
return dp[n]
```

### **Java**

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

```java

class Solution {
public int minHeightShelves(int[][] books, int shelfWidth) {
int n = books.length;
int[] dp = new int[n + 1];
dp[1] = books[0][1];
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + books[i - 1][1];
int w = books[i - 1][0], h = books[i - 1][1];
for (int j = i - 1; j > 0; j--) {
w += books[j - 1][0];
if (w > shelfWidth) {
break;
}
h = Math.max(h, books[j - 1][1]);
dp[i] = Math.min(dp[i], dp[j - 1] + h);
}
}
return dp[n];
}
}
```

### **...**
Expand Down
37 changes: 35 additions & 2 deletions solution/1100-1199/1105.Filling Bookcase Shelves/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,46 @@ Notice that book number 2 does not have to be on the first shelf.
### **Python3**

```python

class Solution:
def minHeightShelves(self, books: List[List[int]], shelfWidth: int) -> int:
n = len(books)
dp = [0] * (n + 1)
dp[1] = books[0][1]
for i in range(2, n + 1):
dp[i] = books[i - 1][1] + dp[i - 1]
w, h = books[i - 1][0], books[i - 1][1]
for j in range(i - 1, 0, -1):
w += books[j - 1][0]
if w > shelfWidth:
break
h = max(books[j - 1][1], h)
dp[i] = min(dp[i], dp[j - 1] + h)
return dp[n]
```

### **Java**

```java

class Solution {
public int minHeightShelves(int[][] books, int shelfWidth) {
int n = books.length;
int[] dp = new int[n + 1];
dp[1] = books[0][1];
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + books[i - 1][1];
int w = books[i - 1][0], h = books[i - 1][1];
for (int j = i - 1; j > 0; j--) {
w += books[j - 1][0];
if (w > shelfWidth) {
break;
}
h = Math.max(h, books[j - 1][1]);
dp[i] = Math.min(dp[i], dp[j - 1] + h);
}
}
return dp[n];
}
}
```

### **...**
Expand Down
20 changes: 20 additions & 0 deletions solution/1100-1199/1105.Filling Bookcase Shelves/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int minHeightShelves(int[][] books, int shelfWidth) {
int n = books.length;
int[] dp = new int[n + 1];
dp[1] = books[0][1];
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + books[i - 1][1];
int w = books[i - 1][0], h = books[i - 1][1];
for (int j = i - 1; j > 0; j--) {
w += books[j - 1][0];
if (w > shelfWidth) {
break;
}
h = Math.max(h, books[j - 1][1]);
dp[i] = Math.min(dp[i], dp[j - 1] + h);
}
}
return dp[n];
}
}
15 changes: 15 additions & 0 deletions solution/1100-1199/1105.Filling Bookcase Shelves/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def minHeightShelves(self, books: List[List[int]], shelfWidth: int) -> int:
n = len(books)
dp = [0] * (n + 1)
dp[1] = books[0][1]
for i in range(2, n + 1):
dp[i] = books[i - 1][1] + dp[i - 1]
w, h = books[i - 1][0], books[i - 1][1]
for j in range(i - 1, 0, -1):
w += books[j - 1][0]
if w > shelfWidth:
break
h = max(books[j - 1][1], h)
dp[i] = min(dp[i], dp[j - 1] + h)
return dp[n]