Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit dc24bca

Browse files
solves #2529: Maximum Count of Positive Integer and Negative Integer in java
1 parent 6c09db4 commit dc24bca

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@
793793
| 2515 | [Shortest Distance to Target String in a Circular Array](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array) | [![Java](assets/java.png)](src/ShortestDistanceToTargetStringInACircularArray.java) | |
794794
| 2520 | [Count the Digits That Divide a Number](https://leetcode.com/problems/count-the-digits-that-divide-a-number) | [![Java](assets/java.png)](src/CountTheDigitsThatDivideANumber.java) | |
795795
| 2525 | [Categorize Box According to Criteria](https://leetcode.com/problems/categorize-box-according-to-criteria) | [![Java](assets/java.png)](src/CategorizeBoxAccordingToCriteria.java) | |
796-
| 2529 | [Maximum Count of Positive Integer and Negative Integer](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer) | | |
796+
| 2529 | [Maximum Count of Positive Integer and Negative Integer](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer) | [![Java](assets/java.png)](src/MaximumCountOfPositiveIntegerAndNegativeInteger.java) | |
797797
| 2535 | [Difference Between Element Sum and Digit Sum of an Array](https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array) | | |
798798
| 2540 | [Minimum Common Value](https://leetcode.com/problems/minimum-common-value) | | |
799799
| 2544 | [Alternating Digit Sum](https://leetcode.com/problems/alternating-digit-sum) | | |
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer
2+
// T: O(log(N))
3+
// S: O(1)
4+
5+
public class MaximumCountOfPositiveIntegerAndNegativeInteger {
6+
public int maximumCount(int[] nums) {
7+
int firstIndex = binarySearchFirstIndex(nums, 0);
8+
int lastIndex = binarySearchLastIndex(nums, 0);
9+
return Math.max(firstIndex, nums.length - lastIndex - (lastIndex < nums.length && nums[lastIndex] == 0 ? 1 : 0));
10+
}
11+
12+
// left leaning binary search with insertion point
13+
private int binarySearchFirstIndex(int[] array, int x) {
14+
int left = 0, right = array.length - 1, middle, index = -1;
15+
while (left <= right) {
16+
middle = left + (right - left) / 2;
17+
if (array[middle] == x) {
18+
index = middle;
19+
right = middle - 1;
20+
} else if (array[middle] > x) right = middle - 1;
21+
else left = middle + 1;
22+
}
23+
return index == -1 ? left : index;
24+
}
25+
26+
// right leaning binary search with insertion point
27+
private int binarySearchLastIndex(int[] array, int x) {
28+
int left = 0, right = array.length - 1, middle, index = -1;
29+
while (left <= right) {
30+
middle = left + (right - left) / 2;
31+
if (array[middle] == x) {
32+
index = middle;
33+
left = middle + 1;
34+
} else if (array[middle] > x) right = middle - 1;
35+
else left = middle + 1;
36+
}
37+
return index == -1 ? left : index;
38+
}
39+
}

0 commit comments

Comments
 (0)