목차
▸ 햄버거 만들기
▸ 성격 유형 검사하기
출처: 프로그래머스 코딩테스트 연습 > 연습문제 > 햄버거 만들기
class Solution {
public int solution(int[] ingredient) {
int answer = 0;
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < ingredient.length; i++){
stringBuilder.append(ingredient[i]);
}
String temp = stringBuilder.toString();
int before = temp.length();
for(int i = 0; i < temp.length(); i++){
temp = temp.replace("1231", "");
}
int after = temp.length();
answer = (before - after)/4;
return answer;
}
}
[수정 코드]
import java.util.List;
import java.util.ArrayList;
class Solution {
public int solution(int[] ingredient) {
int answer = 0;
List<Integer> ingredientList = new ArrayList<>();
for(int i : ingredient){
ingredientList.add(i);
if(ingredientList.size() >= 4){
if(ingredientList.get(ingredientList.size()-4) == 1 && ingredientList.get(ingredientList.size()-3) == 2 && ingredientList.get(ingredientList.size()-2) == 3 && ingredientList.get(ingredientList.size()-1) == 1){
answer ++;
ingredientList.remove(ingredientList.size()-4);
ingredientList.remove(ingredientList.size()-3);
ingredientList.remove(ingredientList.size()-2);
ingredientList.remove(ingredientList.size()-1);
}
}
}
return answer;
}
}
출처: 프로그래머스 코딩테스트 연습 > 2022 KAKAO TECH INTERNSHIP > 성격 유형 검사하기
import java.util.Map;
import java.util.HashMap;
import java.util.Arrays;
class Solution {
public String solution(String[] survey, int[] choices) {
String answer = "";
Map<Character, Integer> map = new HashMap<>();
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < survey.length; i++){
int choice = choices[i];
if(choice > 0 && choice < 4){
char c = survey[i].charAt(0);
map.put(c, map.getOrDefault(c, 0) + 4 - choice);
}else if(choice > 4){
char c = survey[i].charAt(1);
map.put(c, map.getOrDefault(c, 0) + choice - 4);
}
}
stringBuilder.append(map.getOrDefault('R', 0) >= map.getOrDefault('T', 0) ? 'R' : 'T');
stringBuilder.append(map.getOrDefault('C', 0) >= map.getOrDefault('F', 0) ? 'C' : 'F');
stringBuilder.append(map.getOrDefault('J', 0) >= map.getOrDefault('M', 0) ? 'J' : 'M');
stringBuilder.append(map.getOrDefault('A', 0) >= map.getOrDefault('N', 0) ? 'A' : 'N');
answer = stringBuilder.toString();
return answer;
}
}