Skip to content

Commit 9d856e2

Browse files
authored
Merge pull request #1147 from 0xff-dev/2529
Add solution and test-cases for problem 2529
2 parents 57e8469 + ce59d84 commit 9d856e2

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [2529.Maximum Count of Positive Integer and Negative Integer][title]
2+
3+
## Description
4+
Given an array `nums` sorted in **non-decreasing** order, return the maximum between the number of positive integers and the number of negative integers.
5+
6+
- In other words, if the number of positive integers in `nums` is `pos` and the number of negative integers is `neg`, then return the maximum of `pos` and `neg`.
7+
8+
**Note** that `0` is neither positive nor negative.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: nums = [-2,-1,-1,1,2,3]
14+
Output: 3
15+
Explanation: There are 3 positive integers and 3 negative integers. The maximum count among them is 3.
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: nums = [-3,-2,-1,0,0,1,2]
22+
Output: 3
23+
Explanation: There are 2 positive integers and 3 negative integers. The maximum count among them is 3.
24+
```
25+
26+
**Example 3:**
27+
28+
```
29+
Input: nums = [5,20,66,1314]
30+
Output: 4
31+
Explanation: There are 4 positive integers and 0 negative integers. The maximum count among them is 4.
32+
```
33+
34+
## 结语
35+
36+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
37+
38+
[title]: https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer
39+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int) int {
6+
l := len(nums)
7+
posIndex := sort.Search(l, func(i int) bool {
8+
return nums[i] > 0
9+
})
10+
pos := l - posIndex
11+
12+
negIndex := sort.Search(l, func(i int) bool {
13+
return nums[i] >= 0
14+
})
15+
return max(pos, negIndex)
516
}

leetcode/2501-2600/2529.Maximum-Count-of-Positive-Integer-and-Negative-Integer/Solution_test.go

+7-7
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{-2, -1, -1, 1, 2, 3}, 3},
17+
{"TestCase2", []int{-3, -2, -1, 0, 0, 1, 2}, 3},
18+
{"TestCase3", []int{5, 20, 66, 1314}, 4},
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)