프로그래머스 롤케이크 자르기 Java 풀이에 대해 알아보겠습니다.
프로그래머스 – 롤케이크 자르기 경로
문제 설명 및 제한사항

롤케이크 자르기 Java 풀이
처음에 for문 2개에 HashSet으로 했다가 시간 초과가 나서, HashMap으로 for문 1개 만 사용해서 진행했습니다.

아래 주석들 읽어보시면 이해가 되실겁니다.
import java.util.*;
class Solution {
public int solution(int[] topping) {
int answer = 0;
// 동생, 형 HashMap
Map<Integer, Integer> hm1 = new HashMap<>();
Map<Integer, Integer> hm2 = new HashMap<>();
// HashMap 1개는 전부 삽입
for(int x : topping)
hm1.put(x, hm1.getOrDefault(x, 0) + 1);
// 나머지 HashMap에 1개씩 삽입하며, 이전의 HashMap에 해당 요소 1개씩 제거
for(int x : topping) {
hm2.put(x, hm2.getOrDefault(x, 0) + 1);
hm1.put(x, hm1.get(x) - 1);
if(hm1.get(x) == 0)
hm1.remove(x);
// 사이즈 같으면, 종류 수가 같은 거. answer++;
if(hm1.size() == hm2.size())
answer++;
}
return answer;
}
}
결과

답글 남기기