-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
39 lines (33 loc) · 989 Bytes
/
Copy pathsolution.cpp
File metadata and controls
39 lines (33 loc) · 989 Bytes
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
class Solution
{
public:
int optimalKeys(int n)
{
// dp[i] stores the maximum number of A's
// possible using exactly i key presses
vector<int> dp(n + 1, 0);
// Base case:
// For small values, simply press 'A'
for (int i = 1; i <= n; i++)
{
dp[i] = i;
}
// Try all lengths from 1 to n
for (int i = 1; i <= n; i++)
{
// j represents the point where
// we stop typing and start copy-paste operations
for (int j = 1; j <= i - 3; j++)
{
// After j presses:
// Ctrl+A -> 1 press
// Ctrl+C -> 1 press
// Remaining are Ctrl+V operations
// Total multiplication becomes (i - j - 1)
dp[i] = max(dp[i], dp[j] * (i - j - 1));
}
}
// Final answer
return dp[n];
}
};