-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdp_1d.cpp
More file actions
286 lines (268 loc) · 8.7 KB
/
dp_1d.cpp
File metadata and controls
286 lines (268 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
1D Dynamic Programming
Mathematical Foundation: dp[i] = f(dp[j]) for j < i
Optimal substructure: optimal solution contains optimal solutions to subproblems
Overlapping subproblems: memoization avoids recomputation
Time: O(n), Space: O(n) → O(1) with space optimization
*/
#include <bits/stdc++.h>
using namespace std;
// Fibonacci (Basic DP)
// LeetCode: 509. Fibonacci Number
// https://leetcode.com/problems/fibonacci-number/
// Related:
// 70. Climbing Stairs
// https://leetcode.com/problems/climbing-stairs/
// 1137. N-th Tribonacci Number
// https://leetcode.com/problems/n-th-tribonacci-number/
// 746. Min Cost Climbing Stairs
// https://leetcode.com/problems/min-cost-climbing-stairs/
int fib(int n) {
if (n <= 1) return n;
int a = 0, b = 1;
for (int i = 2; i <= n; i++) {
int c = a + b;
a = b; b = c;
}
return b;
}
// Climbing Stairs (Classic DP)
// LeetCode: 70. Climbing Stairs
// https://leetcode.com/problems/climbing-stairs/
// Related:
// 746. Min Cost Climbing Stairs
// https://leetcode.com/problems/min-cost-climbing-stairs/
// 1137. N-th Tribonacci Number
// https://leetcode.com/problems/n-th-tribonacci-number/
// 91. Decode Ways
// https://leetcode.com/problems/decode-ways/
// 509. Fibonacci Number
// https://leetcode.com/problems/fibonacci-number/
int climbStairs(int n) {
if (n <= 2) return n;
int a = 1, b = 2;
for (int i = 3; i <= n; i++) {
int c = a + b;
a = b; b = c;
}
return b;
}
// House Robber (Skip Adjacent DP)
// LeetCode: 198. House Robber
// https://leetcode.com/problems/house-robber/
// Related:
// 213. House Robber II
// https://leetcode.com/problems/house-robber-ii/
// 337. House Robber III
// https://leetcode.com/problems/house-robber-iii/
// 740. Delete and Earn
// https://leetcode.com/problems/delete-and-earn/
// 2320. Count Number of Ways to Place Houses
// https://leetcode.com/problems/count-number-of-ways-to-place-houses/
int rob(vector<int>& nums) {
int prev = 0, curr = 0;
for (int x : nums) {
int temp = max(curr, prev + x);
prev = curr;
curr = temp;
}
return curr;
}
// House Robber II - Circular Array DP
// LeetCode: 213. House Robber II
// https://leetcode.com/problems/house-robber-ii/
// Related:
// 198. House Robber
// https://leetcode.com/problems/house-robber/
// 740. Delete and Earn
// https://leetcode.com/problems/delete-and-earn/
// 256. Paint House
// https://leetcode.com/problems/paint-house/
// 265. Paint House II
// https://leetcode.com/problems/paint-house-ii/
int robCircular(vector<int>& nums) {
if (nums.size() == 1) return nums[0];
auto robLinear = [](vector<int>& a, int start, int end) {
int prev = 0, curr = 0;
for (int i = start; i <= end; i++) {
int temp = max(curr, prev + a[i]);
prev = curr;
curr = temp;
}
return curr;
};
return max(robLinear(nums, 0, nums.size() - 2),
robLinear(nums, 1, nums.size() - 1));
}
// Decode Ways (String DP)
// LeetCode: 91. Decode Ways
// https://leetcode.com/problems/decode-ways/
// Related:
// 639. Decode Ways II
// https://leetcode.com/problems/decode-ways-ii/
// 70. Climbing Stairs
// https://leetcode.com/problems/climbing-stairs/
// 2266. Count Number of Texts
// https://leetcode.com/problems/count-number-of-texts/
// 1277. Count Square Submatrices with All Ones
// https://leetcode.com/problems/count-square-submatrices-with-all-ones/
int numDecodings(string s) {
int n = s.size();
if (n == 0 || s[0] == '0') return 0;
int prev2 = 1, prev1 = 1;
for (int i = 1; i < n; i++) {
int curr = 0;
if (s[i] != '0') curr += prev1;
int two = stoi(s.substr(i - 1, 2));
if (two >= 10 && two <= 26) curr += prev2;
prev2 = prev1;
prev1 = curr;
}
return prev1;
}
// Word Break (String DP with Dictionary)
// LeetCode: 139. Word Break
// https://leetcode.com/problems/word-break/
// Related:
// 140. Word Break II
// https://leetcode.com/problems/word-break-ii/
// 472. Concatenated Words
// https://leetcode.com/problems/concatenated-words/
// 1048. Longest String Chain
// https://leetcode.com/problems/longest-string-chain/
// 2707. Extra Characters in a String
// https://leetcode.com/problems/extra-characters-in-a-string/
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> dict(wordDict.begin(), wordDict.end());
vector<bool> dp(s.size() + 1);
dp[0] = true;
for (int i = 1; i <= s.size(); i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && dict.count(s.substr(j, i - j))) {
dp[i] = true;
break;
}
}
}
return dp[s.size()];
}
// Coin Change (Unbounded Knapsack DP)
// LeetCode: 322. Coin Change
// https://leetcode.com/problems/coin-change/
// Related:
// 518. Coin Change 2
// https://leetcode.com/problems/coin-change-2/
// 377. Combination Sum IV
// https://leetcode.com/problems/combination-sum-iv/
// 279. Perfect Squares
// https://leetcode.com/problems/perfect-squares/
// 983. Minimum Cost For Tickets
// https://leetcode.com/problems/minimum-cost-for-tickets/
// 1449. Form Largest Integer With Digits That Add up to Target
// https://leetcode.com/problems/form-largest-integer-with-digits-that-add-up-to-target/
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount + 1, amount + 1);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
for (int coin : coins) {
if (i >= coin) {
dp[i] = min(dp[i], dp[i - coin] + 1);
}
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
// Longest Increasing Subsequence (LIS - Binary Search Optimization)
// LeetCode: 300. Longest Increasing Subsequence
// https://leetcode.com/problems/longest-increasing-subsequence/
// Related:
// 1143. Longest Common Subsequence
// https://leetcode.com/problems/longest-common-subsequence/
// 354. Russian Doll Envelopes
// https://leetcode.com/problems/russian-doll-envelopes/
// 646. Maximum Length of Pair Chain
// https://leetcode.com/problems/maximum-length-of-pair-chain/
// 1105. Filling Bookcase Shelves
// https://leetcode.com/problems/filling-bookcase-shelves/
// 673. Number of Longest Increasing Subsequence
// https://leetcode.com/problems/number-of-longest-increasing-subsequence/
int lengthOfLIS(vector<int>& nums) {
vector<int> tails;
for (int x : nums) {
auto it = lower_bound(tails.begin(), tails.end(), x);
if (it == tails.end()) tails.push_back(x);
else *it = x;
}
return tails.size();
}
// Perfect Squares (Mathematical DP)
// LeetCode: 279. Perfect Squares
// https://leetcode.com/problems/perfect-squares/
// Related:
// 322. Coin Change
// https://leetcode.com/problems/coin-change/
// 264. Ugly Number II
// https://leetcode.com/problems/ugly-number-ii/
// 204. Count Primes
// https://leetcode.com/problems/count-primes/
// 343. Integer Break
// https://leetcode.com/problems/integer-break/
// 650. 2 Keys Keyboard
// https://leetcode.com/problems/2-keys-keyboard/
int numSquares(int n) {
vector<int> dp(n + 1, n);
dp[0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j * j <= i; j++) {
dp[i] = min(dp[i], dp[i - j * j] + 1);
}
}
return dp[n];
}
// Jump Game (Greedy/DP Approach)
// LeetCode: 55. Jump Game
// https://leetcode.com/problems/jump-game/
// Related:
// 45. Jump Game II
// https://leetcode.com/problems/jump-game-ii/
// 1306. Jump Game III
// https://leetcode.com/problems/jump-game-iii/
// 1696. Jump Game VI
// https://leetcode.com/problems/jump-game-vi/
// 1340. Jump Game V
// https://leetcode.com/problems/jump-game-v/
// 1871. Jump Game VII
// https://leetcode.com/problems/jump-game-vii/
bool canJump(vector<int>& nums) {
int reach = 0;
for (int i = 0; i < nums.size(); i++) {
if (i > reach) return false;
reach = max(reach, i + nums[i]);
}
return true;
}
// Jump Game II (Minimum Jumps - Greedy Optimization)
// LeetCode: 45. Jump Game II
// https://leetcode.com/problems/jump-game-ii/
// Related:
// 55. Jump Game
// https://leetcode.com/problems/jump-game/
// 1024. Video Stitching
// https://leetcode.com/problems/video-stitching/
// 1326. Minimum Number of Taps to Open to Water a Garden
// https://leetcode.com/problems/minimum-number-of-taps-to-open-to-water-a-garden/
// 1306. Jump Game III
// https://leetcode.com/problems/jump-game-iii/
// 1696. Jump Game VI
// https://leetcode.com/problems/jump-game-vi/
int jump(vector<int>& nums) {
int jumps = 0, curEnd = 0, curFar = 0;
for (int i = 0; i < nums.size() - 1; i++) {
curFar = max(curFar, i + nums[i]);
if (i == curEnd) {
jumps++;
curEnd = curFar;
}
}
return jumps;
}