Skip to content

Commit 1e3216d

Browse files
authored
Create minimum-number-of-operations-to-convert-time.cpp
1 parent 3eff60c commit 1e3216d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
// greedy
5+
class Solution {
6+
public:
7+
int convertTime(string current, string correct) {
8+
static vector<int> OPS = {60, 15, 5, 1};
9+
int diff = (stoi(correct.substr(0, 2)) * 60 + stoi(correct.substr(3))) -
10+
(stoi(current.substr(0, 2)) * 60 + stoi(current.substr(3)));
11+
int result = 0;
12+
for (const auto& x : OPS) {
13+
const int q = diff / x;
14+
diff %= x;
15+
result += q;
16+
}
17+
return result;
18+
}
19+
};

0 commit comments

Comments
 (0)