Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
Created April 19, 2015 18:44
Show Gist options
  • Save stephen-maina/637822a35881e22e1cb8 to your computer and use it in GitHub Desktop.
Save stephen-maina/637822a35881e22e1cb8 to your computer and use it in GitHub Desktop.
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