|
# |
Title |
Difficulty |
Category |
Sub-Category |
|
70 |
Easy |
1.Linear
DP |
|
|
|
121 |
Easy |
1.Linear
DP |
|
|
|
746 |
Easy |
1.Linear
DP |
|
|
|
1025 |
Easy |
1.Linear
DP |
|
|
|
123 |
Hard |
1.Linear
DP |
|
|
|
552 |
Hard |
1.Linear
DP |
|
|
|
639 |
Hard |
1.Linear
DP |
|
|
|
982 |
Hard |
1.Linear
DP |
|
|
|
1235 |
Hard |
1.Linear
DP |
|
|
|
1326 |
Hard |
1.Linear
DP |
|
|
|
1359 |
Hard |
1.Linear
DP |
|
|
|
1406 |
Hard |
1.Linear
DP |
|
|
|
1416 |
Hard |
1.Linear
DP |
|
|
|
1449 |
Hard |
1.Linear
DP |
|
|
|
1510 |
Hard |
1.Linear
DP |
|
|
|
91 |
Medium |
1.Linear
DP |
|
|
|
96 |
Medium |
1.Linear
DP |
|
|
|
198 |
Medium |
1.Linear
DP |
|
|
|
279 |
Medium |
1.Linear
DP |
|
|
|
309 |
Medium |
1.Linear
DP |
|
|
|
322 |
Medium |
1.Linear
DP |
|
|
|
338 |
Medium |
1.Linear
DP |
|
|
|
343 |
Medium |
1.Linear
DP |
|
|
|
357 |
Medium |
1.Linear
DP |
|
|
|
376 |
Medium |
1.Linear
DP |
|
|
|
416 |
Medium |
1.Linear
DP |
|
|
|
646 |
Medium |
1.Linear
DP |
|
|
|
714 |
Medium |
1.Linear
DP |
|
|
|
740 |
Medium |
1.Linear
DP |
|
|
|
790 |
Medium |
1.Linear
DP |
|
|
|
935 |
Medium |
1.Linear
DP |
|
|
|
983 |
Medium |
1.Linear
DP |
|
|
|
1043 |
Medium |
1.Linear
DP |
|
|
|
1105 |
Medium |
1.Linear
DP |
|
|
|
1218 |
Medium |
1.Linear
DP |
|
|
|
1262 |
Medium |
1.Linear
DP |
|
|
|
879 |
Hard |
2.Knapsack |
|
|
|
956 |
Hard |
2.Knapsack |
|
|
|
1388 |
Hard |
2.Knapsack |
|
|
|
1402 |
Hard |
2.Knapsack |
|
|
|
213 |
Medium |
2.Knapsack |
|
|
|
474 |
Medium |
2.Knapsack |
|
|
|
494 |
Medium |
2.Knapsack |
|
|
|
638 |
Medium |
2.Knapsack |
|
|
|
650 |
Medium |
2.Knapsack |
|
|
|
801 |
Medium |
2.Knapsack |
|
|
|
1626 |
Medium |
2.Knapsack |
|
|
|
188 |
Hard |
3.Multi
Dimensions DP |
|
|
|
321 |
Hard |
3.Multi
Dimensions DP |
|
|
|
403 |
Hard |
3.Multi
Dimensions DP |
|
|
|
410 |
Hard |
3.Multi
Dimensions DP |
|
|
|
514 |
Hard |
3.Multi
Dimensions DP |
|
|
|
871 |
Hard |
3.Multi
Dimensions DP |
|
|
|
920 |
Hard |
3.Multi
Dimensions DP |
|
|
|
1220 |
Hard |
3.Multi
Dimensions DP |
|
|
|
1289 |
Hard |
3.Multi
Dimensions DP |
|
|
|
1320 |
Hard |
3.Multi
Dimensions DP |
|
|
|
1335 |
Hard |
3.Multi
Dimensions DP |
|
|
|
1411 |
Hard |
3.Multi
Dimensions DP |
|
|
|
1420 |
Build Array Where You Can Find The Maximum Exactly K Comparisons |
Hard |
3.Multi
Dimensions DP |
|
|
1444 |
Hard |
3.Multi
Dimensions DP |
|
|
|
1473 |
Hard |
3.Multi
Dimensions DP |
|
|
|
1575 |
Hard |
3.Multi
Dimensions DP |
|
|
|
120 |
Medium |
3.Multi
Dimensions DP |
|
|
|
377 |
Medium |
3.Multi
Dimensions DP |
|
|
|
576 |
Medium |
3.Multi
Dimensions DP |
|
|
|
688 |
Medium |
3.Multi
Dimensions DP |
|
|
|
799 |
Medium |
3.Multi
Dimensions DP |
|
|
|
813 |
Medium |
3.Multi
Dimensions DP |
|
|
|
931 |
Medium |
3.Multi
Dimensions DP |
|
|
|
1024 |
Medium |
3.Multi
Dimensions DP |
|
|
|
1027 |
Medium |
3.Multi
Dimensions DP |
|
|
|
1140 |
Medium |
3.Multi
Dimensions DP |
|
|
|
1155 |
Medium |
3.Multi
Dimensions DP |
|
|
|
1223 |
Medium |
3.Multi
Dimensions DP |
|
|
|
1621 |
Medium |
3.Multi
Dimensions DP |
|
|
|
132 |
Hard |
4.Interval
DP |
|
|
|
312 |
Hard |
4.Interval
DP |
|
|
|
546 |
Hard |
4.Interval
DP |
|
|
|
664 |
Hard |
4.Interval
DP |
|
|
|
903 |
Hard |
4.Interval
DP |
|
|
|
1000 |
Hard |
4.Interval
DP |
|
|
|
1478 |
Hard |
4.Interval
DP |
|
|
|
1547 |
Hard |
4.Interval
DP |
|
|
|
1563 |
Hard |
4.Interval
DP |
|
|
|
375 |
Medium |
4.Interval
DP |
|
|
|
413 |
Medium |
4.Interval
DP |
|
|
|
486 |
Medium |
4.Interval
DP |
|
|
|
647 |
Medium |
4.Interval
DP |
|
|
|
877 |
Medium |
4.Interval
DP |
|
|
|
1039 |
Medium |
4.Interval
DP |
|
|
|
1049 |
Medium |
4.Interval
DP |
|
|
|
1130 |
Medium |
4.Interval
DP |
|
|
|
1690 |
Medium |
4.Interval
DP |
|
|
|
691 |
Hard |
5.bit DP |
|
|
|
847 |
Hard |
5.bit DP |
|
|
|
1125 |
Hard |
5.bit DP |
|
|
|
1349 |
Hard |
5.bit DP |
|
|
|
1434 |
Hard |
5.bit DP |
|
|
|
1595 |
Hard |
5.bit DP |
|
|
|
1601 |
Hard |
5.bit DP |
|
|
|
1655 |
Hard |
5.bit DP |
|
|
|
1659 |
Hard |
5.bit DP |
|
|
|
464 |
Medium |
5.bit DP |
|
|
|
698 |
Medium |
5.bit DP |
|
|
|
600 |
Hard |
6.Digit DP |
|
|
|
902 |
Hard |
6.Digit DP |
|
|
|
1012 |
Hard |
6.Digit DP |
|
|
|
968 |
Hard |
7.DP on
Trees |
|
|
|
1373 |
Hard |
7.DP on
Trees |
|
|
|
1569 |
Hard |
7.DP on
Trees |
|
|
|
95 |
Medium |
7.DP on
Trees |
|
|
|
337 |
Medium |
7.DP on
Trees |
|
|
|
1339 |
Medium |
7.DP on
Trees |
|
|
|
1367 |
Medium |
7.DP on
Trees |
|
|
|
1372 |
Medium |
7.DP on
Trees |
|
|
|
392 |
Easy |
8.String
DP |
|
|
|
32 |
Hard |
8.String
DP |
|
|
|
115 |
Hard |
8.String
DP |
|
|
|
140 |
Hard |
8.String
DP |
|
|
|
466 |
Hard |
8.String
DP |
|
|
|
472 |
Hard |
8.String
DP |
|
|
|
730 |
Hard |
8.String
DP |
|
|
|
940 |
Hard |
8.String
DP |
|
|
|
1147 |
Hard |
8.String
DP |
|
|
|
1278 |
Hard |
8.String
DP |
|
|
|
1397 |
Hard |
8.String
DP |
|
|
|
1531 |
Hard |
8.String
DP |
|
|
|
1639 |
Hard |
8.String
DP |
|
|
|
131 |
Medium |
8.String
DP |
|
|
|
139 |
Medium |
8.String
DP |
|
|
|
467 |
Medium |
8.String
DP |
|
|
|
712 |
Medium |
8.String
DP |
|
|
|
1048 |
Medium |
8.String
DP |
|
|
|
1405 |
Medium |
8.String
DP |
|
|
|
808 |
Medium |
9.Probability
DP |
|
|
|
837 |
Medium |
9.Probability
DP |
|
|
|
1227 |
Medium |
9.Probability
DP |
|
|
|
53 |
Easy |
10.Classic
DP |
Cadane's Algorithm |
|
|
152 |
Medium |
10.Classic
DP |
Cadane's Algorithm |
|
|
898 |
Medium |
10.Classic
DP |
Cadane's Algorithm |
|
|
978 |
Medium |
10.Classic
DP |
Cadane's Algorithm |
|
|
1186 |
Medium |
10.Classic
DP |
Cadane's Algorithm |
|
|
1191 |
Medium |
10.Classic
DP |
Cadane's Algorithm |
|
|
368 |
Medium |
10.Classic
DP |
Cadane's Algorithm |
|
|
873 |
Medium |
10.Classic
DP |
Cadane's Algorithm |
|
|
10 |
Hard |
10.Classic
DP |
LCS |
|
|
44 |
Hard |
10.Classic
DP |
LCS |
|
|
72 |
Hard |
10.Classic
DP |
LCS |
|
|
97 |
Hard |
10.Classic
DP |
LCS |
|
|
1092 |
Hard |
10.Classic
DP |
LCS |
|
|
1312 |
Hard |
10.Classic
DP |
LCS |
|
|
1458 |
Hard |
10.Classic
DP |
LCS |
|
|
5 |
Medium |
10.Classic
DP |
LCS |
|
|
516 |
Medium |
10.Classic
DP |
LCS |
|
|
718 |
Medium |
10.Classic
DP |
LCS |
|
|
1143 |
Medium |
10.Classic
DP |
LCS |
|
|
354 |
Hard |
10.Classic
DP |
LIS |
|
|
960 |
Hard |
10.Classic
DP |
LIS |
|
|
1187 |
Hard |
10.Classic
DP |
LIS |
|
|
1671 |
Hard |
10.Classic
DP |
LIS |
|
|
1691 |
Hard |
10.Classic
DP |
LIS |
|
|
300 |
Medium |
10.Classic
DP |
LIS |
|
|
673 |
Medium |
10.Classic
DP |
LIS |
|
|
174 |
Hard |
10.Classic
DP |
2D Grid Traversal |
|
|
741 |
Hard |
10.Classic
DP |
2D Grid Traversal |
|
|
1301 |
Hard |
10.Classic
DP |
2D Grid Traversal |
|
|
1463 |
Hard |
10.Classic
DP |
2D Grid Traversal |
|
|
1643 |
Hard |
10.Classic
DP |
2D Grid Traversal |
|
|
62 |
Medium |
10.Classic
DP |
2D Grid Traversal |
|
|
63 |
Medium |
10.Classic
DP |
2D Grid Traversal |
|
|
64 |
Medium |
10.Classic
DP |
2D Grid Traversal |
|
|
1594 |
Medium |
10.Classic
DP |
2D Grid Traversal |
|
|
1706 |
Medium |
10.Classic
DP |
2D Grid Traversal |
|
|
303 |
Easy |
10.Classic
DP |
Cumulative Sum |
|
|
85 |
Hard |
10.Classic
DP |
Cumulative Sum |
|
|
363 |
Hard |
10.Classic
DP |
Cumulative Sum |
|
|
517 |
Hard |
10.Classic
DP |
Cumulative Sum |
|
|
689 |
Hard |
10.Classic
DP |
Cumulative Sum |
|
|
1074 |
Hard |
10.Classic
DP |
Cumulative Sum |
|
|
1537 |
Hard |
10.Classic
DP |
Cumulative Sum |
|
|
221 |
Medium |
10.Classic
DP |
Cumulative Sum |
|
|
304 |
Medium |
10.Classic
DP |
Cumulative Sum |
|
|
764 |
Medium |
10.Classic
DP |
Cumulative Sum |
|
|
838 |
Medium |
10.Classic
DP |
Cumulative Sum |
|
|
1139 |
Medium |
10.Classic
DP |
Cumulative Sum |
|
|
1277 |
Medium |
10.Classic
DP |
Cumulative Sum |
|
|
1314 |
Medium |
10.Classic
DP |
Cumulative Sum |
|
|
1423 |
Medium |
10.Classic
DP |
Cumulative Sum |
|
|
1504 |
Medium |
10.Classic
DP |
Cumulative Sum |
|
|
1664 |
Medium |
10.Classic
DP |
Cumulative Sum |
|
|
523 |
Medium |
10.Classic
DP |
Hashmap (SubArray) |
|
|
1477 |
Medium |
10.Classic
DP |
Hashmap (SubArray) |
|
|
1546 |
Maximum Number of
Non-Overlapping Subarrays With Sum Equals
Target |
Medium |
10.Classic
DP |
Hashmap (SubArray) |
|
446 |
Hard |
11. DP +
Alpha (Tricks/DS) |
|
|
|
975 |
Hard |
11. DP +
Alpha (Tricks/DS) |
|
|
|
1425 |
Hard |
11. DP +
Alpha (Tricks/DS) |
|
|
|
1687 |
Hard |
11. DP +
Alpha (Tricks/DS) |
|
|
|
629 |
Hard |
12.Insertion
DP |
|
|
|
943 |
Hard |
13.Graph
DP |
|
|
|
787 |
Medium |
13.Graph
DP |
|
|
|
87 |
Hard |
14.Memoization |
|
|
|
1240 |
Hard |
14.Memoization |
|
|
|
1269 |
Hard |
14.Memoization |
|
|
|
1340 |
Hard |
14.Memoization |
|
|
|
1553 |
Hard |
14.Memoization |
|
|
|
1654 |
Medium |
14.Memoization |
|
|
|
1483 |
Hard |
15. Binary
Lifting |
|
|
|
818 |
Hard |
16. Math |
|
|
|
887 |
Hard |
16. Math |
|
|
|
964 |
Hard |
16. Math |
|
|
|
1363 |
Hard |
16. Math |
|
|
|
1611 |
Hard |
16. Math |
|
|
|
264 |
Medium |
16. Math |
|
|
|
1641 |
Medium |
16. Math |
|
Sunday, January 3, 2021
All Public Dynamic Programming (DP) Problems at LeetCode
Saturday, January 2, 2021
POJ.1741 Tree
1.Problem
1741 -- Tree (poj.org)
2.Idea
Centroid Decomposition of Tree
3.Source
struct edge {
int to, length;
edge(int x, int y) { to = x; length = y; }
};
int n, k;
vector<edge> G[10004];
bool centroid[10004];
int subtree_size[10004];
int ans;
void init(int n) {
REP(i, n) G[i].clear();
}
void add_edge(int a, int b, int c) {
G[a].push_back(edge( b, c ));
G[b].push_back(edge( a, c ));
}
int compute_subtree(int v, int p)
{
int c = 1;
for (int i = 0; i < G[v].size(); i++) {
int w = G[v][i].to;
if (w == p || centroid[w]) continue;
c += compute_subtree(G[v][i].to, v);
}
subtree_size[v] = c;
return c;
}
pair<int, int> search_centroid(int v, int p, int t)
{
pair<int, int> res = make_pair(INT_MAX, -1);
int s = 1, m = 0;
for (int i = 0; i < G[v].size(); i++) {
int w = G[v][i].to;
if (w == p || centroid[w]) continue;
res = min(res, search_centroid(w, v, t));
m = max(m, subtree_size[w]);
s += subtree_size[w];
}
m = max(m, t - s);
res = min(res, make_pair(m, v));
return res;
}
void enumarate_paths(int v, int p, int d, vector<int> &ds)
{
ds.push_back(d);
for (int i = 0; i < G[v].size(); i++) {
int w = G[v][i].to;
if (w == p || centroid[w]) continue;
enumarate_paths(w, v, d + G[v][i].length, ds);
}
}
int count_pairs(vector<int> &ds)
{
int res = 0;
sort(ds.begin(), ds.end());
int j = ds.size();
for (int i = 0; i < ds.size(); i++) {
while (j > 0 && ds[i] + ds[j - 1] > k) --j;
res += j - (j > i ? 1 : 0);
}
return res / 2;
}
void solve_subproblem(int v)
{
compute_subtree(v, -1);
int s = search_centroid(v, -1, subtree_size[v]).second;
centroid[s] = true;
for (int i = 0; i < G[s].size(); i++) {
if (centroid[G[s][i].to]) continue;
solve_subproblem(G[s][i].to);
}
vector<int> ds;
ds.push_back(0);
for (int i = 0; i < G[s].size(); i++) {
if (centroid[G[s][i].to]) continue;
vector<int> tds;
enumarate_paths(G[s][i].to, s, G[s][i].length, tds);
ans -= count_pairs(tds);
ds.insert(ds.end(), tds.begin(), tds.end());
}
ans += count_pairs(ds);
centroid[s] = false;
}
void solve()
{
while (scanf("%d %d", &n, &k), n) {
init(n);
REP(i, n - 1) {
int u, w, l;
scanf("%d %d %d", &u, &w, &l);
u--; w--;
add_edge(u, w, l);
}
ans = 0;
solve_subproblem(0);
printf("%d\n", ans);
}
}
Friday, January 1, 2021
POJ.3260 The Fewest Coins
1.Problem
3260 -- The Fewest Coins (poj.org)
2.Idea
Knapsack problem. Reference
3.Source
#include <cstdio>
#include <cstring>
const int maxn = 101, maxt = 10001, maxv = 121, INF = 0x3f3f3f3f;
int n, t, lim, v[maxn], c[maxn], que[maxt + maxv * maxv][2], l, r, f[maxt + maxv * maxv], g[maxv * maxv], ans;
int main()
{
scanf("%d%d", &n, &t);
for(int i = 0; i < n; ++i)
{
scanf("%d", v + i);
if(lim < v[i])
lim = v[i];
}
for(int i = 0; i < n; ++i)
scanf("%d", c + i);
lim *= lim;
t += lim;
memset(f, 0x3f, sizeof f);
memset(g, 0x3f, sizeof g);
f[0] = g[0] = 0;
for(int i = 0; i < n; ++i)
for(int a = 0; a < v[i]; ++a)
{
l = r = 0;
for(int j = 0; j * v[i] + a <= t; ++j)
{
int tmp = f[j * v[i] + a] - j;
while(l < r && que[r - 1][1] >= tmp)
--r;
que[r][0] = j;
que[r++][1] = tmp;
f[j * v[i] + a] = que[l][1] + j;
if(que[l][0] == j - c[i])
++l;
}
}
for(int i = 0; i < n; ++i)
for(int j = v[i]; j <= lim; ++j)
if(g[j] > g[j - v[i]] + 1)
g[j] = g[j - v[i]] + 1;
t -= lim;
ans = INF;
for(int i = 0; i <= lim; ++i)
if(ans > f[t + i] + g[i])
ans = f[t + i] + g[i];
if(ans != INF)
printf("%d\n", ans);
else
puts("-1");
return 0;
}
POJ.2823 Sliding Window
1.Problem
2823 -- Sliding Window (poj.org)
2.Idea
Using Deque
3.Source
const int N = 1000006;
//////////////////////////////
int n, k;
int a[N];
int mn[N], mx[N];
int deq[N];
void solve()
{
scanf("%d%d", &n, &k);
REP(i, n) scanf("%d", &a[i]);
//MIN values
int s = 0, t = 0;
REP(i, n) {
while (s < t && a[deq[t - 1]] >= a[i]) t--;
deq[t++] = i;
if (i - k + 1 >= 0) {
mn[i - k + 1] = a[deq[s]];
if (deq[s] == i - k + 1) s++;
}
}
REP(i, n - k + 1) {
printf("%d%c", mn[i], i == n - k ? '\n' : ' ');
}
//MAX Values
s = 0, t = 0;
REP(i, n) {
while (s < t && a[deq[t - 1]] <= a[i]) t--;
deq[t++] = i;
if (i - k + 1 >= 0) {
mx[i - k + 1] = a[deq[s]];
if (deq[s] == i - k + 1) s++;
}
}
REP(i, n - k + 1) {
printf("%d%c", mx[i], i == n - k ? '\n' : ' ');
}
}
Wednesday, December 30, 2020
POJ.3709 K-Anonymous Sequence
1.Problem
3709 -- K-Anonymous Sequence (poj.org)
2.Idea
Deque
3.Source
int n, k;
Int a[N];
Int dp[N], S[N];
int deq[N];
Int f(int j, int x)
{
return -a[j] * x + dp[j] - S[j] + a[j] * j;
}
bool check(int f1, int f2, int f3)
{
Int a1 = -a[f1], b1 = dp[f1] - S[f1] + a[f1] * f1;
Int a2 = -a[f2], b2 = dp[f2] - S[f2] + a[f2] * f2;
Int a3 = -a[f3], b3 = dp[f3] - S[f3] + a[f3] * f3;
return (a2 - a1) * (b3 - b2) >= (b2 - b1) * (a3 - a2);
}
void solve()
{
int t; cin >> t;
while (t--) {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
S[i + 1] = S[i] + a[i];
}
int s = 0, t = 1;
deq[0] = 0;
dp[0] = 0;
for (int i = k; i <= n; i++) {
if (i - k >= k) {
while (s + 1 < t && check(deq[t - 2], deq[t - 1], i - k)) t--;
deq[t++] = i - k;
}
while (s + 1 < t && f(deq[s], i) >= f(deq[s + 1], i)) s++;
dp[i] = S[i] + f(deq[s], i);
}
printf("%lld\n", dp[n]);
}
}
Tuesday, December 29, 2020
LeetCode.472 Concatenated Words
1.Problem
Concatenated Words - LeetCode
2.Idea
Trie + DFS
3.Source
class TrieNode {
public:
bool isKey = false;
TrieNode* child[26];
};
class Solution {
public:
TrieNode* root = new TrieNode();
void addWord(string word) {
TrieNode* curr = root;
for(char c : word) {
if(!curr->child[c - 'a'])
curr->child[c - 'a'] = new TrieNode();
curr = curr->child[c - 'a'];
}
curr->isKey = true;
}
bool search(string word, int index, int count) {
if(index >= word.size()) return count >= 2;
TrieNode* curr = root;
for(int i= index; i < word.size(); i++) {
char c = word[i];
if(!curr->child[c - 'a'])
return false;
curr = curr->child[c - 'a'];
if(curr->isKey) {
if(search(word, i+1, count + 1))
return true;
}
}
return false;
}
vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
vector<string> res;
if(words.size() == 0) return res;
for(string s : words)
addWord(s);
for(string s : words) {
if(search(s, 0, 0))
res.push_back(s);
}
return res;
}
};
POJ.2082 Terrible Sets
1.Problem
2082 -- Terrible Sets (poj.org)
2.Idea
Description is terrible but it was just simple max rectangle in histogram problem.
3.Source
int n;
int h[N];
int st[N], L[N], R[N], sum[N];
int maxRec() {
//L
int t = 0;
for (int i = 0; i < n; i++) {
while (t > 0 && h[st[t - 1]] >= h[i]) t--;
L[i] = (t == 0 ? 0 : (st[t - 1] + 1));
st[t++] = i;
}
//R
t = 0;
for (int i = n - 1; i >= 0; i--) {
while (t > 0 && h[st[t - 1]] >= h[i]) t--;
R[i] = (t == 0 ? n : st[t - 1]);
st[t++] = i;
}
int res = 0;
for (int i = 0; i < n; i++) {
res = max(res, h[i] * (sum[R[i]] - sum[L[i]]));
}
return res;
}
void solve()
{
while (cin >> n) {
if (n < 0) break;
for (int i = 0; i < n; i++) {
scanf("%d%d", &sum[i + 1], &h[i]);
}
for (int i = 0; i < n; i++)
sum[i + 1] += sum[i];
printf("%d\n", maxRec());
}
}
Subscribe to:
Posts (Atom)