Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 962 #1005

Merged
merged 1 commit into from
Oct 30, 2024
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
25 changes: 11 additions & 14 deletions leetcode/901-1000/0962.Maximum-Width-Ramp/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
# [962.Maximum Width Ramp][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
A **ramp** in an integer array `nums` is a pair `(i, j)` for which `i < j` and `nums[i] <= nums[j]`. The **width** of such a ramp is `j - i`.

Given an integer array `nums`, return the maximum width of a **ramp** in `nums`. If there is no **ramp** in `nums`, return `0`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [6,0,8,2,1,5]
Output: 4
Explanation: The maximum width ramp is achieved at (i, j) = (1, 5): nums[1] = 0 and nums[5] = 5.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Maximum Width Ramp
```go
```

Input: nums = [9,8,1,0,1,9,4,0,4,1]
Output: 7
Explanation: The maximum width ramp is achieved at (i, j) = (2, 9): nums[2] = 1 and nums[9] = 1.
```

## 结语

Expand Down
24 changes: 22 additions & 2 deletions leetcode/901-1000/0962.Maximum-Width-Ramp/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int) int {
n := len(nums)
stack := make([]int, n)

index := -1
for i := 0; i < n; i++ {
if index == -1 || nums[stack[index]] > nums[i] {
index++
stack[index] = i
}
}

ans := 0

for j := n - 1; j >= 0; j-- {
for index != -1 && nums[stack[index]] <= nums[j] {
ans = max(ans, j-stack[index])
index--
}
}

return ans
}
13 changes: 6 additions & 7 deletions leetcode/901-1000/0962.Maximum-Width-Ramp/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{6, 0, 8, 2, 1, 5}, 4},
{"TestCase2", []int{9, 8, 1, 0, 1, 9, 4, 0, 4, 1}, 7},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

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

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