본문 바로가기

IT/알고리즘

[코딜리티] - Lesson 7.StoneWall

안녕하세요 남갯입니다.




public static int solution(int[] H) {
// write your code in Java SE 8
int result = 0;
Stack<Integer> stack = new Stack<>();
stack.push(H[0]);
result ++;
for (int i = 0; i < H.length; i++) {
if (H[i] > stack.peek()) {
stack.push(H[i]);
result++;
} else if (H[i] < stack.peek()) {
while (!stack.empty() && H[i] < stack.peek()) {
stack.pop();
}
if(!stack.empty() && stack.peek() == H[i]){

}else{
result++;
}
stack.push(H[i]);
}
}

return result;
}



https://app.codility.com/demo/results/trainingCT2CYU-NAM/

'IT > 알고리즘' 카테고리의 다른 글

[코딜리티] - Lesson 9.MaxProfit  (0) 2019.09.16
[코딜리티] - Lesson 8.Dominator  (0) 2019.09.16
[코딜리티] - Lesson 7. Nesting  (0) 2019.09.09
[코딜리티] - Lesson 7. Fish  (0) 2019.09.09
[코딜리티] - Lesson 7. Brackets  (0) 2019.09.03