Opening your workspace…
Workspace/Coding room
Opening your workspace…
The princess lies on a stack of mattresses with a single pea underneath. She can feel the pea if the total thickness above it is at most her sensitivity limit. The queen lifts one mattress at a time from the top and lets the princess try again. Return the fewest mattresses the queen must lift for the princess to feel the pea.
[4,3,6,2], 7Output2Lifting the top mattress leaves 13 cm. Lifting the next one leaves exactly 7 cm, so the princess feels the pea.
[5,2,3], 12Output0The whole stack is only 10 cm thick, so the princess already feels the pea.
Start with the total thickness, then remove mattresses from the top.
function solve(mattresses, limit) { // Write your solution here. }
Tab inserts two spaces. Shift+Tab outdents. Use the arrow keys and Tab to choose a suggestion. Press Escape, then Tab, to leave the editor. Press Control or Command with apostrophe to run samples or with Enter to submit.