Skip to content

Commit 2b53594

Browse files
authored
Create broken-calculator.cpp
1 parent 3a48b4b commit 2b53594

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

C++/broken-calculator.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time: O(logn)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int brokenCalc(int X, int Y) {
7+
int result = 0;
8+
while (X < Y) {
9+
if (Y % 2) {
10+
++Y;
11+
} else {
12+
Y /= 2;
13+
}
14+
++result;
15+
}
16+
return result + X - Y;
17+
}
18+
};

0 commit comments

Comments
 (0)