Skip to content

Commit

Permalink
Merge pull request #787 from 0xff-dev/1642
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1642
  • Loading branch information
6boris committed Mar 23, 2024
2 parents 4d40c88 + d695cb8 commit 2f472bc
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 26 deletions.
44 changes: 30 additions & 14 deletions leetcode/1601-1700/1642.Furthest-Building-You-Can-Reach/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
# [1642.Furthest Building You Can Reach][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
You are given an integer array `heights` representing the heights of buildings, some `bricks`, and some `ladders`.

You start your journey from building `0` and move to the next building by possibly using bricks or ladders.

While moving from building `i` to building `i+1` (**0-indexed**),

- If the current building's height is **greater than or equal** to the next building's height, you do **not** need a ladder or bricks.
- If the current building's height is **less than** the next building's height, you can either use **one ladder** or `(h[i+1] - h[i])` **bricks**.
Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.

**Example 1:**

**Example 1:**
![1](./q4.gif)

```
Input: a = "11", b = "1"
Output: "100"
Input: heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
Output: 4
Explanation: Starting at building 0, you can follow these steps:
- Go to building 1 without using ladders nor bricks since 4 >= 2.
- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 < 7.
- Go to building 3 without using ladders nor bricks since 7 >= 6.
- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 < 9.
It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Furthest Building You Can Reach
```go
```
Input: heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
Output: 7
```

**Example 3:**

```
Input: heights = [14,3,19,3], bricks = 17, ladders = 0
Output: 3
```

## 结语

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

func Solution(x bool) bool {
import "container/heap"

func Solution(heights []int, bricks int, ladders int) int {
// 1 2 3 4 5 6
// 4 0 1 1 1 9996 diff bsearchr+judge????
// b=4, l=1
diff := make([]int, len(heights))
for i := 1; i < len(heights); i++ {
if r := heights[i] - heights[i-1]; r > 0 {
diff[i] = r
}
}
l, r := 0, len(heights)
for l < r {
mid := (l + r) / 2
tmp := make([]int, mid+1)
for i := 0; i <= mid; i++ {
tmp[i] = diff[i]
}
if isOk1642(tmp, bricks, ladders) {
l = mid + 1
} else {
r = mid
}
}
if l != 0 {
l--
}
return l
}

type heap1642 []int

func (h *heap1642) Len() int {
return len(*h)
}

func (h *heap1642) Less(i, j int) bool {
return (*h)[i] < (*h)[j]
}
func (h *heap1642) Swap(i, j int) {
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
}
func (h *heap1642) Push(x interface{}) {
*h = append(*h, x.(int))
}

func (h *heap1642) Pop() interface{} {
old := *h
l := len(old)
x := old[l-1]
*h = old[:l-1]
return x
}
func isOk1642(diffs []int, b, l int) bool {
h := heap1642(diffs)
heap.Init(&h)
for h.Len() > 0 {
top := heap.Pop(&h).(int)
if top <= 0 {
continue
}
if top <= b {
b -= top
continue
}
if l > 0 {
l--
continue
}
return false
}
return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,33 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
heights []int
bricks, ladders int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{4, 2, 7, 6, 9, 14, 12}, 5, 1, 4},
{"TestCase2", []int{4, 12, 2, 7, 3, 18, 20, 3, 19}, 10, 2, 7},
{"TestCase3", []int{14, 3, 19, 3}, 17, 0, 3},
{"TestCase4", []int{7, 5, 13}, 0, 0, 1},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.heights, c.bricks, c.ladders)
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.heights, c.bricks, c.ladders)
}
})
}
}

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

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2f472bc

Please sign in to comment.