-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2529MaximumCountOfPositiveIntegerAndNegativeInteger.java
More file actions
67 lines (59 loc) · 2.45 KB
/
LC2529MaximumCountOfPositiveIntegerAndNegativeInteger.java
File metadata and controls
67 lines (59 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.nphausg.leetcode.easy;
import com.nphausg.leetcode.config.BaseTest;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
/**
* <a href="https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer">2529. Maximum Count of Positive Integer and Negative Integer</a>
*/
@RunWith(Enclosed.class)
public class LC2529MaximumCountOfPositiveIntegerAndNegativeInteger {
public static int maximumCount(int[] nums) {
int countPos = 0;
int countNeg = 0;
for (int num : nums) {
if (num > 0) {
countPos++;
}
if (num < 0) {
countNeg++;
}
}
return Math.max(countPos, countNeg);
}
// Method calculates the maximum count of either 0's or 1's in a sorted binary array
public int maximumCount2(int[] nums) {
// Find the number of 1's by subtracting the index of the first 1 from the array length
int countOfOnes = nums.length - firstOccurrence(nums, 1);
// Find the first occurrence index of 0, which is also the count of 0's
int countOfZeros = firstOccurrence(nums, 0);
// Return the max count between 0's and 1's
return Math.max(countOfOnes, countOfZeros);
}
// Helper method to find the first occurrence index of 'x' in the sorted array 'nums'
private int firstOccurrence(int[] nums, int x) {
int left = 0;
int right = nums.length;
// Binary search to find the first occurrence of 'x'
while (left < right) {
int mid = (left + right) >> 1; // Equivalent to (left + right) / 2 but faster
// If mid element is greater than or equal to x, we move the right boundary
if (nums[mid] >= x) {
right = mid;
} else {
// If mid element is less than x, we move the left boundary
left = mid + 1;
}
}
// 'left' will point to the first occurrence of 'x' or nums.length if 'x' is not found
return left;
}
public static class TestCase extends BaseTest {
@org.junit.Test
public void testCases() {
assertEquals(3, maximumCount(new int[]{-2, -1, -1, 1, 2, 3}));
assertEquals(3, maximumCount(new int[]{-3, -2, -1, 0, 0, 1, 2}));
assertEquals(4, maximumCount(new int[]{5, 20, 66, 1314}));
}
}
}