알고리즘 풀이/Codility
Codility - Fish (Stacks and Queues)
배게
2019. 12. 13. 15:13
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 |