-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1105.java
More file actions
35 lines (31 loc) · 1.13 KB
/
Copy pathSolution1105.java
File metadata and controls
35 lines (31 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package medium;
import java.util.Arrays;
public class Solution1105 {
public static void main(String[] args) {
int[][][] books = {
{{1, 1}, {2, 3}, {2, 3}, {1, 1}, {1, 1}, {1, 1}, {1, 2}}
};
int[] widths = {
4
};
Solution1105 solution = new Solution1105();
for (int i = 0; i < books.length; i++) {
System.out.println(solution.minHeightShelves(books[i], widths[i]));
}
}
public int minHeightShelves(int[][] books, int shelf_width) {
int[][] heights = new int[books.length][books.length];
Arrays.fill(heights[0], Integer.MAX_VALUE);
for (int i = 0; i < books.length; ++i) {
int width = 0, height = 0;
for (int j = i; j < books.length; ++j) {
width += books[j][0];
if (width > shelf_width) break;
heights[i][j] = height = Math.max(height, books[j][1]);
if (i > 0)
heights[0][j] = Math.min(heights[0][j], height + heights[0][i - 1]);
}
}
return heights[0][books.length - 1];
}
}