1105. Filling Bookcase Shelves

Back to Homepage   |     Back to Code List


class Solution {
    public int minHeightShelves(int[][] books, int shelf_width) {
        int n = books.length;
        int[] dp = new int[n + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;

        for (int i = 1; i <= n; i++) {
            int maxHeight = 0;
            int widthLeft = shelf_width;

            for (int j = i - 1; j >= 0; j--) {
                widthLeft -= books[j][0];
                maxHeight = Math.max(maxHeight, books[j][1]);

                if (widthLeft >= 0) {
                    dp[i] = Math.min(dp[i], dp[j] + maxHeight);
                }
            }
        }

        return dp[n];
    }
}