Skip to content

Commit 9878738

Browse files
committed
Time: 26 ms (19.81%), Space: 157.3 MB (8.56%) - LeetHub
1 parent 75d0be1 commit 9878738

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
4+
int helper(int n, int last, int tot, vector<vector<int>>& dp)
5+
{
6+
if(n < 0)
7+
return 1e9;
8+
9+
if(n == 0)
10+
return 0;
11+
12+
if(dp[n][last] != -1)
13+
return dp[n][last];
14+
15+
int previousCopy = 1 + helper(n - last, last, tot, dp);
16+
17+
int alreadyTaken = tot - n;
18+
19+
int newCopy = 2 + helper(n - alreadyTaken, alreadyTaken, tot, dp);
20+
21+
return dp[n][last] = min(newCopy, previousCopy);
22+
23+
}
24+
25+
int minSteps(int n) {
26+
27+
if(n == 1)
28+
return 0;
29+
30+
vector<vector<int>> dp(n+1,vector<int>(n+1, -1));
31+
32+
return helper(n-1, 1, n, dp) + 1;
33+
34+
}
35+
};

0 commit comments

Comments
 (0)