Skip to content

Commit

Permalink
Merge pull request #867 from 0xff-dev/2441
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2441
  • Loading branch information
6boris committed Jun 6, 2024
2 parents c8ff7b5 + 2c36a83 commit 84e9c19
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
# [2441.Largest Positive Integer That Exists With Its Negative][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
Given an integer array nums that **does not contain** any zeros, find **the largest positive** integer `k` such that `-k` also exists in the array.

Return the positive integer `k`. If there is no such integer, return `-1`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [-1,2,-3,3]
Output: 3
Explanation: 3 is the only valid k we can find in the array.
```

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

## 题解

### 思路1
> ...
Largest Positive Integer That Exists With Its Negative
```go
```
Input: nums = [-1,10,6,7,-7,1]
Output: 7
Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
```

**Exists 3:**

```
Input: nums = [-10,8,6,7,-2,-3]
Output: -1
Explanation: There is no a single valid k, we return -1.
```

## 结语

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

func Solution(x bool) bool {
return x
func Solution(nums []int) int {
e := make(map[int]struct{})
ans := -1
for _, n := range nums {
if _, ok := e[-n]; ok {
if n < 0 {
n = -n
}
ans = max(ans, n)
continue
}
e[n] = struct{}{}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ 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{-1, 2, -3, 3}, 3},
{"TestCase2", []int{-1, 10, 6, 7, -7, 1}, 7},
{"TestCase3", []int{-10, 8, 6, 7, -2, -3}, -1},
}

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

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

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

0 comments on commit 84e9c19

Please sign in to comment.