Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
# [2106.Maximum Fruits Harvested After at Most K Steps][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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:**
**Example 1:**

![1](./1.png)

```
Input: a = "11", b = "1"
Output: "100"
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:**

## 题解
![2](./2.png)

### 思路1
> ...
Maximum Fruits Harvested After at Most K Steps
```go
```
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:**

![3](./3.png)

```
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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func Solution(fruits [][]int, startPos int, k int) int {
l := len(fruits)
index := sort.Search(l, func(i int) bool {
return fruits[i][0] >= startPos
})
ans, dis := 0, 0
sum := make([]int, l)

a := 0
for i := index; i < l; i++ {
a += fruits[i][1]
sum[i] = a
}
a = 0
for i := index - 1; i >= 0; i-- {
a += fruits[i][1]
sum[i] = a
}
right := 0
for i := index; i < l; i++ {
dis = fruits[i][0] - startPos
if dis > k {
break
}
right += fruits[i][1]
ans = max(ans, right)

if x := sort.Search(index, func(j int) bool {
return fruits[i][0]-fruits[j][0] <= k-dis
}); x != index {
ans = max(ans, right+sum[x])
}
}
left := 0
ll := l - index

for i := index - 1; i >= 0; i-- {
dis = startPos - fruits[i][0]
if dis > k {
break
}
left += fruits[i][1]

ans = max(ans, left)
if x := sort.Search(ll, func(j int) bool {
return fruits[index+j][0]-fruits[i][0] > k-dis
}); x != 0 {
ans = max(ans, left+sum[index+x-1])

}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
inputs [][]int
startPos, k int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{2, 8}, {6, 3}, {8, 6}}, 5, 4, 9},
{"TestCase2", [][]int{{0, 9}, {4, 1}, {5, 7}, {6, 2}, {7, 4}, {10, 9}}, 5, 4, 14},
{"TestCase3", [][]int{{0, 3}, {6, 4}, {8, 5}}, 3, 2, 0},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.startPos, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.inputs, c.startPos, c.k)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading