-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmaximum-walls-destroyed-by-robots.py
More file actions
35 lines (34 loc) · 1.35 KB
/
maximum-walls-destroyed-by-robots.py
File metadata and controls
35 lines (34 loc) · 1.35 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
# Time: O(nlogn + mlogm)
# Space: O(n)
# sort, dp, two pointers
class Solution(object):
def maxWalls(self, robots, distance, walls):
"""
:type robots: List[int]
:type distance: List[int]
:type walls: List[int]
:rtype: int
"""
x_d = [(0, 0)]+sorted(zip(robots, distance), key=lambda x: x[0])+[(float("inf"), 0)]
walls.sort()
left0 = left1 = right = curr = 0
dp, new_dp = [0]*2, [0]*2
for i in xrange(1, len(x_d)-1):
while curr < len(walls) and walls[curr] < x_d[i][0]:
curr += 1
r = min(x_d[i][0]+x_d[i][1], x_d[i+1][0]-1)
while right < len(walls) and walls[right] <= r:
right += 1
new_dp[1] = max(dp[0], dp[1])+(right-curr)
if curr < len(walls) and walls[curr] == x_d[i][0]:
curr += 1
l0 = max(x_d[i][0]-x_d[i][1], x_d[i-1][0]+1)
while left0 < len(walls) and walls[left0] < l0:
left0 += 1
l1 = max(min(x_d[i-1][0]+x_d[i-1][1], x_d[i][0]-1)+1,
max(x_d[i][0]-x_d[i][1], x_d[i-1][0]+1))
while left1 < len(walls) and walls[left1] < l1:
left1 += 1
new_dp[0] = max(dp[0]+(curr-left0), dp[1]+(curr-left1))
dp, new_dp = new_dp, dp
return max(dp[0], dp[1])