반응형
문제
- 링크: https://school.programmers.co.kr/learn/courses/30/lessons/132265?language=java
풀이
더보기
import java.util.*;
class Solution {
public int solution(int[] topping) {
int answer = 0;
HashMap<Integer, Integer> toppingCount = new HashMap<>();
for (int i = 0; i < topping.length; ++i) {
toppingCount.put(topping[i], toppingCount.getOrDefault(topping[i], 0) + 1);
}
HashSet<Integer> toppingChulsu = new HashSet<>();
for (int i = 0; i < topping.length; ++i) {
toppingChulsu.add(topping[i]);
toppingCount.put(topping[i], toppingCount.getOrDefault(topping[i], 0) - 1);
if (toppingCount.get(topping[i]) == 0) {
toppingCount.remove(topping[i]);
}
if (toppingChulsu.size() == toppingCount.size()) {
answer += 1;
}
}
return answer;
}
}
반응형
'코딩테스트 문제집 > Programmers(Lv2)' 카테고리의 다른 글
[Programmers] Lv2: 이진 변환 반복하기(70129) (0) | 2025.02.12 |
---|---|
[Programmers] Lv2: 카펫(42842) (0) | 2025.02.12 |
[Programmers] Lv2: 점프와 순간 이동(12980) (0) | 2025.02.12 |
[Programmers] Lv2: 2 x n 타일링(12900) (0) | 2025.02.12 |
[Programmers] Lv2: 피보나치 수(12945) (0) | 2025.02.12 |