Skip to content

Commit 59c081d

Browse files
committed
Sync LeetCode submission Runtime - 670 ms (34.28%), Memory - 52.5 MB (50.71%)
1 parent a9d1f87 commit 59c081d

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<p>Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array <code>fruits</code> where <code>fruits[i] = [position<sub>i</sub>, amount<sub>i</sub>]</code> depicts <code>amount<sub>i</sub></code> fruits at the position <code>position<sub>i</sub></code>. <code>fruits</code> is already <strong>sorted</strong> by <code>position<sub>i</sub></code> in <strong>ascending order</strong>, and each <code>position<sub>i</sub></code> is <strong>unique</strong>.</p>
2+
3+
<p>You are also given an integer <code>startPos</code> and an integer <code>k</code>. Initially, you are at the position <code>startPos</code>. From any position, you can either walk to the <strong>left or right</strong>. It takes <strong>one step</strong> to move <strong>one unit</strong> on the x-axis, and you can walk <strong>at most</strong> <code>k</code> steps in total. For every position you reach, you harvest all the fruits at that position, and the fruits will disappear from that position.</p>
4+
5+
<p>Return <em>the <strong>maximum total number</strong> of fruits you can harvest</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/21/1.png" style="width: 472px; height: 115px;" />
10+
<pre>
11+
<strong>Input:</strong> fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4
12+
<strong>Output:</strong> 9
13+
<strong>Explanation:</strong>
14+
The optimal way is to:
15+
- Move right to position 6 and harvest 3 fruits
16+
- Move right to position 8 and harvest 6 fruits
17+
You moved 3 steps and harvested 3 + 6 = 9 fruits in total.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/21/2.png" style="width: 512px; height: 129px;" />
22+
<pre>
23+
<strong>Input:</strong> fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4
24+
<strong>Output:</strong> 14
25+
<strong>Explanation:</strong>
26+
You can move at most k = 4 steps, so you cannot reach position 0 nor 10.
27+
The optimal way is to:
28+
- Harvest the 7 fruits at the starting position 5
29+
- Move left to position 4 and harvest 1 fruit
30+
- Move right to position 6 and harvest 2 fruits
31+
- Move right to position 7 and harvest 4 fruits
32+
You moved 1 + 3 = 4 steps and harvested 7 + 1 + 2 + 4 = 14 fruits in total.
33+
</pre>
34+
35+
<p><strong class="example">Example 3:</strong></p>
36+
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/21/3.png" style="width: 476px; height: 100px;" />
37+
<pre>
38+
<strong>Input:</strong> fruits = [[0,3],[6,4],[8,5]], startPos = 3, k = 2
39+
<strong>Output:</strong> 0
40+
<strong>Explanation:</strong>
41+
You can move at most k = 2 steps and cannot reach any position with fruits.
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= fruits.length &lt;= 10<sup>5</sup></code></li>
49+
<li><code>fruits[i].length == 2</code></li>
50+
<li><code>0 &lt;= startPos, position<sub>i</sub> &lt;= 2 * 10<sup>5</sup></code></li>
51+
<li><code>position<sub>i-1</sub> &lt; position<sub>i</sub></code> for any <code>i &gt; 0</code>&nbsp;(<strong>0-indexed</strong>)</li>
52+
<li><code>1 &lt;= amount<sub>i</sub> &lt;= 10<sup>4</sup></code></li>
53+
<li><code>0 &lt;= k &lt;= 2 * 10<sup>5</sup></code></li>
54+
</ul>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Approach 1: Binary Search
2+
3+
# Time: O(n + k log n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def maxTotalFruits(self, fruits: List[List[int]], startPos: int, k: int) -> int:
8+
n = len(fruits)
9+
sum_ = [0] * (n + 1)
10+
indices = [0] * n
11+
12+
for i in range(n):
13+
sum_[i + 1] = sum_[i] + fruits[i][1]
14+
indices[i] = fruits[i][0]
15+
16+
ans = 0
17+
for x in range(k // 2 + 1):
18+
# Move left x steps, then right (k - 2x) steps
19+
y = k - 2 * x
20+
left = startPos - x
21+
right = startPos + y
22+
start = bisect_left(indices, left)
23+
end = bisect_right(indices, right)
24+
ans = max(ans, sum_[end] - sum_[start])
25+
26+
# Move right x steps, then left (k - 2x) steps
27+
y = k - 2 * x
28+
left = startPos - y
29+
right = startPos + x
30+
start = bisect_left(indices, left)
31+
end = bisect_right(indices, right)
32+
ans = max(ans, sum_[end] - sum_[start])
33+
34+
return ans

0 commit comments

Comments
 (0)