Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
Last active August 29, 2015 14:19
Show Gist options
  • Save stephen-maina/30f598438e063f39a487 to your computer and use it in GitHub Desktop.
Save stephen-maina/30f598438e063f39a487 to your computer and use it in GitHub Desktop.
researched the counting bit online
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.Stack;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
Stack<Integer> stack=new Stack<Integer>();
Stack<Integer> equi=new Stack<Integer>();
for(int index=0;index<A.length;index++){
int i=index;
if((index%2==0||index>1)&&A.length-1-index>2){
equi.push(index);
}
if(stack.empty()){
stack.push(A[index]);
}
if(!stack.empty()&&stack.peek()!=A[index]){
stack.pop();
}else{
stack.push(A[index]);
}
}
int size=0;
for(int index=0;index<A.length;index++){
if(stack.peek()==A[index]){
size++;
}
}
int count=0;
if (size <= A.length / 2) return 0;
int ans = 0;
for (int i = 0; i < A.length; i++) {
if (A[i] == stack.peek()) {
count++;
}
if (count * 2 > i + 1 && (size - count) * 2 > A.length - i - 1) {
ans++;
}
}
return ans;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment