Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
Created May 4, 2015 21:05
Show Gist options
  • Save stephen-maina/05717265a838b1bc84ce to your computer and use it in GitHub Desktop.
Save stephen-maina/05717265a838b1bc84ce to your computer and use it in GitHub Desktop.
still not fully working 40%
import java.util.ArrayList;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length<=2){
return 0;
}
ArrayList<Integer>peak=getPeaksIndex(A);
if(peak.size()==0){
return 0;
}
if(peak.size()==1){
return 1;
}
int flags=0;
int flagDistance=peak.size();
for(int loop=peak.size()-1;loop>=0;loop--){
int index=peak.size()-1;
int foundflags=0;
int check=1;
int len=0;
boolean found=false;
while(check<peak.size() && index>=1){
if(peak.get(index)-peak.get(index-check)>=flagDistance){
found=true;
}else{
found=false;
flagDistance--;
// foundflags=0;
// break;
}
check++;
// flags=index+1;
if(found){
foundflags++;
check=1;
index=index-check;
}
if(check>index){
break;
}
if(foundflags+1==flagDistance&&found ){
return flagDistance;
}
// if(index==1&&foundflags!=flagDistance){
// flagDistance--;
// }
}
}
return flagDistance;
}
public ArrayList<Integer> getPeaksIndex(int[] A){
ArrayList<Integer> peak=new ArrayList<Integer>();
for(int index=1;index<A.length-1;index++){
if(A[index]>Math.max(A[index-1],A[index+1])){
peak.add(index);
}
}
return peak;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment