Skip to content

Commit 7449cf4

Browse files
committed
feat: add solutions to lc problem: No.2529
No.2529.Maximum Count of Positive Integer and Negative Integer
1 parent 4b810c0 commit 7449cf4

File tree

5 files changed

+308
-0
lines changed

5 files changed

+308
-0
lines changed

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

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,129 @@ func max(a, b int) int {
206206
}
207207
```
208208

209+
### **TypeScript**
210+
211+
```ts
212+
function maximumCount(nums: number[]): number {
213+
const count = [0, 0];
214+
for (const num of nums) {
215+
if (num < 0) {
216+
count[0]++;
217+
} else if (num > 0) {
218+
count[1]++;
219+
}
220+
}
221+
return Math.max(...count);
222+
}
223+
```
224+
225+
```ts
226+
function maximumCount(nums: number[]): number {
227+
const search = (target: number) => {
228+
let left = 0;
229+
let right = n;
230+
while (left < right) {
231+
const mid = (left + right) >>> 1;
232+
if (nums[mid] < target) {
233+
left = mid + 1;
234+
} else {
235+
right = mid;
236+
}
237+
}
238+
return left;
239+
};
240+
const n = nums.length;
241+
const i = search(0);
242+
const j = search(1);
243+
return Math.max(i, n - j);
244+
}
245+
```
246+
247+
### **Rust**
248+
249+
```rust
250+
impl Solution {
251+
pub fn maximum_count(nums: Vec<i32>) -> i32 {
252+
let mut count = [0, 0];
253+
for &num in nums.iter() {
254+
if num < 0 {
255+
count[0] += 1;
256+
} else if num > 0 {
257+
count[1] += 1;
258+
}
259+
}
260+
*count.iter().max().unwrap()
261+
}
262+
}
263+
```
264+
265+
```rust
266+
impl Solution {
267+
fn search(nums: &Vec<i32>, target: i32) -> usize {
268+
let mut left = 0;
269+
let mut right = nums.len();
270+
while left < right {
271+
let mid = (left + right) >> 1;
272+
if nums[mid] < target {
273+
left = mid + 1;
274+
} else {
275+
right = mid;
276+
}
277+
}
278+
left
279+
}
280+
281+
pub fn maximum_count(nums: Vec<i32>) -> i32 {
282+
let n = nums.len();
283+
let i = Self::search(&nums, 0);
284+
let j = Self::search(&nums, 1);
285+
i.max(n - j) as i32
286+
}
287+
}
288+
```
289+
290+
### **C**
291+
292+
```c
293+
#define max(a, b) (((a) > (b)) ? (a) : (b))
294+
295+
int maximumCount(int *nums, int numsSize) {
296+
int count[2] = {0};
297+
for (int i = 0; i < numsSize; i++) {
298+
if (nums[i] < 0) {
299+
count[0]++;
300+
} else if (nums[i] > 0) {
301+
count[1]++;
302+
}
303+
}
304+
return max(count[0], count[1]);
305+
}
306+
```
307+
308+
```c
309+
#define max(a, b) (((a) > (b)) ? (a) : (b))
310+
311+
int search(int *nums, int numsSize, int target) {
312+
int left = 0;
313+
int right = numsSize;
314+
while (left < right) {
315+
int mid = (left + right) >> 1;
316+
if (nums[mid] < target) {
317+
left = mid + 1;
318+
} else {
319+
right = mid;
320+
}
321+
}
322+
return left;
323+
}
324+
325+
int maximumCount(int *nums, int numsSize) {
326+
int i = search(nums, numsSize, 0);
327+
int j = search(nums, numsSize, 1);
328+
return max(i, numsSize - j);
329+
}
330+
```
331+
209332
### **...**
210333

211334
```

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

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,129 @@ func max(a, b int) int {
183183
}
184184
```
185185

186+
### **TypeScript**
187+
188+
```ts
189+
function maximumCount(nums: number[]): number {
190+
const count = [0, 0];
191+
for (const num of nums) {
192+
if (num < 0) {
193+
count[0]++;
194+
} else if (num > 0) {
195+
count[1]++;
196+
}
197+
}
198+
return Math.max(...count);
199+
}
200+
```
201+
202+
```ts
203+
function maximumCount(nums: number[]): number {
204+
const search = (target: number) => {
205+
let left = 0;
206+
let right = n;
207+
while (left < right) {
208+
const mid = (left + right) >>> 1;
209+
if (nums[mid] < target) {
210+
left = mid + 1;
211+
} else {
212+
right = mid;
213+
}
214+
}
215+
return left;
216+
};
217+
const n = nums.length;
218+
const i = search(0);
219+
const j = search(1);
220+
return Math.max(i, n - j);
221+
}
222+
```
223+
224+
### **Rust**
225+
226+
```rust
227+
impl Solution {
228+
pub fn maximum_count(nums: Vec<i32>) -> i32 {
229+
let mut count = [0, 0];
230+
for &num in nums.iter() {
231+
if num < 0 {
232+
count[0] += 1;
233+
} else if num > 0 {
234+
count[1] += 1;
235+
}
236+
}
237+
*count.iter().max().unwrap()
238+
}
239+
}
240+
```
241+
242+
```rust
243+
impl Solution {
244+
fn search(nums: &Vec<i32>, target: i32) -> usize {
245+
let mut left = 0;
246+
let mut right = nums.len();
247+
while left < right {
248+
let mid = (left + right) >> 1;
249+
if nums[mid] < target {
250+
left = mid + 1;
251+
} else {
252+
right = mid;
253+
}
254+
}
255+
left
256+
}
257+
258+
pub fn maximum_count(nums: Vec<i32>) -> i32 {
259+
let n = nums.len();
260+
let i = Self::search(&nums, 0);
261+
let j = Self::search(&nums, 1);
262+
i.max(n - j) as i32
263+
}
264+
}
265+
```
266+
267+
### **C**
268+
269+
```c
270+
#define max(a, b) (((a) > (b)) ? (a) : (b))
271+
272+
int maximumCount(int *nums, int numsSize) {
273+
int count[2] = {0};
274+
for (int i = 0; i < numsSize; i++) {
275+
if (nums[i] < 0) {
276+
count[0]++;
277+
} else if (nums[i] > 0) {
278+
count[1]++;
279+
}
280+
}
281+
return max(count[0], count[1]);
282+
}
283+
```
284+
285+
```c
286+
#define max(a, b) (((a) > (b)) ? (a) : (b))
287+
288+
int search(int *nums, int numsSize, int target) {
289+
int left = 0;
290+
int right = numsSize;
291+
while (left < right) {
292+
int mid = (left + right) >> 1;
293+
if (nums[mid] < target) {
294+
left = mid + 1;
295+
} else {
296+
right = mid;
297+
}
298+
}
299+
return left;
300+
}
301+
302+
int maximumCount(int *nums, int numsSize) {
303+
int i = search(nums, numsSize, 0);
304+
int j = search(nums, numsSize, 1);
305+
return max(i, numsSize - j);
306+
}
307+
```
308+
186309
### **...**
187310

188311
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#define max(a, b) (((a) > (b)) ? (a) : (b))
2+
3+
int search(int *nums, int numsSize, int target) {
4+
int left = 0;
5+
int right = numsSize;
6+
while (left < right) {
7+
int mid = (left + right) >> 1;
8+
if (nums[mid] < target) {
9+
left = mid + 1;
10+
} else {
11+
right = mid;
12+
}
13+
}
14+
return left;
15+
}
16+
17+
int maximumCount(int *nums, int numsSize) {
18+
int i = search(nums, numsSize, 0);
19+
int j = search(nums, numsSize, 1);
20+
return max(i, numsSize - j);
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
fn search(nums: &Vec<i32>, target: i32) -> usize {
3+
let mut left = 0;
4+
let mut right = nums.len();
5+
while left < right {
6+
let mid = (left + right) >> 1;
7+
if nums[mid] < target {
8+
left = mid + 1;
9+
} else {
10+
right = mid;
11+
}
12+
}
13+
left
14+
}
15+
16+
pub fn maximum_count(nums: Vec<i32>) -> i32 {
17+
let n = nums.len();
18+
let i = Self::search(&nums, 0);
19+
let j = Self::search(&nums, 1);
20+
i.max(n - j) as i32
21+
}
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function maximumCount(nums: number[]): number {
2+
const search = (target: number) => {
3+
let left = 0;
4+
let right = n;
5+
while (left < right) {
6+
const mid = (left + right) >>> 1;
7+
if (nums[mid] < target) {
8+
left = mid + 1;
9+
} else {
10+
right = mid;
11+
}
12+
}
13+
return left;
14+
};
15+
const n = nums.length;
16+
const i = search(0);
17+
const j = search(1);
18+
return Math.max(i, n - j);
19+
}

0 commit comments

Comments
 (0)