Minimum Total Height When Shelving Photo Prints in Order

Last updated: November 29, 2026

Quick Overview

Framed prints must be placed on shelves in their given order; each shelf has width W, and a shelf's height is the tallest print on it. Return the minimum total height of the shelves.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Framed prints must be placed on shelves in their given order; each shelf has width W, and a shelf's height is the tallest print on it. Return the minimum total height of the shelves.

This is a partition DP where each state tries all possible last groups, a pattern Google onsite interviewers like because the greedy (fill each shelf as much as possible) is wrong. They watch for a clean inner loop that stops when width runs out.

What the Interviewer Expects
  • Show greedy filling fails with a counterexample
  • Define best[i] for the first i prints
  • Try every possible last shelf ending at print i
  • Track running width and max height in the inner loop
  • Break early when the width limit is exceeded
Key Topics to Cover
1D dynamic programming
Partition DP
Filling bookcase shelves
Greedy counterexample
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Shelves have different widths in a fixed order.
  • Minimize the number of shelves subject to a total height limit.
  • Return where each shelf starts.
  • n is 10^5 and W is large. Can you beat O(n^2)?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list prints of (width, height) pairs, each width <= W, and integer W. Prints keep their order. Output the minimum sum of shelf heights. Greedy (fill each shelf as far as it goes) fails: with ...

High-Level Design

Let best[i] be the minimum total height for the first i prints, with best[0] = 0. The last shelf holds prints j..i-1 for some j. Walk j backward from i - 1, accumulating width and the max height...


Submit Your Answer
Markdown supported