<출처>
프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
https://school.programmers.co.kr/learn/courses/30/lessons/133502
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
<해결 방법 1>
ArrayList 사용하여 단순 반
public static int solution(int[] ingredient) {
int answer = 0;
int[] burger = new int[]{1, 2, 3, 1};
ArrayList<Integer> checkList = new ArrayList<>();
for (int i = 0; i < ingredient.length; i++) {
checkList.add(ingredient[i]);
if (checkList.size() >= 4) {
int[] temp = new int[4];
temp[0] = checkList.get(checkList.size() - 4);
temp[1] = checkList.get(checkList.size() - 3);
temp[2] = checkList.get(checkList.size() - 2);
temp[3] = checkList.get(checkList.size() - 1);
if (Arrays.equals(temp, burger)) {
answer++;
checkList.remove(checkList.size() - 1);
checkList.remove(checkList.size() - 1);
checkList.remove(checkList.size() - 1);
checkList.remove(checkList.size() - 1);
}
}
}
return answer;
}
<해결 방법 2>
public static int solution2(int[] ingredient) {
int answer = 0;
Stack<Integer> checkList = new Stack<>();
int[] burger = new int[]{1, 3, 2, 1};
for (int i = 0; i < ingredient.length; i++) {
checkList.push(ingredient[i]);
if (checkList.size() >= 4) {
ArrayList<Integer> temp = new ArrayList<>();
for(int p=0; p<4; p++) {
if(checkList.peek() != burger[p]){
break;
}
temp.add(checkList.pop());
}
if(temp.size()==4){
answer++;
}
else {
for(int j=temp.size()-1; j>=0; j--){
checkList.push(temp.get(j));
}
}
}
}
return answer;
}
'▶ 코테 준비 > 프로그래머스 문제풀이' 카테고리의 다른 글
[JAVA] 프로그래머스 - 우박수열 정적분 (0) | 2022.11.23 |
---|---|
[JAVA] 프로그래머스 - 콜라 문제 (0) | 2022.11.06 |
[JAVA] 프로그래머스 - 옹알이(2) (0) | 2022.11.06 |
[JAVA] 프로그래머스 - 푸드 파이트 대회 (0) | 2022.11.05 |