diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README.md b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README.md index b870506f74593..1cc627db71068 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README.md +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README.md @@ -60,17 +60,17 @@ ### 方法一:遍历 -遍历数组,统计正整数和负整数的个数 $a$ 和 $b$,返回 $a$ 和 $b$ 中的较大值即可。 +我们可以直接遍历数组,统计正整数和负整数的个数 $a$ 和 $b$,返回 $a$ 和 $b$ 中的较大值即可。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。 +时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。 ```python class Solution: def maximumCount(self, nums: List[int]) -> int: - a = sum(v > 0 for v in nums) - b = sum(v < 0 for v in nums) + a = sum(x > 0 for x in nums) + b = sum(x < 0 for x in nums) return max(a, b) ``` @@ -78,11 +78,10 @@ class Solution: class Solution { public int maximumCount(int[] nums) { int a = 0, b = 0; - for (int v : nums) { - if (v > 0) { + for (int x : nums) { + if (x > 0) { ++a; - } - if (v < 0) { + } else if (x < 0) { ++b; } } @@ -96,11 +95,10 @@ class Solution { public: int maximumCount(vector& nums) { int a = 0, b = 0; - for (int& v : nums) { - if (v > 0) { + for (int x : nums) { + if (x > 0) { ++a; - } - if (v < 0) { + } else if (x < 0) { ++b; } } @@ -111,12 +109,11 @@ public: ```go func maximumCount(nums []int) int { - a, b := 0, 0 - for _, v := range nums { - if v > 0 { + var a, b int + for _, x := range nums { + if x > 0 { a++ - } - if v < 0 { + } else if x < 0 { b++ } } @@ -126,47 +123,50 @@ func maximumCount(nums []int) int { ```ts function maximumCount(nums: number[]): number { - const count = [0, 0]; - for (const num of nums) { - if (num < 0) { - count[0]++; - } else if (num > 0) { - count[1]++; + let [a, b] = [0, 0]; + for (const x of nums) { + if (x > 0) { + ++a; + } else if (x < 0) { + ++b; } } - return Math.max(...count); + return Math.max(a, b); } ``` ```rust impl Solution { pub fn maximum_count(nums: Vec) -> i32 { - let mut count = [0, 0]; - for &num in nums.iter() { - if num < 0 { - count[0] += 1; - } else if num > 0 { - count[1] += 1; + let mut a = 0; + let mut b = 0; + + for x in nums { + if x > 0 { + a += 1; + } else if x < 0 { + b += 1; } } - *count.iter().max().unwrap() + + std::cmp::max(a, b) } } ``` ```c -#define max(a, b) (((a) > (b)) ? (a) : (b)) +#define max(a, b) (a > b ? a : b) int maximumCount(int* nums, int numsSize) { - int count[2] = {0}; - for (int i = 0; i < numsSize; i++) { - if (nums[i] < 0) { - count[0]++; - } else if (nums[i] > 0) { - count[1]++; + int a = 0, b = 0; + for (int i = 0; i < numsSize; ++i) { + if (nums[i] > 0) { + ++a; + } else if (nums[i] < 0) { + ++b; } } - return max(count[0], count[1]); + return max(a, b); } ``` @@ -176,7 +176,7 @@ int maximumCount(int* nums, int numsSize) { 由于数组是按非递减顺序排列的,因此可以使用二分查找找到第一个大于等于 $1$ 的元素的下标 $i$ 以及第一个大于等于 $0$ 的元素的下标 $j$,那么正整数的个数 $a = n - i$,负整数的个数 $b = j$,返回 $a$ 和 $b$ 中的较大值即可。 -时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。 +时间复杂度 $O(\log n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。 @@ -232,37 +232,36 @@ func maximumCount(nums []int) int { ```ts function maximumCount(nums: number[]): number { - const search = (target: number) => { - let left = 0; - let right = n; + const search = (x: number): number => { + let [left, right] = [0, nums.length]; while (left < right) { - const mid = (left + right) >>> 1; - if (nums[mid] < target) { - left = mid + 1; - } else { + const mid = (left + right) >> 1; + if (nums[mid] >= x) { right = mid; + } else { + left = mid + 1; } } return left; }; - const n = nums.length; - const i = search(0); - const j = search(1); - return Math.max(i, n - j); + const i = search(1); + const j = search(0); + const [a, b] = [nums.length - i, j]; + return Math.max(a, b); } ``` ```rust impl Solution { - fn search(nums: &Vec, target: i32) -> usize { + fn search(nums: &Vec, x: i32) -> usize { let mut left = 0; let mut right = nums.len(); while left < right { let mid = (left + right) >> 1; - if nums[mid] < target { - left = mid + 1; - } else { + if nums[mid] >= x { right = mid; + } else { + left = mid + 1; } } left @@ -270,59 +269,34 @@ impl Solution { pub fn maximum_count(nums: Vec) -> i32 { let n = nums.len(); - let i = Self::search(&nums, 0); - let j = Self::search(&nums, 1); - i.max(n - j) as i32 + let i = Self::search(&nums, 1); + let j = Self::search(&nums, 0); + (n - i).max(j) as i32 } } ``` ```c -#define max(a, b) (((a) > (b)) ? (a) : (b)) +#define max(a, b) (a > b ? a : b) -int search(int* nums, int numsSize, int target) { +int search(int* nums, int numsSize, int x) { int left = 0; int right = numsSize; while (left < right) { int mid = (left + right) >> 1; - if (nums[mid] < target) { - left = mid + 1; - } else { + if (nums[mid] >= x) { right = mid; + } else { + left = mid + 1; } } return left; } int maximumCount(int* nums, int numsSize) { - int i = search(nums, numsSize, 0); - int j = search(nums, numsSize, 1); - return max(i, numsSize - j); -} -``` - - - -### 方法三 - - - -```rust -impl Solution { - pub fn maximum_count(nums: Vec) -> i32 { - let mut a = 0; - let mut b = 0; - - for n in nums { - if n > 0 { - a += 1; - } else if n < 0 { - b += 1; - } - } - - std::cmp::max(a, b) - } + int i = search(nums, numsSize, 1); + int j = search(nums, numsSize, 0); + return max(numsSize - i, j); } ``` diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README_EN.md b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README_EN.md index 55b9802257b19..1c8e8b6b8fab1 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README_EN.md +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README_EN.md @@ -53,15 +53,19 @@ ## Solutions -### Solution 1 +### Solution 1: Traversal + +We can directly traverse the array, count the number of positive and negative integers $a$ and $b$, and return the larger of $a$ and $b$. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. ```python class Solution: def maximumCount(self, nums: List[int]) -> int: - a = sum(v > 0 for v in nums) - b = sum(v < 0 for v in nums) + a = sum(x > 0 for x in nums) + b = sum(x < 0 for x in nums) return max(a, b) ``` @@ -69,11 +73,10 @@ class Solution: class Solution { public int maximumCount(int[] nums) { int a = 0, b = 0; - for (int v : nums) { - if (v > 0) { + for (int x : nums) { + if (x > 0) { ++a; - } - if (v < 0) { + } else if (x < 0) { ++b; } } @@ -87,11 +90,10 @@ class Solution { public: int maximumCount(vector& nums) { int a = 0, b = 0; - for (int& v : nums) { - if (v > 0) { + for (int x : nums) { + if (x > 0) { ++a; - } - if (v < 0) { + } else if (x < 0) { ++b; } } @@ -102,12 +104,11 @@ public: ```go func maximumCount(nums []int) int { - a, b := 0, 0 - for _, v := range nums { - if v > 0 { + var a, b int + for _, x := range nums { + if x > 0 { a++ - } - if v < 0 { + } else if x < 0 { b++ } } @@ -117,53 +118,60 @@ func maximumCount(nums []int) int { ```ts function maximumCount(nums: number[]): number { - const count = [0, 0]; - for (const num of nums) { - if (num < 0) { - count[0]++; - } else if (num > 0) { - count[1]++; + let [a, b] = [0, 0]; + for (const x of nums) { + if (x > 0) { + ++a; + } else if (x < 0) { + ++b; } } - return Math.max(...count); + return Math.max(a, b); } ``` ```rust impl Solution { pub fn maximum_count(nums: Vec) -> i32 { - let mut count = [0, 0]; - for &num in nums.iter() { - if num < 0 { - count[0] += 1; - } else if num > 0 { - count[1] += 1; + let mut a = 0; + let mut b = 0; + + for x in nums { + if x > 0 { + a += 1; + } else if x < 0 { + b += 1; } } - *count.iter().max().unwrap() + + std::cmp::max(a, b) } } ``` ```c -#define max(a, b) (((a) > (b)) ? (a) : (b)) +#define max(a, b) (a > b ? a : b) int maximumCount(int* nums, int numsSize) { - int count[2] = {0}; - for (int i = 0; i < numsSize; i++) { - if (nums[i] < 0) { - count[0]++; - } else if (nums[i] > 0) { - count[1]++; + int a = 0, b = 0; + for (int i = 0; i < numsSize; ++i) { + if (nums[i] > 0) { + ++a; + } else if (nums[i] < 0) { + ++b; } } - return max(count[0], count[1]); + return max(a, b); } ``` -### Solution 2 +### Solution 2: Binary Search + +Since the array is sorted in non-decreasing order, we can use binary search to find the index $i$ of the first element that is greater than or equal to $1$, and the index $j$ of the first element that is greater than or equal to $0$. The number of positive integers is $a = n - i$, and the number of negative integers is $b = j$. We return the larger of $a$ and $b$. + +The time complexity is $O(\log n)$, where $n$ is the length of the array. The space complexity is $O(1)$. @@ -219,37 +227,36 @@ func maximumCount(nums []int) int { ```ts function maximumCount(nums: number[]): number { - const search = (target: number) => { - let left = 0; - let right = n; + const search = (x: number): number => { + let [left, right] = [0, nums.length]; while (left < right) { - const mid = (left + right) >>> 1; - if (nums[mid] < target) { - left = mid + 1; - } else { + const mid = (left + right) >> 1; + if (nums[mid] >= x) { right = mid; + } else { + left = mid + 1; } } return left; }; - const n = nums.length; - const i = search(0); - const j = search(1); - return Math.max(i, n - j); + const i = search(1); + const j = search(0); + const [a, b] = [nums.length - i, j]; + return Math.max(a, b); } ``` ```rust impl Solution { - fn search(nums: &Vec, target: i32) -> usize { + fn search(nums: &Vec, x: i32) -> usize { let mut left = 0; let mut right = nums.len(); while left < right { let mid = (left + right) >> 1; - if nums[mid] < target { - left = mid + 1; - } else { + if nums[mid] >= x { right = mid; + } else { + left = mid + 1; } } left @@ -257,59 +264,34 @@ impl Solution { pub fn maximum_count(nums: Vec) -> i32 { let n = nums.len(); - let i = Self::search(&nums, 0); - let j = Self::search(&nums, 1); - i.max(n - j) as i32 + let i = Self::search(&nums, 1); + let j = Self::search(&nums, 0); + (n - i).max(j) as i32 } } ``` ```c -#define max(a, b) (((a) > (b)) ? (a) : (b)) +#define max(a, b) (a > b ? a : b) -int search(int* nums, int numsSize, int target) { +int search(int* nums, int numsSize, int x) { int left = 0; int right = numsSize; while (left < right) { int mid = (left + right) >> 1; - if (nums[mid] < target) { - left = mid + 1; - } else { + if (nums[mid] >= x) { right = mid; + } else { + left = mid + 1; } } return left; } int maximumCount(int* nums, int numsSize) { - int i = search(nums, numsSize, 0); - int j = search(nums, numsSize, 1); - return max(i, numsSize - j); -} -``` - - - -### Solution 3 - - - -```rust -impl Solution { - pub fn maximum_count(nums: Vec) -> i32 { - let mut a = 0; - let mut b = 0; - - for n in nums { - if n > 0 { - a += 1; - } else if n < 0 { - b += 1; - } - } - - std::cmp::max(a, b) - } + int i = search(nums, numsSize, 1); + int j = search(nums, numsSize, 0); + return max(numsSize - i, j); } ``` diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.c b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.c index 8262cf30c55c4..aa86f4c747d66 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.c +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.c @@ -1,13 +1,13 @@ -#define max(a, b) (((a) > (b)) ? (a) : (b)) +#define max(a, b) (a > b ? a : b) int maximumCount(int* nums, int numsSize) { - int count[2] = {0}; - for (int i = 0; i < numsSize; i++) { - if (nums[i] < 0) { - count[0]++; - } else if (nums[i] > 0) { - count[1]++; + int a = 0, b = 0; + for (int i = 0; i < numsSize; ++i) { + if (nums[i] > 0) { + ++a; + } else if (nums[i] < 0) { + ++b; } } - return max(count[0], count[1]); + return max(a, b); } \ No newline at end of file diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.cpp b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.cpp index ca924f9995e67..543b0d42ccba1 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.cpp +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.cpp @@ -2,11 +2,10 @@ class Solution { public: int maximumCount(vector& nums) { int a = 0, b = 0; - for (int& v : nums) { - if (v > 0) { + for (int x : nums) { + if (x > 0) { ++a; - } - if (v < 0) { + } else if (x < 0) { ++b; } } diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.go b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.go index bb4508a3da925..0cb236ed6d416 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.go +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.go @@ -1,10 +1,9 @@ func maximumCount(nums []int) int { - a, b := 0, 0 - for _, v := range nums { - if v > 0 { + var a, b int + for _, x := range nums { + if x > 0 { a++ - } - if v < 0 { + } else if x < 0 { b++ } } diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.java b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.java index 492ca1b07a803..1a3837fd08be4 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.java +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.java @@ -1,11 +1,10 @@ class Solution { public int maximumCount(int[] nums) { int a = 0, b = 0; - for (int v : nums) { - if (v > 0) { + for (int x : nums) { + if (x > 0) { ++a; - } - if (v < 0) { + } else if (x < 0) { ++b; } } diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.py b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.py index 9a4fb379b8092..4d0dd2793cd5b 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.py +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.py @@ -1,5 +1,5 @@ class Solution: def maximumCount(self, nums: List[int]) -> int: - a = sum(v > 0 for v in nums) - b = sum(v < 0 for v in nums) + a = sum(x > 0 for x in nums) + b = sum(x < 0 for x in nums) return max(a, b) diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.rs b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.rs index d8fc9365fcd4d..2f1e0f1167156 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.rs +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.rs @@ -1,13 +1,16 @@ impl Solution { pub fn maximum_count(nums: Vec) -> i32 { - let mut count = [0, 0]; - for &num in nums.iter() { - if num < 0 { - count[0] += 1; - } else if num > 0 { - count[1] += 1; + let mut a = 0; + let mut b = 0; + + for x in nums { + if x > 0 { + a += 1; + } else if x < 0 { + b += 1; } } - *count.iter().max().unwrap() + + std::cmp::max(a, b) } } diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.ts b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.ts index b89b8204abf3c..a2651420b5743 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.ts +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution.ts @@ -1,11 +1,11 @@ function maximumCount(nums: number[]): number { - const count = [0, 0]; - for (const num of nums) { - if (num < 0) { - count[0]++; - } else if (num > 0) { - count[1]++; + let [a, b] = [0, 0]; + for (const x of nums) { + if (x > 0) { + ++a; + } else if (x < 0) { + ++b; } } - return Math.max(...count); + return Math.max(a, b); } diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.c b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.c index 9c4877a6a99e2..472eff15ca4cf 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.c +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.c @@ -1,21 +1,21 @@ -#define max(a, b) (((a) > (b)) ? (a) : (b)) +#define max(a, b) (a > b ? a : b) -int search(int* nums, int numsSize, int target) { +int search(int* nums, int numsSize, int x) { int left = 0; int right = numsSize; while (left < right) { int mid = (left + right) >> 1; - if (nums[mid] < target) { - left = mid + 1; - } else { + if (nums[mid] >= x) { right = mid; + } else { + left = mid + 1; } } return left; } int maximumCount(int* nums, int numsSize) { - int i = search(nums, numsSize, 0); - int j = search(nums, numsSize, 1); - return max(i, numsSize - j); + int i = search(nums, numsSize, 1); + int j = search(nums, numsSize, 0); + return max(numsSize - i, j); } \ No newline at end of file diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.java b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.java index 8eae0f526f906..7426d8171b95a 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.java +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.java @@ -1,20 +1,9 @@ class Solution { public int maximumCount(int[] nums) { - int a = nums.length - search(nums, 1); - int b = search(nums, 0); + int i = Arrays.binarySearch(nums, 1); + int a = i < 0 ? nums.length - (-i - 1) : nums.length - i; + int j = Arrays.binarySearch(nums, 0); + int b = j < 0 ? -j - 1 : j; return Math.max(a, b); } - - private int search(int[] nums, int x) { - int left = 0, right = nums.length; - while (left < right) { - int mid = (left + right) >> 1; - if (nums[mid] >= x) { - right = mid; - } else { - left = mid + 1; - } - } - return left; - } } \ No newline at end of file diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.rs b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.rs index 75c5f9ed944fc..ecd6df7f21164 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.rs +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.rs @@ -1,13 +1,13 @@ impl Solution { - fn search(nums: &Vec, target: i32) -> usize { + fn search(nums: &Vec, x: i32) -> usize { let mut left = 0; let mut right = nums.len(); while left < right { let mid = (left + right) >> 1; - if nums[mid] < target { - left = mid + 1; - } else { + if nums[mid] >= x { right = mid; + } else { + left = mid + 1; } } left @@ -15,8 +15,8 @@ impl Solution { pub fn maximum_count(nums: Vec) -> i32 { let n = nums.len(); - let i = Self::search(&nums, 0); - let j = Self::search(&nums, 1); - i.max(n - j) as i32 + let i = Self::search(&nums, 1); + let j = Self::search(&nums, 0); + (n - i).max(j) as i32 } } diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.ts b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.ts index aae118f62b519..f40223af05f62 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.ts +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution2.ts @@ -1,19 +1,18 @@ function maximumCount(nums: number[]): number { - const search = (target: number) => { - let left = 0; - let right = n; + const search = (x: number): number => { + let [left, right] = [0, nums.length]; while (left < right) { - const mid = (left + right) >>> 1; - if (nums[mid] < target) { - left = mid + 1; - } else { + const mid = (left + right) >> 1; + if (nums[mid] >= x) { right = mid; + } else { + left = mid + 1; } } return left; }; - const n = nums.length; - const i = search(0); - const j = search(1); - return Math.max(i, n - j); + const i = search(1); + const j = search(0); + const [a, b] = [nums.length - i, j]; + return Math.max(a, b); } diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution3.rs b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution3.rs deleted file mode 100644 index 61945cb483e7d..0000000000000 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/Solution3.rs +++ /dev/null @@ -1,16 +0,0 @@ -impl Solution { - pub fn maximum_count(nums: Vec) -> i32 { - let mut a = 0; - let mut b = 0; - - for n in nums { - if n > 0 { - a += 1; - } else if n < 0 { - b += 1; - } - } - - std::cmp::max(a, b) - } -}