forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirror-reflection.py
36 lines (31 loc) · 970 Bytes
/
mirror-reflection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Time: O(1)
# Space: O(1)
class Solution(object):
def mirrorReflection(self, p, q):
"""
:type p: int
:type q: int
:rtype: int
"""
# explanation commented in the following solution
return 2 if (p & -p) > (q & -q) else 0 if (p & -p) < (q & -q) else 1
# Time: O(log(max(p, q))) = O(1) due to 32-bit integer
# Space: O(1)
class Solution2(object):
def mirrorReflection(self, p, q):
"""
:type p: int
:type q: int
:rtype: int
"""
def gcd(a, b):
while b:
a, b = b, a % b
return a
lcm = p*q // gcd(p, q)
# let a = lcm / p, b = lcm / q
if lcm // p % 2 == 1:
if lcm // q % 2 == 1:
return 1 # a is odd, b is odd <=> (p & -p) == (q & -q)
return 2 # a is odd, b is even <=> (p & -p) > (q & -q)
return 0 # a is even, b is odd <=> (p & -p) < (q & -q)