-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2106_Maximum_Fruits_Harvested_After_at_Most_K_Steps.py
More file actions
82 lines (61 loc) · 2.79 KB
/
Copy path2106_Maximum_Fruits_Harvested_After_at_Most_K_Steps.py
File metadata and controls
82 lines (61 loc) · 2.79 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array fruits where fruits[i] = [positioni, amounti] depicts amounti fruits at the position positioni. fruits is already sorted by positioni in ascending order, and each positioni is unique.
You are also given an integer startPos and an integer k. Initially, you are at the position startPos. From any position, you can either walk to the left or right. It takes one step to move one unit on the x-axis, and you can walk at most k steps in total. For every position you reach, you harvest all the fruits at that position, and the fruits will disappear from that position.
Return the maximum total number of fruits you can harvest.
Example 1:
Input: fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4
Output: 9
Explanation:
The optimal way is to:
- Move right to position 6 and harvest 3 fruits
- Move right to position 8 and harvest 6 fruits
You moved 3 steps and harvested 3 + 6 = 9 fruits in total.
Example 2:
Input: fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4
Output: 14
Explanation:
You can move at most k = 4 steps, so you cannot reach position 0 nor 10.
The optimal way is to:
- Harvest the 7 fruits at the starting position 5
- Move left to position 4 and harvest 1 fruit
- Move right to position 6 and harvest 2 fruits
- Move right to position 7 and harvest 4 fruits
You moved 1 + 3 = 4 steps and harvested 7 + 1 + 2 + 4 = 14 fruits in total.
Example 3:
Input: fruits = [[0,3],[6,4],[8,5]], startPos = 3, k = 2
Output: 0
Explanation:
You can move at most k = 2 steps and cannot reach any position with fruits.
Constraints:
1 <= fruits.length <= 105
fruits[i].length == 2
0 <= startPos, positioni <= 2 * 105
positioni-1 < positioni for any i > 0 (0-indexed)
1 <= amounti <= 104
0 <= k <= 2 * 105
"""
from bisect import bisect_left, bisect_right
class Solution:
def maxTotalFruits(self, fruits: List[List[int]], startPos: int, k: int) -> int:
# Extract positions and prefix sums
pos = [p for p, _ in fruits]
preSum = [0] * (len(fruits) + 1)
for i in range(len(fruits)):
preSum[i+1] = preSum[i] + fruits[i][1]
def get_fruits(l, r):
# Return sum of fruits in [l, r]
i = bisect_left(pos, l)
j = bisect_right(pos, r)
return preSum[j] - preSum[i]
res = 0
# Case 1: go left first, then right
for x in range(k + 1):
left = startPos - x
right = startPos + max(0, k - 2 * x)
res = max(res, get_fruits(left, right))
# Case 2: go right first, then left
for x in range(k + 1):
right = startPos + x
left = startPos - max(0, k - 2 * x)
res = max(res, get_fruits(left, right))
return res