Skip to content

Commit a70e1e0

Browse files
committed
feat: update solutions for lc No.2531
1 parent fe39965 commit a70e1e0

4 files changed

Lines changed: 22 additions & 47 deletions

File tree

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -276,20 +276,8 @@ func maximumCount(nums []int) int {
276276

277277
```ts
278278
function maximumCount(nums: number[]): number {
279-
const search = (x: number): number => {
280-
let [left, right] = [0, nums.length];
281-
while (left < right) {
282-
const mid = (left + right) >> 1;
283-
if (nums[mid] >= x) {
284-
right = mid;
285-
} else {
286-
left = mid + 1;
287-
}
288-
}
289-
return left;
290-
};
291-
const i = search(1);
292-
const j = search(0);
279+
const i = _.sortedLastIndex(nums, 0);
280+
const j = _.sortedIndex(nums, 0);
293281
const [a, b] = [nums.length - i, j];
294282
return Math.max(a, b);
295283
}

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,20 +273,8 @@ func maximumCount(nums []int) int {
273273

274274
```ts
275275
function maximumCount(nums: number[]): number {
276-
const search = (x: number): number => {
277-
let [left, right] = [0, nums.length];
278-
while (left < right) {
279-
const mid = (left + right) >> 1;
280-
if (nums[mid] >= x) {
281-
right = mid;
282-
} else {
283-
left = mid + 1;
284-
}
285-
}
286-
return left;
287-
};
288-
const i = search(1);
289-
const j = search(0);
276+
const i = _.sortedLastIndex(nums, 0);
277+
const j = _.sortedIndex(nums, 0);
290278
const [a, b] = [nums.length - i, j];
291279
return Math.max(a, b);
292280
}
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
class Solution {
22
public int maximumCount(int[] nums) {
3-
int i = Arrays.binarySearch(nums, 1);
4-
int a = i < 0 ? nums.length - (-i - 1) : nums.length - i;
5-
int j = Arrays.binarySearch(nums, 0);
6-
int b = j < 0 ? -j - 1 : j;
3+
int a = nums.length - search(nums, 1);
4+
int b = search(nums, 0);
75
return Math.max(a, b);
86
}
9-
}
7+
8+
private int search(int[] nums, int x) {
9+
int left = 0, right = nums.length;
10+
while (left < right) {
11+
int mid = (left + right) >> 1;
12+
if (nums[mid] >= x) {
13+
right = mid;
14+
} else {
15+
left = mid + 1;
16+
}
17+
}
18+
return left;
19+
}
20+
}
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
function maximumCount(nums: number[]): number {
2-
const search = (x: number): number => {
3-
let [left, right] = [0, nums.length];
4-
while (left < right) {
5-
const mid = (left + right) >> 1;
6-
if (nums[mid] >= x) {
7-
right = mid;
8-
} else {
9-
left = mid + 1;
10-
}
11-
}
12-
return left;
13-
};
14-
const i = search(1);
15-
const j = search(0);
2+
const i = _.sortedLastIndex(nums, 0);
3+
const j = _.sortedIndex(nums, 0);
164
const [a, b] = [nums.length - i, j];
175
return Math.max(a, b);
186
}

0 commit comments

Comments
 (0)