diff --git a/solution/1100-1199/1105.Filling Bookcase Shelves/README.md b/solution/1100-1199/1105.Filling Bookcase Shelves/README.md index a4b60c0941cca..9e442242d610b 100644 --- a/solution/1100-1199/1105.Filling Bookcase Shelves/README.md +++ b/solution/1100-1199/1105.Filling Bookcase Shelves/README.md @@ -64,7 +64,21 @@ ```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** @@ -72,7 +86,26 @@ ```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]; + } +} ``` ### **...** diff --git a/solution/1100-1199/1105.Filling Bookcase Shelves/README_EN.md b/solution/1100-1199/1105.Filling Bookcase Shelves/README_EN.md index 7de025648aa0c..2f06956fee337 100644 --- a/solution/1100-1199/1105.Filling Bookcase Shelves/README_EN.md +++ b/solution/1100-1199/1105.Filling Bookcase Shelves/README_EN.md @@ -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]; + } +} ``` ### **...** diff --git a/solution/1100-1199/1105.Filling Bookcase Shelves/Solution.java b/solution/1100-1199/1105.Filling Bookcase Shelves/Solution.java new file mode 100644 index 0000000000000..ab988d7791237 --- /dev/null +++ b/solution/1100-1199/1105.Filling Bookcase Shelves/Solution.java @@ -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]; + } +} \ No newline at end of file diff --git a/solution/1100-1199/1105.Filling Bookcase Shelves/Solution.py b/solution/1100-1199/1105.Filling Bookcase Shelves/Solution.py new file mode 100644 index 0000000000000..159aecc744afc --- /dev/null +++ b/solution/1100-1199/1105.Filling Bookcase Shelves/Solution.py @@ -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] \ No newline at end of file