Skip to content

Commit 4f0ffaa

Browse files
authored
Create minimum-operations-to-make-a-special-number.cpp
1 parent 2397adb commit 4f0ffaa

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(n)
2+
// Space; O(1)
3+
4+
// math, greedy
5+
class Solution {
6+
public:
7+
int minimumOperations(string num) {
8+
static const string target1 = "05";
9+
static const string target2 = "27";
10+
11+
vector<int> lookup(10);
12+
for (int i = size(num) - 1; i >= 0; --i) {
13+
if ((target1.find(num[i]) != string::npos && lookup[0]) ||
14+
(target2.find(num[i]) != string::npos && lookup[5])) {
15+
return (size(num) - i) - 2;
16+
}
17+
lookup[num[i] - '0'] = 1;
18+
}
19+
return size(num) - lookup[0];
20+
}
21+
};

0 commit comments

Comments
 (0)