Created
April 19, 2015 18:44
-
-
Save stephen-maina/637822a35881e22e1cb8 to your computer and use it in GitHub Desktop.
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
import java.util.Stack; | |
class Solution { | |
public int solution(int[] H) { | |
// write your code in Java SE 8 | |
Stack<Integer> wall=new Stack<Integer>(); | |
int blocks=0; | |
for(int index=0;index<H.length;index++){ | |
while(!wall.empty()&&H[index]<wall.peek()){ | |
wall.pop(); | |
if(!wall.empty()&&H[index]>wall.peek()){ | |
wall.push(H[index]); | |
blocks++; | |
} | |
} | |
if(wall.empty()){ | |
wall.push(H[index]); | |
blocks++; | |
} | |
if(!wall.empty()&&H[index]>wall.peek()){ | |
wall.push(H[index]); | |
blocks++; | |
} | |
} | |
return blocks; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment