Skip to content

Commit

Permalink
feat: update solutions to lc problem: No.2529 (#2553)
Browse files Browse the repository at this point in the history
No.2529.Maximum Count of Positive Integer and Negative Integer
  • Loading branch information
yanglbme authored Apr 8, 2024
1 parent ea860e7 commit 96852d1
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,28 @@

### 方法一:遍历

遍历数组,统计正整数和负整数的个数 $a$ 和 $b$,返回 $a$ 和 $b$ 中的较大值即可。
我们可以直接遍历数组,统计正整数和负整数的个数 $a$ 和 $b$,返回 $a$ 和 $b$ 中的较大值即可。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度
时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

```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)
```

```java
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;
}
}
Expand All @@ -96,11 +95,10 @@ class Solution {
public:
int maximumCount(vector<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;
}
}
Expand All @@ -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++
}
}
Expand All @@ -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>) -> 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);
}
```
Expand All @@ -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)$。
<!-- tabs:start -->
Expand Down Expand Up @@ -232,97 +232,71 @@ 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<i32>, target: i32) -> usize {
fn search(nums: &Vec<i32>, 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
}

pub fn maximum_count(nums: Vec<i32>) -> 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);
}
```
<!-- tabs:end -->
### 方法三
<!-- tabs:start -->
```rust
impl Solution {
pub fn maximum_count(nums: Vec<i32>) -> 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);
}
```
Expand Down
Loading

0 comments on commit 96852d1

Please sign in to comment.