Skip to content

Commit 0ab1946

Browse files
committed
Add 858
1 parent 3dfb3f3 commit 0ab1946

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

Diff for: Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
| 849 | Maximize Distance to Closest Person | Medium | O(N) | O(1) | Array | | |
161161
| 851 | Loud and Rich | Medium | O(N^2) | O(N^2) | DFS | | |
162162
| 853 | Car Fleet | Medium | O(NlogN) | O(N) | Sort | | |
163+
| 858 | Mirror Reflection | Medium | O(logN) | O(1) | Math | | |
163164
| 859 | Buddy Strings | Easy | O(N) | O(1) | String | | |
164165
| 875 | Koko Eating Bananas | Medium | O(NlogN) | O(1) | Binary Search | | |
165166
| 886 | Possible Bipartition | Medium | O(N + E) | O(N + E) | Depth First Search | Good problem | |

Diff for: src/858.mirror-reflection.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#
2+
# @lc app=leetcode id=858 lang=python3
3+
#
4+
# [858] Mirror Reflection
5+
#
6+
7+
# @lc code=start
8+
# TAGS: Math
9+
# REVIEWME: Math
10+
import fractions
11+
class Solution:
12+
# 24 ms, 91.58%. Time: O(logN). Space: O(1)
13+
def mirrorReflection(self, p: int, q: int) -> int:
14+
if q == 0: return 0
15+
val = 0
16+
while val == 0 or val % p:
17+
val += q
18+
19+
if (val // p) % 2 == 0: return 0
20+
if (val // q) % 2 : return 1
21+
return 2
22+
23+
# 24 ms, 91.58%. using fraction. Simplified version of above.
24+
def mirrorReflection(self, p: int, q: int) -> int:
25+
num = fractions.Fraction(q, p)
26+
val = num.numerator * num.denominator
27+
if (val // num.denominator) % 2 == 0: return 0
28+
if (val // num.numerator) % 2 : return 1
29+
return 2
30+
31+
# 24 ms, 91.58%. Use gcd. Simplified version of above.
32+
def mirrorReflection(self, p: int, q: int) -> int:
33+
gcd = math.gcd(p, q)
34+
p //= gcd
35+
q //= gcd
36+
if q % 2 == 0: return 0
37+
if p % 2 : return 1
38+
return 2
39+
# @lc code=end
40+

Diff for: testcases/858

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2
2+
1
3+
3
4+
1
5+
4
6+
1
7+
4
8+
3
9+
7
10+
3
11+
3
12+
2
13+
6
14+
3
15+
9
16+
1
17+
13
18+
11
19+
1
20+
0
21+
4
22+
4
23+
4
24+
2
25+
6
26+
2

0 commit comments

Comments
 (0)