diff --git a/leetcode/2501-2600/2529.Maximum-Count-of-Positive-Integer-and-Negative-Integer/README.md b/leetcode/2501-2600/2529.Maximum-Count-of-Positive-Integer-and-Negative-Integer/README.md new file mode 100644 index 000000000..cc35a4f1e --- /dev/null +++ b/leetcode/2501-2600/2529.Maximum-Count-of-Positive-Integer-and-Negative-Integer/README.md @@ -0,0 +1,39 @@ +# [2529.Maximum Count of Positive Integer and Negative Integer][title] + +## Description +Given an array `nums` sorted in **non-decreasing** order, return the maximum between the number of positive integers and the number of negative integers. + +- 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`. + +**Note** that `0` is neither positive nor negative. + +**Example 1:** + +``` +Input: nums = [-2,-1,-1,1,2,3] +Output: 3 +Explanation: There are 3 positive integers and 3 negative integers. The maximum count among them is 3. +``` + +**Example 2:** + +``` +Input: nums = [-3,-2,-1,0,0,1,2] +Output: 3 +Explanation: There are 2 positive integers and 3 negative integers. The maximum count among them is 3. +``` + +**Example 3:** + +``` +Input: nums = [5,20,66,1314] +Output: 4 +Explanation: There are 4 positive integers and 0 negative integers. The maximum count among them is 4. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2529.Maximum-Count-of-Positive-Integer-and-Negative-Integer/Solution.go b/leetcode/2501-2600/2529.Maximum-Count-of-Positive-Integer-and-Negative-Integer/Solution.go index d115ccf5e..1dd989dd3 100755 --- a/leetcode/2501-2600/2529.Maximum-Count-of-Positive-Integer-and-Negative-Integer/Solution.go +++ b/leetcode/2501-2600/2529.Maximum-Count-of-Positive-Integer-and-Negative-Integer/Solution.go @@ -1,5 +1,16 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(nums []int) int { + l := len(nums) + posIndex := sort.Search(l, func(i int) bool { + return nums[i] > 0 + }) + pos := l - posIndex + + negIndex := sort.Search(l, func(i int) bool { + return nums[i] >= 0 + }) + return max(pos, negIndex) } diff --git a/leetcode/2501-2600/2529.Maximum-Count-of-Positive-Integer-and-Negative-Integer/Solution_test.go b/leetcode/2501-2600/2529.Maximum-Count-of-Positive-Integer-and-Negative-Integer/Solution_test.go index 14ff50eb4..a51517f1d 100755 --- a/leetcode/2501-2600/2529.Maximum-Count-of-Positive-Integer-and-Negative-Integer/Solution_test.go +++ b/leetcode/2501-2600/2529.Maximum-Count-of-Positive-Integer-and-Negative-Integer/Solution_test.go @@ -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{-2, -1, -1, 1, 2, 3}, 3}, + {"TestCase2", []int{-3, -2, -1, 0, 0, 1, 2}, 3}, + {"TestCase3", []int{5, 20, 66, 1314}, 4}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }