Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 96852d1

Browse files
feat: update solutions to lc problem: No.2529 (doocs#2553)
No.2529.Maximum Count of Positive Integer and Negative Integer
1 parent ea860e7 commit 96852d1

File tree

14 files changed

+203
-275
lines changed

14 files changed

+203
-275
lines changed

‎solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README.md‎

Lines changed: 65 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,28 @@
6060

6161
### 方法一:遍历
6262

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

65-
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 为数组长度
65+
时间复杂度 $O(n),ドル其中 $n$ 为数组长度。空间复杂度 $O(1)$。
6666

6767
<!-- tabs:start -->
6868

6969
```python
7070
class Solution:
7171
def maximumCount(self, nums: List[int]) -> int:
72-
a = sum(v > 0 for v in nums)
73-
b = sum(v < 0 for v in nums)
72+
a = sum(x > 0 for x in nums)
73+
b = sum(x < 0 for x in nums)
7474
return max(a, b)
7575
```
7676

7777
```java
7878
class Solution {
7979
public int maximumCount(int[] nums) {
8080
int a = 0, b = 0;
81-
for (int v : nums) {
82-
if (v > 0) {
81+
for (int x : nums) {
82+
if (x > 0) {
8383
++a;
84-
}
85-
if (v < 0) {
84+
} else if (x < 0) {
8685
++b;
8786
}
8887
}
@@ -96,11 +95,10 @@ class Solution {
9695
public:
9796
int maximumCount(vector<int>& nums) {
9897
int a = 0, b = 0;
99-
for (int& v : nums) {
100-
if (v > 0) {
98+
for (int x : nums) {
99+
if (x > 0) {
101100
++a;
102-
}
103-
if (v < 0) {
101+
} else if (x < 0) {
104102
++b;
105103
}
106104
}
@@ -111,12 +109,11 @@ public:
111109
112110
```go
113111
func maximumCount(nums []int) int {
114-
a, b := 0, 0
115-
for _, v := range nums {
116-
if v > 0 {
112+
var a, b int
113+
for _, x := range nums {
114+
if x > 0 {
117115
a++
118-
}
119-
if v < 0 {
116+
} else if x < 0 {
120117
b++
121118
}
122119
}
@@ -126,47 +123,50 @@ func maximumCount(nums []int) int {
126123

127124
```ts
128125
function maximumCount(nums: number[]): number {
129-
const count = [0, 0];
130-
for (const num of nums) {
131-
if (num< 0) {
132-
count[0]++;
133-
} else if (num> 0) {
134-
count[1]++;
126+
let [a, b] = [0, 0];
127+
for (const x of nums) {
128+
if (x> 0) {
129+
++a;
130+
} else if (x< 0) {
131+
++b;
135132
}
136133
}
137-
return Math.max(...count);
134+
return Math.max(a, b);
138135
}
139136
```
140137

141138
```rust
142139
impl Solution {
143140
pub fn maximum_count(nums: Vec<i32>) -> i32 {
144-
let mut count = [0, 0];
145-
for &num in nums.iter() {
146-
if num < 0 {
147-
count[0] += 1;
148-
} else if num > 0 {
149-
count[1] += 1;
141+
let mut a = 0;
142+
let mut b = 0;
143+
144+
for x in nums {
145+
if x > 0 {
146+
a += 1;
147+
} else if x < 0 {
148+
b += 1;
150149
}
151150
}
152-
*count.iter().max().unwrap()
151+
152+
std::cmp::max(a, b)
153153
}
154154
}
155155
```
156156

157157
```c
158-
#define max(a, b) (((a) > (b)) ? (a) : (b))
158+
#define max(a, b) (a > b ? a : b)
159159

160160
int maximumCount(int* nums, int numsSize) {
161-
int count[2] = {0};
162-
for (int i = 0; i < numsSize; i++) {
163-
if (nums[i] < 0) {
164-
count[0]++;
165-
} else if (nums[i] > 0) {
166-
count[1]++;
161+
int a = 0, b = 0;
162+
for (int i = 0; i < numsSize; ++i) {
163+
if (nums[i] > 0) {
164+
++a;
165+
} else if (nums[i] < 0) {
166+
++b;
167167
}
168168
}
169-
return max(count[0], count[1]);
169+
return max(a, b);
170170
}
171171
```
172172
@@ -176,7 +176,7 @@ int maximumCount(int* nums, int numsSize) {
176176
177177
由于数组是按非递减顺序排列的,因此可以使用二分查找找到第一个大于等于 1ドル$ 的元素的下标 $i$ 以及第一个大于等于 0ドル$ 的元素的下标 $j,ドル那么正整数的个数 $a = n - i,ドル负整数的个数 $b = j,ドル返回 $a$ 和 $b$ 中的较大值即可。
178178
179-
时间复杂度 $O(\log n),ドル空间复杂度 $O(1)$。其中 $n$ 为数组长度
179+
时间复杂度 $O(\log n),ドル其中 $n$ 为数组长度。空间复杂度 $O(1)$。
180180
181181
<!-- tabs:start -->
182182
@@ -232,97 +232,71 @@ func maximumCount(nums []int) int {
232232

233233
```ts
234234
function maximumCount(nums: number[]): number {
235-
const search = (target: number) => {
236-
let left = 0;
237-
let right = n;
235+
const search = (x: number): number => {
236+
let [left, right] = [0, nums.length];
238237
while (left < right) {
239-
const mid = (left + right) >>> 1;
240-
if (nums[mid] < target) {
241-
left = mid + 1;
242-
} else {
238+
const mid = (left + right) >> 1;
239+
if (nums[mid] >= x) {
243240
right = mid;
241+
} else {
242+
left = mid + 1;
244243
}
245244
}
246245
return left;
247246
};
248-
const n = nums.length;
249-
const i = search(0);
250-
const j =search(1);
251-
return Math.max(i, n-j);
247+
const i = search(1);
248+
const j = search(0);
249+
const [a, b] = [nums.length-i, j];
250+
return Math.max(a, b);
252251
}
253252
```
254253

255254
```rust
256255
impl Solution {
257-
fn search(nums: &Vec<i32>, target: i32) -> usize {
256+
fn search(nums: &Vec<i32>, x: i32) -> usize {
258257
let mut left = 0;
259258
let mut right = nums.len();
260259
while left < right {
261260
let mid = (left + right) >> 1;
262-
if nums[mid] < target {
263-
left = mid + 1;
264-
} else {
261+
if nums[mid] >= x {
265262
right = mid;
263+
} else {
264+
left = mid + 1;
266265
}
267266
}
268267
left
269268
}
270269

271270
pub fn maximum_count(nums: Vec<i32>) -> i32 {
272271
let n = nums.len();
273-
let i = Self::search(&nums, 0);
274-
let j = Self::search(&nums, 1);
275-
i.max(n - j) as i32
272+
let i = Self::search(&nums, 1);
273+
let j = Self::search(&nums, 0);
274+
(n - i).max(j) as i32
276275
}
277276
}
278277
```
279278

280279
```c
281-
#define max(a, b) (((a) > (b)) ? (a) : (b))
280+
#define max(a, b) (a > b ? a : b)
282281

283-
int search(int* nums, int numsSize, int target) {
282+
int search(int* nums, int numsSize, int x) {
284283
int left = 0;
285284
int right = numsSize;
286285
while (left < right) {
287286
int mid = (left + right) >> 1;
288-
if (nums[mid] < target) {
289-
left = mid + 1;
290-
} else {
287+
if (nums[mid] >= x) {
291288
right = mid;
289+
} else {
290+
left = mid + 1;
292291
}
293292
}
294293
return left;
295294
}
296295

297296
int maximumCount(int* nums, int numsSize) {
298-
int i = search(nums, numsSize, 0);
299-
int j = search(nums, numsSize, 1);
300-
return max(i, numsSize - j);
301-
}
302-
```
303-
304-
<!-- tabs:end -->
305-
306-
### 方法三
307-
308-
<!-- tabs:start -->
309-
310-
```rust
311-
impl Solution {
312-
pub fn maximum_count(nums: Vec<i32>) -> i32 {
313-
let mut a = 0;
314-
let mut b = 0;
315-
316-
for n in nums {
317-
if n > 0 {
318-
a += 1;
319-
} else if n < 0 {
320-
b += 1;
321-
}
322-
}
323-
324-
std::cmp::max(a, b)
325-
}
297+
int i = search(nums, numsSize, 1);
298+
int j = search(nums, numsSize, 0);
299+
return max(numsSize - i, j);
326300
}
327301
```
328302

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /