Skip to content

Commit 44e9616

Browse files
committed
feat(LeetCode): Solve leetcode/leetcoding_challenge/2024/aug2024/week3/2_keys_keyboard.cpp
1 parent af30cc7 commit 44e9616

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <algorithm>
2+
#include <vector>
3+
#include <queue>
4+
#include <set>
5+
#include <limits>
6+
#include <map>
7+
#include <unordered_set>
8+
#include <unordered_map>
9+
#include <iterator>
10+
#include <sstream>
11+
#include <iostream> // includes cin to read from stdin and cout to write to stdout
12+
using namespace std; // since cin and cout are both in namespace std, this saves some text
13+
14+
int UNINITIALIZED = numeric_limits<int>::max();
15+
class Solution {
16+
public:
17+
int minSteps(int n) {
18+
memo = vector<vector<long long>>(n + 1, vector<long long>(n + 1, UNINITIALIZED));
19+
return findMin(n, 1, 0);
20+
}
21+
22+
private:
23+
vector<vector<long long>> memo;
24+
25+
int findMin(int n, int size, int copySize) {
26+
if (size > n || copySize > n) {
27+
return numeric_limits<int>::max();
28+
}
29+
if (size == n) {
30+
return 0;
31+
}
32+
33+
if (memo[size][copySize] == UNINITIALIZED) {
34+
if (size > copySize) {
35+
memo[size][copySize] = min(
36+
memo[size][copySize],
37+
1 + (long long) findMin(n, size, size)
38+
); // copy
39+
}
40+
41+
if (copySize > 0) {
42+
memo[size][copySize] = min(
43+
memo[size][copySize],
44+
1 + (long long) findMin(n, size + copySize, copySize)
45+
); // paste
46+
}
47+
}
48+
return memo[size][copySize];
49+
}
50+
};

0 commit comments

Comments
 (0)