728x90
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public int solution(int[] A, int[] B) { // write your code in Java SE 8 Stack<Integer> st = new Stack<>(); for(int i=A.length-1; i>=0 ; i--){ if(st.isEmpty()) st.push(i); else { while(true){ if( B[st.peek()] == 0 && B[i] ==1 ){ if(A[i]>A[st.peek()]) st.pop(); else break; } else { st.push(i); break; } if(st.isEmpty()) { st.push(i); break; } } } } return st.size(); } | cs |
'알고리즘 풀이 > Codility' 카테고리의 다른 글
Codility - StoneWall (Stacks and Queues) (0) | 2019.12.13 |
---|---|
Codility - Nesting (Stacks and Queues) (0) | 2019.12.13 |
Codility - Brackets (Stacks and Queues) (0) | 2019.12.13 |
Codility - Triangle (Sorting) (0) | 2019.12.13 |
Codility - Distinct (Sorting) (0) | 2019.12.13 |