Dsa
Dynamic Programming
Easy
Bookshelf Arrangement
You have a bookshelf that can hold a fixed number of books. Each book has a certain height, and you want to arrange the books on the shelf such that the total height of the books on the shelf does not exceed a limit. However, books can only be stacked if the book on top is shorter than the book below. Given a list of book heights and the maximum height limit for the shelf, find the maximum number of books that can be arranged on the shelf without exceeding the limit and maintaining the stacking condition.
Input format:
- The first line contains two integers n (1 ≤ n ≤ 100) and h (1 ≤ h ≤ 1000), the number of books and the height limit.
- The second line contains n integers representing the heights of the books.
Output format:
- Output a single integer, the maximum number of books that can be arranged.
Example:
Input:
5 10
1 2 3 4 5
Output:
4
Key concepts
dynamic_programmingcombinatoricsoptimization
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now