-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathjump-game-ix.py
More file actions
44 lines (41 loc) · 1.14 KB
/
jump-game-ix.py
File metadata and controls
44 lines (41 loc) · 1.14 KB
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
# Time: O(n)
# Space: O(1)
# graph, prefix sum
class Solution(object):
def maxValue(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
result = [0]*len(nums)
result[0] = nums[0]
for i in xrange(len(nums)-1):
result[i+1] = max(result[i], nums[i+1])
mn = float("inf")
for i in reversed(xrange(len(nums))):
if result[i] > mn:
result[i] = result[i+1]
mn = min(mn, nums[i])
return result
# Time: O(n)
# Space: O(n)
# graph, prefix sum
class Solution2(object):
def maxValue(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
suffix = [float("inf")]*(len(nums)+1)
for i in reversed(xrange(len(nums))):
suffix[i] = min(suffix[i+1], nums[i])
result = [0]*len(nums)
mx = left = 0
for right in xrange(len(nums)):
mx = max(mx, nums[right])
if mx > suffix[right+1]:
continue
while left <= right:
result[left] = mx
left += 1
return result