-
Notifications
You must be signed in to change notification settings - Fork 43
/
elimination-game.py
61 lines (55 loc) · 1.29 KB
/
elimination-game.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# V1
# class Solution(object):
# def lastRemaining(self, n):
# mylist = [ i for i in range(1,n+1)]
# for i in range(n):
# print ('i :', i)
# print ('mylist :', mylist)
# if len(mylist) <= 2:
# if len(mylist) <= 1:
# return mylist[0]
# if i%2==0:
# return mylist[1]
# else:
# return mylist[0]
# return mylist[0]
# elif n%2 == 0:
# mylist = mylist[1::2]
# else:
# mylist = mylist[::-1][1::2][::-1]
# return mylist
# V2
# http://bookshadow.com/weblog/2016/08/28/leetcode-elimination-game/
class Solution(object):
def lastRemaining(self, n):
"""
:type n: int
:rtype: int
"""
a = p = 1
cnt = 0
while n > 1:
n /= 2
cnt += 1
p *= 2
if cnt % 2:
a += p / 2 + p * (n - 1)
else:
a -= p / 2 + p * (n - 1)
return a
# V3
# Time: O(logn)
# Space: O(1)
class Solution(object):
def lastRemaining(self, n):
"""
:type n: int
:rtype: int
"""
start, step, direction = 1, 2, 1
while n > 1:
start += direction * (step * (n/2) - step/2)
n /= 2
step *= 2
direction *= -1
return start