-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathzero-array-transformation-iv.py
51 lines (45 loc) · 1.62 KB
/
zero-array-transformation-iv.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
# Time: O(n^2 * r * logq), r = max(nums)
# Space: O(r)
# binary search, dp
class Solution(object):
def minZeroArray(self, nums, queries):
"""
:type nums: List[int]
:type queries: List[List[int]]
:rtype: int
"""
def binary_search(left, right, check):
while left <= right:
mid = left + (right-left)//2
if check(mid):
right = mid-1
else:
left = mid+1
return left
def check(l):
def valid(arr, target):
dp = [False]*(target+1)
dp[0] = 1
for i in xrange(len(arr)):
dp = [dp[j] or (j-arr[i] >= 0 and dp[j-arr[i]]) for j in xrange(target+1)]
return dp[target]
return all(valid([queries[j][2] for j in xrange(l) if queries[j][0] <= i <= queries[j][1]], nums[i]) for i in xrange(len(nums)))
result = binary_search(0, len(queries), check)
return result if result <= len(queries) else -1
# Time: O(q * n * 2^n)
# Space: O(n * 2^n)
# dp
class Solution2(object):
def minZeroArray(self, nums, queries):
"""
:type nums: List[int]
:type queries: List[List[int]]
:rtype: int
"""
dp = [{0} for _ in xrange(len(nums))]
for i, (l, r, v) in enumerate(queries):
if all(nums[i] in dp[i] for i in xrange(len(dp))):
return i
for j in xrange(l, r+1):
dp[j] |= set(x+v for x in dp[j])
return len(queries) if all(nums[i] in dp[i] for i in xrange(len(dp))) else -1