Skip to content

Commit a91211e

Browse files
authored
Create delivering-boxes-from-storage-to-ports.py
1 parent a1d8a78 commit a91211e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def boxDelivering(self, boxes, portsCount, maxBoxes, maxWeight):
6+
"""
7+
:type boxes: List[List[int]]
8+
:type portsCount: int
9+
:type maxBoxes: int
10+
:type maxWeight: int
11+
:rtype: int
12+
"""
13+
dp = [0]*(len(boxes)+1)
14+
left, cost, curr = 0, 1, 0
15+
for right in xrange(len(boxes)):
16+
if right == 0 or boxes[right][0] != boxes[right-1][0]:
17+
cost += 1
18+
curr += boxes[right][1]
19+
while right-left+1 > maxBoxes or \
20+
curr > maxWeight or \
21+
(left+1 < right+1 and dp[left+1] == dp[left]):
22+
curr -= boxes[left][1]
23+
if boxes[left+1][0] != boxes[left][0]:
24+
cost -= 1
25+
left += 1
26+
dp[right+1] = dp[(left-1)+1] + cost
27+
return dp[len(boxes)]

0 commit comments

Comments
 (0)