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
| class Solution { int[][] books; int shelfWidth; int[] cache;
public int minHeightShelves(int[][] books, int shelfWidth) { this.books = books; this.shelfWidth = shelfWidth; int n = books.length; this.cache = new int[n]; return dfs(n - 1); }
private int dfs(int i) { if (i < 0) return 0; if (cache[i] != 0) return cache[i]; int ans = Integer.MAX_VALUE, maxHeight = 0, width = 0; for (int j = i; j >= 0; --j) { width += books[j][0]; if (width > shelfWidth) break; maxHeight = Math.max(maxHeight, books[j][1]); ans = Math.min(ans, dfs(j - 1) + maxHeight); } return cache[i] = ans; } }
|