-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path754.py
27 lines (27 loc) · 985 Bytes
/
754.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
__________________________________________________________________________________________________
sample 24 ms submission
class Solution:
def reachNumber(self, target: int) -> int:
# (n+1)*n/2....t
t=abs(target)
n=math.floor((t*2)**0.5)
while True:
diff=(n+1)*n/2-t
if diff>=0:
if diff%2==0:
return int(n)
n+=1
__________________________________________________________________________________________________
sample 13072 kb submission
class Solution:
def reachNumber(self, target: int) -> int:
target = abs(target)
n = math.sqrt(target * 2 + 1 / 4) - 1 / 2
if n == int(n):
return int(n)
else:
k = int(n) + 1
while (int((k + k * k) / 2) - target) % 2 != 0:
k = k + 1
return k
__________________________________________________________________________________________________