Dynamic Programming Notes
Dynamic Programming Notes
Dynamic Programming Notes
Fibonacci Number
F(0) = 0, F(1) = 1
Example 1:
Input: n = 2
Output: 1
Example 2:
Input: n = 3
Output: 2
Try problem
shared by HeyCoach 1
Climbing Stairs
Example 1:
Input: n = 2
Output: 2
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: n = 3
Output: 3
Try problem
shared by HeyCoach 2
House Robber
Example 1:
Output: 4
Example 2:
Output: 12
Try problem
shared by HeyCoach
3
House Robber 2
Example 1:
Output: 3
Example 2:
Output: 4
Try problem
shared by HeyCoach 4
DAY 02
Knapsack Dp
Given an integer array nums, return true if you can partition the
array into two subsets such that the sum of the elements in
both subsets is equal or false otherwise.
Example 1:
Output: true
Example 2:
Output: false
Try problem
shared by HeyCoach 5
Target Sum
You are given an integer array nums and an integer target. You
want to build an expression out of nums by adding one of the
symbols '+' and '-' before each integer in nums and then
concatenate all the integers.
Example:
Output: 5
- 1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3
Try problem
shared by HeyCoach 6
Coin Change
Example 1:
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Output: -1
Try problem
shared by HeyCoach 7
Coin Change 2
Example 1:
Output: 4
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
Example 2:
Output: 0
Try problem
shared by HeyCoach
8
DAY 03
General 1-D Dp
Decode Ways
'A' -> "1", 'B' -> "2", ..., 'Z' -> "26"
Example:
Input: s = "12"
Output: 2
Try problem
shared by HeyCoach 9
Min cost for tickets
You have planned some train traveling one year in advance. The
days of the year in which you will travel are given as an integer
array days. Each day is an integer from 1 to 365.
Example:
Output: 11
Try problem
shared by HeyCoach 10
Combination Sum 4
Example 1:
Output: 7
Explanation:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Example 2:
Output: 0
Try Problem
shared by HeyCoach 11
Delete and Earn
You are given an integer array nums. You want to maximize the
number of points you get by performing the following operation
any number of times:
Example:
Output: 6
Try problem
shared by HeyCoach 12
DAY 04
Dp on grids
Unique Paths
Example:
Input: m = 3, n = 7
Output: 28
shared by HeyCoach 13
Unique Paths 2
Example:
Output: 2
Try problem
shared by HeyCoach 14
Minimum Path Sum
Example:
Output: 7
Try problem
shared by HeyCoach 15
Triangle
Given a triangle array, return the minimum path sum from top
to bottom.
For each step, you may move to an adjacent number of the row
below. More formally, if you are on index i on the current row,
you may move to either index i or index i + 1 on the next row.
Example:
Output: 11
3 4
6 5 7
4 1 8 3
is 2 + 3 + 5 + 1 = 11 (underlined above).
Try problem
shared by HeyCoach 16
DAY 05
Dp on Grids (Continued)
A falling path starts at any element in the first row and chooses
the element in the next row that is either directly below or
diagonally left/right. Specifically, the next element from
position (row, col) will be (row + 1, col - 1), (row + 1, col), or
(row + 1, col + 1).
Example:
Output: 13
Try problem
shared by HeyCoach 17
Maximal Square
Given an m x n binary matrix filled with 0's and 1's, find the
largest square containing only 1's and return its area.
Example:
Output: 4
Try problem
shared by HeyCoach 18
Number of increasing paths
You are given an m x n integer matrix grid, where you can move
that you can start from any cell and end at any cell. Since the
Example:
Output: 8
Try problem
shared by HeyCoach
19
DAY 06
Dp on Strings
Given two strings text1 and text2, return the length of their
longest common subsequence. If there is no common
subsequence, return 0.
Example 1:
Output: 3
Example 2:
Output: 0
Try problem
shared by HeyCoach 20
Longest Palindromic Subsequence
length in s.
Example 1:
Input: s = "bbbab"
Output: 4
"bbbb".
Example 2:
Input: s = "cbbd"
Output: 2
"bb".
Try problem
shared by HeyCoach
21
Word Break
Example 1:
Output: true
Example 2:
Output: false
Try problem
shared by HeyCoach 22
Min insertions to make a string palindrome
Given a string s. In one step you can insert any character at any
make s palindrome.
Example 1:
Input: s = "zzazz"
Output: 0
Example 2:
Input: s = "mbadm"
Output: 2
Try problem
shared by HeyCoach
23
DAY 07
Dp on Strings (Continued)
Edit Distance
Example:
Output: 3
Explanation:
Try problem
shared by HeyCoach 24
Distinct Subsequences
Example:
Output: 3
Explanation:
Try problem
shared by HeyCoach 25
Wildcard Matching
The matching should cover the entire input string (not partial).
Example 1:
Output: false
Example 2:
Output: true
Try problem
shared by HeyCoach 26
Shortest Common Supersequence
Given two strings str1 and str2, return the shortest string that
Example:
Output: "cabac"
Explanation:
these properties.
Try problem
shared by HeyCoach
27
DAY 08
LIS Pattern
Example 1:
Output: 4
Example 2:
Output: 4
Try problem
shared by HeyCoach 28
Russian Dolls
Example:
Output: 3
Try problem
shared by HeyCoach 29
Largest Divisible Subset
answer[i] % answer[j] == 0, o
answer[j] % answer[i] == 0
Example:
Output: [1,2]
Try problem
shared by HeyCoach
30
DAY 09
Dp + Hashing
Example 1:
Output: 3
Example 2:
Output: 1
Try problem
shared by HeyCoach 31
Longest Arithmetic Subsequence
Example 1:
Output: 3
Example 2:
Output: 4
Try problem
shared by HeyCoach
32
Number of Lis
Example 1:
Output: 2
Example 2:
Output: 5
Try problem
shared by HeyCoach 33
DAY 10
Dp on Stocks
Example:
Output: 5
Try problem
shared by HeyCoach 34
Buy and Sell Stock 2
You are given an integer array prices where prices[i] is the price
of a given stock on the ith day. On each day, you may decide to
buy and/or sell the stock.
You can only hold at most one share of the stock at any time.
However, you can buy it then immediately sell it on the same
day. Find and return the maximum profit you can achieve.
Example:
Output: 7
Total profit is 4 + 3 = 7.
Try problem
shared by HeyCoach 35
Buy and Sell Stock 3
given stock on the ith day. Find the maximum profit you can
simultaneously (i.e., you must sell the stock before you buy
again).
Example:
Output: 6
profit = 4-1 = 3.
Try problem
shared by HeyCoach
36
DAY 11
Dp on Stocks (Continued)
You are given an integer array prices where prices[i] is the price
of a given stock on the ith day, and an integer k. Find the
maximum profit you can achieve. You may complete at most k
transactions: i.e. you may buy at most k times and sell at most k
times.
Example:
Output: 2
Try problem
shared by HeyCoach 37
Buy and Sell Stock with Cooldown
given stock on the ith day. Find the maximum profit you can
(i.e., buy one and sell one share of the stock multiple times)
After you sell your stock, you cannot buy stock on the next day
Example:
Output: 3
Try problem
shared by HeyCoach
38
Buy and Sell Stock with Transaction Fee
transaction fee. Find the maximum profit you can achieve. You
Example:
Output: 8
- Buying at prices[0] = 1
- Selling at prices[3] = 8
- Buying at prices[4] = 4
- Selling at prices[5] = 9
Try Problem
shared by HeyCoach
39
DAY 12
Partition Dp
Palindrome Partitioning 2
Example 1:
Input: s = "aab"
Output: 1
Example 2:
Input: s = "a"
Output: 0
Try Problem
shared by HeyCoach 40
Burst Balloons
If you burst the ith balloon, you will get nums[i - 1] * nums[i] *
nums[i + 1] coins. If i - 1 or i + 1 goes out of bounds of the array,
then treat it as if there is a balloon with a 1 painted on it. Return
the maximum coins you can collect by bursting the balloons
wisely.
Example:
Output: 167
Explanation:
Try problem
shared by HeyCoach 41
Scramble String
Example 1:
Output: true
Example 2:
Output: false
Try problem
shared by HeyCoach 42
BONUS SECTION
Interview?
Problem:
to k.
Example:
Output: 3
Explanation:
[1, 3]
[1, 3]
[4]
Approach to solve it
MAANG - ready!
Signup Now
(link in caption)