updated at 2022. 12. 8. 오후 5:38:38
This commit is contained in:
@@ -1,34 +1,20 @@
|
||||
// [문제 링크]: https://school.programmers.co.kr/learn/courses/30/lessons/133502
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class Solution {
|
||||
public int solution(int[] ingr) {
|
||||
public int solution(int[] ingredient) {
|
||||
int[] stack = new int[ingredient.length];
|
||||
int sp = 0;
|
||||
int answer = 0;
|
||||
ArrayList<Integer> list = new ArrayList<>();
|
||||
ArrayList<Integer> list2 = new ArrayList<>();
|
||||
list2.add(1);
|
||||
list2.add(2);
|
||||
list2.add(3);
|
||||
list2.add(1);
|
||||
for (int i = 0; i < ingr.length; i++){
|
||||
list.add(ingr[i]);
|
||||
if(list.size() >= 4){
|
||||
ArrayList<Integer> list3 = new ArrayList<>();
|
||||
list3.add(list.get(list.size()-4));
|
||||
list3.add(list.get(list.size()-3));
|
||||
list3.add(list.get(list.size()-2));
|
||||
list3.add(list.get(list.size()-1));
|
||||
if(list3.equals(list2)){
|
||||
for (int i : ingredient) {
|
||||
stack[sp++] = i;
|
||||
if (sp >= 4 && stack[sp - 1] == 1
|
||||
&& stack[sp - 2] == 3
|
||||
&& stack[sp - 3] == 2
|
||||
&& stack[sp - 4] == 1) {
|
||||
sp -= 4;
|
||||
answer++;
|
||||
list.remove(list.size()-1);
|
||||
list.remove(list.size()-1);
|
||||
list.remove(list.size()-1);
|
||||
list.remove(list.size()-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return answer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user