Skip to content

Commit fa67119

Browse files
authored
Merge pull request #1276 from 0xff-dev/2106
Add solution and test-cases for problem 2106
2 parents a5c38f4 + 08e7783 commit fa67119

File tree

6 files changed

+104
-26
lines changed

6 files changed

+104
-26
lines changed
10.4 KB
Loading
11.5 KB
Loading
9.32 KB
Loading

leetcode/2101-2200/2106.Maximum-Fruits-Harvested-After-at-Most-K-Steps/README.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,53 @@
11
# [2106.Maximum Fruits Harvested After at Most K Steps][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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**.
5+
6+
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.
7+
8+
Return the **maximum total number** of fruits you can harvest.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![1](./1.png)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4
16+
Output: 9
17+
Explanation:
18+
The optimal way is to:
19+
- Move right to position 6 and harvest 3 fruits
20+
- Move right to position 8 and harvest 6 fruits
21+
You moved 3 steps and harvested 3 + 6 = 9 fruits in total.
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
26+
![2](./2.png)
1927

20-
### 思路1
21-
> ...
22-
Maximum Fruits Harvested After at Most K Steps
23-
```go
28+
```
29+
Input: fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4
30+
Output: 14
31+
Explanation:
32+
You can move at most k = 4 steps, so you cannot reach position 0 nor 10.
33+
The optimal way is to:
34+
- Harvest the 7 fruits at the starting position 5
35+
- Move left to position 4 and harvest 1 fruit
36+
- Move right to position 6 and harvest 2 fruits
37+
- Move right to position 7 and harvest 4 fruits
38+
You moved 1 + 3 = 4 steps and harvested 7 + 1 + 2 + 4 = 14 fruits in total.
2439
```
2540

41+
**Example 3:**
42+
43+
![3](./3.png)
44+
45+
```
46+
Input: fruits = [[0,3],[6,4],[8,5]], startPos = 3, k = 2
47+
Output: 0
48+
Explanation:
49+
You can move at most k = 2 steps and cannot reach any position with fruits.
50+
```
2651

2752
## 结语
2853

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,57 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(fruits [][]int, startPos int, k int) int {
6+
l := len(fruits)
7+
index := sort.Search(l, func(i int) bool {
8+
return fruits[i][0] >= startPos
9+
})
10+
ans, dis := 0, 0
11+
sum := make([]int, l)
12+
13+
a := 0
14+
for i := index; i < l; i++ {
15+
a += fruits[i][1]
16+
sum[i] = a
17+
}
18+
a = 0
19+
for i := index - 1; i >= 0; i-- {
20+
a += fruits[i][1]
21+
sum[i] = a
22+
}
23+
right := 0
24+
for i := index; i < l; i++ {
25+
dis = fruits[i][0] - startPos
26+
if dis > k {
27+
break
28+
}
29+
right += fruits[i][1]
30+
ans = max(ans, right)
31+
32+
if x := sort.Search(index, func(j int) bool {
33+
return fruits[i][0]-fruits[j][0] <= k-dis
34+
}); x != index {
35+
ans = max(ans, right+sum[x])
36+
}
37+
}
38+
left := 0
39+
ll := l - index
40+
41+
for i := index - 1; i >= 0; i-- {
42+
dis = startPos - fruits[i][0]
43+
if dis > k {
44+
break
45+
}
46+
left += fruits[i][1]
47+
48+
ans = max(ans, left)
49+
if x := sort.Search(ll, func(j int) bool {
50+
return fruits[index+j][0]-fruits[i][0] > k-dis
51+
}); x != 0 {
52+
ans = max(ans, left+sum[index+x-1])
53+
54+
}
55+
}
56+
return ans
557
}

leetcode/2101-2200/2106.Maximum-Fruits-Harvested-After-at-Most-K-Steps/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
inputs [][]int
14+
startPos, k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{{2, 8}, {6, 3}, {8, 6}}, 5, 4, 9},
18+
{"TestCase2", [][]int{{0, 9}, {4, 1}, {5, 7}, {6, 2}, {7, 4}, {10, 9}}, 5, 4, 14},
19+
{"TestCase3", [][]int{{0, 3}, {6, 4}, {8, 5}}, 3, 2, 0},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.startPos, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.inputs, c.startPos, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)