Skip to content

Commit d89413b

Browse files
authored
Create 2-keys-keyboard.cpp
1 parent e147f9e commit d89413b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

C++/2-keys-keyboard.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time: O(sqrt(n))
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int minSteps(int n) {
7+
auto result = 0;
8+
// the answer is the sum of prime factors
9+
for (auto p = 2 ; p * p <= n; ++p) {
10+
while (n % p == 0) {
11+
result += p;
12+
n /= p;
13+
}
14+
}
15+
result += (n > 1) ? n : 0;
16+
return result;
17+
}
18+
};

0 commit comments

Comments
 (0)