- 난이도: Lv1
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/133502
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/1/133502. 햄버거 만들기
풀이 시간 : 15분
import java.util.*;
class Solution {
public int solution(int[] ingredient) {
int answer = 0;
ArrayList<Integer> hamburger = new ArrayList<>();
for (int i = 0; i < ingredient.length; i++) {
hamburger.add(ingredient[i]);
if (isHamburger(hamburger)) {
answer++;
for(int j=0; j<4; j++){
hamburger.remove(hamburger.size()-1);
}
}
}
return answer;
}
public boolean isHamburger(ArrayList<Integer> hamburger) {
int size = hamburger.size();
return size >= 4 && hamburger.get(size - 4) == 1 && hamburger.get(size - 3) == 2 && hamburger.get(size - 2) == 3 && hamburger.get(size - 1) == 1;
}
}
import java.util.*;
class Solution {
public int solution(int[] ingredient) {
int answer = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ingredient.length; i++) {
sb.append(ingredient[i]);
if (sb.length() > 3 && sb.substring(sb.length() - 4, sb.length()).equals("1231")) {
answer++;
sb.delete(sb.length() - 4, sb.length());
}
}
return answer;
}
}