Red-Green-Code

Deliberate practice techniques for software developers

  • Home
  • About
  • Contact
  • Project 462
  • CP FAQ
  • Newsletter

Multidimensional Dynamic Programming

By Duncan Smith Mar 6 0

A robot floating inside a cube

As we have seen in previous weeks, a key step in analyzing a dynamic programming problem is selecting state variables that identify the subproblems to be solved. The solutions to these subproblems are stored in a table, which allows us to retrieve them efficiently when they come up again. So far, we have looked at problems with only one state variable. But the dynamic programming process also works when more than one state variable is required.

State variables are closely related to the table used to store subproblem solutions. We can think of them as indices into the table. So in last week’s problem, Longest Increasing Subsequence, we had a variable i storing a position in the input array. And we had an array dp where dp[i] stored the length of the longest increasing subsequence ending at position i.

When we select state variables, the goal is to select the minimum number of variables required to solve the problem. This has two advantages. In terms of storage resources, using one state variable means we only need $O(n)$ space to store the solution to every subproblem. If we designed our dp table for Longest Increasing Subsequence to store the length of the longest increasing subsequence starting at position i and ending at position j, we would need a two-dimensional array, or $O(n^2)$ space.

The other advantage of fewer state variables is a simpler solution. That doesn’t mean a solution that’s easier to invent. A solution with two variable might be more obvious, just as it’s easier to come up with brute force solution than an $O(n \log n)$ or $O(n)$ solution. But an extra state variable requires more complexity to keep track of. As with any LeetCode problem, the best approach is often to implement a more complex but more obvious solution first, and then optimize it. So you may start with multiple variables and discover that you can remove some of them.

For the LIS problem, we only need to store one solution for each ending position. In Coin Change, the only state variable required is amount, even though the input includes both an amount and an array of coins. But there are other problems that require more than one variable to identify a state. The additional variables may be explicitly identified in the problem statement, or you may come across them as you analyze the problem. There’s no step-by-step process to find these other variables. You just have to solve more problems and see a variety of patterns.

Next week, we’ll look at a well-known dynamic programming problem whose solution requires two state variables.

(Image credit: DALLĀ·E 3)

For an introduction to this year’s project, see A Project for 2024.

Categories: LeetCode

Prev
Next

Stay in the Know

I'm trying out the latest learning techniques on software development concepts, and writing about what works best. Sound interesting? Subscribe to my free newsletter to keep up to date. Learn More
Unsubscribing is easy, and I'll keep your email address private.

Getting Started

Are you new here? Check out my review posts for a tour of the archives:

  • 2023 in Review: 50 LeetCode Tips
  • 2022 in Review: Content Bots
  • 2021 in Review: Thoughts on Solving Programming Puzzles
  • Lessons from the 2020 LeetCode Monthly Challenges
  • 2019 in Review
  • Competitive Programming Frequently Asked Questions: 2018 In Review
  • What I Learned Working On Time Tortoise in 2017
  • 2016 in Review
  • 2015 in Review
  • 2015 Summer Review

Archives

Recent Posts

  • Will AI Coding Assistants “Deskill” Us? January 30, 2026
  • Stateless by Design: How to Work With AI Coding Assistants December 31, 2025
  • Do Coding Bots Mean the End of Coding Interviews? December 31, 2024
  • Another Project for 2024 May 8, 2024
  • Dynamic Programming Wrap-Up May 1, 2024
  • LeetCode 91: Decode Ways April 24, 2024
  • LeetCode 70: Climbing Stairs April 17, 2024
  • LeetCode 221: Maximal Square April 10, 2024
  • Using Dynamic Programming for Maximum Product Subarray April 3, 2024
  • LeetCode 62: Unique Paths March 27, 2024
Red-Green-Code
  • Home
  • About
  • Contact
  • Project 462
  • CP FAQ
  • Newsletter
Copyright © 2026 Duncan Smith