Skip to content

Commit 6b0e358

Browse files
committed
minimum index of a valid split
1 parent 1bdfe4c commit 6b0e358

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package solutions;
2+
3+
import java.util.List;
4+
5+
public class minimum_index_of_a_valid_split {
6+
public int minimumIndex(List<Integer> nums) {
7+
int majority = 0;
8+
int count = 0;
9+
for(int i: nums) {
10+
if(i == majority) count++;
11+
else if(count == 0) { majority = i; count = 1; }
12+
else count--;
13+
}
14+
15+
count = 0;
16+
for(int i: nums) if(i == majority) count++;
17+
18+
// System.out.println(majority + " " + count);
19+
int leftCount = 0;
20+
for(int i = 0; i < nums.size() - 1; i++) {
21+
if(nums.get(i) == majority) leftCount++;
22+
// System.out.println(leftCount + " " + i + " " + count);
23+
// System.out.println(((double) leftCount) / (i + 1) + " " +
24+
// (double)(count - leftCount) / (nums.size() - i - 1));
25+
26+
if( ((double) leftCount) / (i + 1) > 0.5 &&
27+
(double)(count - leftCount) / (nums.size() - i - 1) > 0.5) return i;
28+
}
29+
30+
return -1;
31+
}
32+
}

0 commit comments

Comments
 (0)