Created
May 4, 2015 17:16
-
-
Save stephen-maina/da43dabcafcaa6f1e4dd to your computer and use it in GitHub Desktop.
not working only works for sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int solution(int[] A) { | |
// write your code in Java SE 8 | |
if(A.length<=2){ | |
return 0; | |
} | |
int []peak=getPeaks(A); | |
int blocks=0; | |
for(int blk=1;blk<A.length;blk++){ | |
int index=1; | |
int loop=1; | |
boolean check=true; | |
while(loop<=blk && check){ | |
if(loop>1){ | |
index=(A.length/blk)*loop; | |
} | |
if(A.length%blk!=0){ | |
break; | |
} | |
check=check(peak,index-1,index-1+(A.length/blk)); | |
loop+=1; | |
} | |
// if(!check){ | |
// loop-=1;} | |
if(loop-1!=blk){ | |
break; | |
} | |
blocks++; | |
} | |
return blocks; | |
} | |
public boolean check(int [] peak,int index,int max){ | |
for(;index<max;index++){ | |
if(peak[index]==1){ | |
return true; | |
} | |
if(peak[index]==-1){ | |
return false; | |
} | |
} | |
return false; | |
} | |
public int[] getPeaks(int[] A){ | |
int [] peak=new int[A.length]; | |
peak[A.length-1]=-1; | |
for(int index=1;index<A.length-1;index++){ | |
if(A[index]>Math.max(A[index-1],A[index+1])){ | |
peak[index]=1; | |
}else{ | |
peak[index]=0; | |
} | |
} | |
return peak; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment