forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
min-max-game.py
35 lines (32 loc) · 859 Bytes
/
min-max-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
# Time: O(n)
# Space: O(1)
# simulation, optimized from solution2
class Solution(object):
def minMaxGame(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
while n != 1:
new_q = []
for i in xrange(n//2):
nums[i] = min(nums[2*i], nums[2*i+1]) if i%2 == 0 else max(nums[2*i], nums[2*i+1])
n //= 2
return nums[0]
# Time: O(n)
# Space: O(n)
# simulation
class Solution2(object):
def minMaxGame(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
q = nums[:]
while len(q) != 1:
new_q = []
for i in xrange(len(q)//2):
new_q.append(min(q[2*i], q[2*i+1]) if i%2 == 0 else max(q[2*i], q[2*i+1]))
q = new_q
return q[0]