-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstruct-The-Longest-New-String.cpp
More file actions
52 lines (48 loc) · 1.13 KB
/
Copy pathConstruct-The-Longest-New-String.cpp
File metadata and controls
52 lines (48 loc) · 1.13 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
// Leetcode 2745
class Solution
{
public:
int dp[51][51][51][4] = {0};
int solve(vector<string> &words, int x, int y, int z, int prev)
{
// dp check
if (dp[x][y][z][prev] != 0)
return dp[x][y][z][prev];
// adding one x
int xx = 0;
if (prev == 0 || prev != 1)
{
if (x > 0)
{
xx = 2 + solve(words, x - 1, y, z, 1);
}
}
// adding one y
int yy = 0;
if (prev == 0 || (prev != 2 && prev != 3))
{
if (y > 0)
{
yy = 2 + solve(words, x, y - 1, z, 2);
}
}
// adding one z
int zz = 0;
if (prev == 0 || prev != 1)
{
if (z > 0)
{
zz = 2 + solve(words, x, y, z - 1, 3);
}
}
return dp[x][y][z][prev] = max({xx, yy, zz});
}
int longestString(int x, int y, int z)
{
vector<string> words;
words.push_back("AA");
words.push_back("BB");
words.push_back("AB");
return solve(words, x, y, z, 0);
}
};