Dynamic Programming pattern

Paint House II

Each house must be painted one of k colors, and no two adjacent houses may share a color. Given the cost of painting each house each color, return the minimum total painting cost. Watch it run step by step, then write your own pseudocode and get it checked by AI.

  • LeetCode 265
  • Hard
  • 2 approaches

Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.

Dynamic ProgrammingLeetCode 265

Hard

From the first idea to the best one

  1. 01 Naive · scan all other colours O(n·k²)O(n·k) space
  2. 02 Optimized · two-smallest trick O(n·k)O(k) space
Each approach plays step by step, with its time and space.

What you will learn

  • How to read Paint House II and spot the dynamic programming pattern in it
  • Naive · scan all other colours: O(n·k²) time, O(n·k) space
  • Optimized · two-smallest trick: O(n·k) time, O(k) space
  • The pseudocode for each approach, its line lit as the animation runs
  • Practice: write your own pseudocode and get it checked

More Dynamic Programming

See Paint House II run, step by step

Sign in and this page opens on the animation, with all 18 Dynamic Programming problems beside it.

DSA comes with the all labs plan and every ChaiCode batch.