File tree 3 files changed +51
-0
lines changed
3 files changed +51
-0
lines changed Original file line number Diff line number Diff line change 234
234
| 983 | Minimum Cost For Tickets | Medium | O(365) | O(365) | LRU Cache, Dynamic Programming | | |
235
235
| 986 | Interval List Intersections | Medium | O(N + M) | O(1) | Two Pointers | | |
236
236
| 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 | | |
237
238
| 993 | Cousins in Binary Tree | Easy | O(N) | O(H) | Trees, Recursion | | |
238
239
| 994 | Rotting Oranges | Medium | O(N^2) | O(N) | Trees, Recursion | | |
239
240
| 1004 | Max Consecutive Ones III | Medium | O(N) | O(1) | Sliding Window, Two Pointers | | |
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments