Skip to content

Commit 84e9c19

Browse files
authored
Merge pull request #867 from 0xff-dev/2441
Add solution and test-cases for problem 2441
2 parents c8ff7b5 + 2c36a83 commit 84e9c19

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

leetcode/2401-2500/2441.Largest-Positive-Integer-That-Exists-With-Its-Negative/README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [2441.Largest Positive Integer That Exists With Its Negative][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+
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.
5+
6+
Return the positive integer `k`. If there is no such integer, return `-1`.
77

88
**Example 1:**
99

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

15-
## 题意
16-
> ...
16+
**Exists 2:**
1717

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Largest Positive Integer That Exists With Its Negative
23-
```go
18+
```
19+
Input: nums = [-1,10,6,7,-7,1]
20+
Output: 7
21+
Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
2422
```
2523

24+
**Exists 3:**
25+
26+
```
27+
Input: nums = [-10,8,6,7,-2,-3]
28+
Output: -1
29+
Explanation: There is no a single valid k, we return -1.
30+
```
2631

2732
## 结语
2833

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
e := make(map[int]struct{})
5+
ans := -1
6+
for _, n := range nums {
7+
if _, ok := e[-n]; ok {
8+
if n < 0 {
9+
n = -n
10+
}
11+
ans = max(ans, n)
12+
continue
13+
}
14+
e[n] = struct{}{}
15+
}
16+
return ans
517
}

leetcode/2401-2500/2441.Largest-Positive-Integer-That-Exists-With-Its-Negative/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{-1, 2, -3, 3}, 3},
17+
{"TestCase2", []int{-1, 10, 6, 7, -7, 1}, 7},
18+
{"TestCase3", []int{-10, 8, 6, 7, -2, -3}, -1},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)