class Solution:
    def minHeightShelves(self, books, shelf_width: int) -> int:
        n, dp = len(books), [float('inf')] * (len(books)+1)
        dp[0] = 0

        for i in range(1, n+1):
            max_width, max_height, j = shelf_width, 0, i - 1
            
            while j >= 0 and max_width - books[j][0] >= 0:
                max_width -= books[j][0]
                max_height = max(max_height, books[j][1])
                dp[i] = max_height
                j -= 1

            if j >= 0 and max_width - books[j][0] < 0:
                j = i - 1
                dp[i] = float('inf')
                width, height = 0, 0
                while j >= 0 and width + books[j][0] <= shelf_width:
                    width = width + books[j][0]
                    height = max(books[j][1], height)
                    dp[i] = min(dp[i], height + dp[j])
                    j -= 1

        return dp[n]
class Solution {
    int[][] books;
    int shefWidth;

    Map<String, Integer> memo = new HashMap();

    public int findMinHeight(int curr, int maxHeight, int wRem) {
        String key = curr  + ":" + wRem ;
        
        if(memo.containsKey(key)) return memo.get(key);


        if(curr == books.length ) return maxHeight;
        int[] currBook = books[curr];

        memo.put( key, currBook[0] <= wRem ? Math.min(   maxHeight + findMinHeight(curr + 1, currBook[1], shefWidth - currBook[0]) , // new shelf
                  findMinHeight(curr + 1, Math.max(maxHeight, currBook[1]),wRem - currBook[0] )) // same shelf
                : maxHeight + findMinHeight(curr + 1,  currBook[1], shefWidth - currBook[0])
        ); // new shelf

        return memo.get(key);
    }


    public int minHeightShelves(int[][] books, int shelfWidth) {
        this.books = books;
        this.shefWidth = shelfWidth;
        return findMinHeight(0, 0,  shelfWidth);
    }
}
Filling Bookcase Shelves
You are given an array books where books[i] = [thicknessi, heighti] indicates the thickness and height of the ith book. You are also given an integer shelfWidth.

We want to place these books in order onto bookcase shelves that have a total width shelfWidth.

We choose some of the books to place on this shelf such that the sum of their thickness is less than or equal to shelfWidth, then build another level of the shelf of the bookcase so that the total height of the bookcase has increased by the maximum height of the books we just put down. We repeat this process until there are no more books to place.

Note that at each step of the above process, the order of the books we place is the same order as the given sequence of books.


	For example, if we have an ordered list of 5 books, we might place the first and second book onto the first shelf, the third book on the second shelf, and the fourth and fifth book on the last shelf.


Return the minimum possible height that the total bookshelf can be after placing shelves in this manner.

&nbsp;
Example 1:

Input: books = [[1,1],[2,3],[2,3],[1,1],[1,1],[1,1],[1,2]], shelf_width = 4
Output: 6
Explanation:
The sum of the heights of the 3 shelves is 1 + 3 + 2 = 6.
Notice that book number 2 does not have to be on the first shelf.


Example 2:

Input: books = [[1,3],[2,4],[3,2]], shelfWidth = 6
Output: 4


&nbsp;
Constraints:


	1 &lt;= books.length &lt;= 1000
	1 &lt;= thicknessi &lt;= shelfWidth &lt;= 1000
	1 &lt;= heighti &lt;= 1000


var minHeightShelves = function(books, shelfWidth) {
    let booksArr = []

    for(let i = 0; i < books.length; i++) {
        let remainingWidth = shelfWidth - books[i][0]
        let bookHeight = books[i][1]

        let maxHeight = bookHeight

        let prevSum = booksArr[i - 1] !== undefined ? booksArr[i - 1] : 0
        let minSumHeight = bookHeight + prevSum

        for(let x = i - 1; x >= 0 ; x--) {

            let prevBookWidth = books[x][0]
            let prevBookHeight = books[x][1]
            if(remainingWidth - prevBookWidth < 0) break
            remainingWidth -= prevBookWidth

            prevSum = booksArr[x - 1] !== undefined ? booksArr[x - 1] : 0

            maxHeight = Math.max(maxHeight, prevBookHeight)

            minSumHeight = Math.min(prevSum + maxHeight, minSumHeight)
        }
        booksArr[i] = minSumHeight
    }

    return booksArr[books.length - 1]
};
class Solution {
public:
    int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {
        int n=books.size();
        vector<vector<int>> cost(n+1,vector<int>(n+1));
        for(int i=1;i<=n;i++)
        {
            int height=books[i-1][1];
            int width=books[i-1][0];
            cost[i][i]=height;
            for(int j=i+1;j<=n;j++)
            {
                height=max(height,books[j-1][1]);
                width+=books[j-1][0];
                if(width<=shelfWidth) cost[i][j]=height;
                else cost[i][j]=-1;
            }
        }
        vector<int> ans(n+1);
        ans[0]=0;
        for(int i=1;i<=n;i++)
        {
            ans[i]=INT_MAX;
            for(int j=1;j<=i;j++)
            {
                if(cost[j][i]==-1) continue;
                if(ans[j-1]!=INT_MAX) ans[i]=min(ans[i],ans[j-1]+cost[j][i]);
            }
        }
        return ans[n];
    }
};