Skip to content

Commit a64b542

Browse files
committed
Add 991
1 parent fb36ebd commit a64b542

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@
234234
| 983 | Minimum Cost For Tickets | Medium | O(365) | O(365) | LRU Cache, Dynamic Programming | | |
235235
| 986 | Interval List Intersections | Medium | O(N + M) | O(1) | Two Pointers | | |
236236
| 987 | Vertical Order Traversal of a Binary Tree | Medium | O(NlogN) | O(N) | Hash Table, Tree | | |
237+
| 991 | Broken Calculator | Medium | O(logN) | O(1) | Math, Greedy | | |
237238
| 993 | Cousins in Binary Tree | Easy | O(N) | O(H) | Trees, Recursion | | |
238239
| 994 | Rotting Oranges | Medium | O(N^2) | O(N) | Trees, Recursion | | |
239240
| 1004 | Max Consecutive Ones III | Medium | O(N) | O(1) | Sliding Window, Two Pointers | | |

src/991.broken-calculator.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# @lc app=leetcode id=991 lang=python3
3+
#
4+
# [991] Broken Calculator
5+
#
6+
7+
# @lc code=start
8+
# TAGS: Math, Greedy
9+
class Solution:
10+
# 20 ms, 99%. Time: O(logN). Space: O(1).
11+
def brokenCalc(self, X: int, Y: int) -> int:
12+
if X >= Y: return X - Y
13+
14+
cnt = 0
15+
while X < Y:
16+
X *= 2
17+
cnt += 1
18+
19+
diff = X - Y
20+
for i in range(cnt, -1, -1):
21+
d, diff = divmod(diff, 2**i)
22+
cnt += d
23+
return cnt
24+
25+
26+
27+
# @lc code=end
28+

testcases/991

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2
2+
3
3+
1
4+
33
5+
5
6+
8
7+
3
8+
10
9+
1024
10+
1
11+
1
12+
1000000
13+
1
14+
1
15+
1
16+
2
17+
1024
18+
1025
19+
100000000
20+
1000000000
21+
1
22+
64

0 commit comments

Comments
 (0)