원본 : https://school.programmers.co.kr/learn/courses/30/lessons/132265
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
import java.util.*;
class Solution {
public int solution(int[] topping) {
int cnt = 0;
Set<Integer> set = new HashSet<>();
Map<Integer, Integer> map = new HashMap<>();
Queue<Integer> queue = new LinkedList<>();
for (int num : topping) {
queue.add(num);
if (map.containsKey(num)) {
map.replace(num, map.get(num) + 1);
} else {
map.put(num, 1);
}
}
while (!queue.isEmpty()) {
int num = queue.poll();
if (map.get(num) == 1) {
map.remove(num);
} else {
map.replace(num, map.get(num) - 1);
}
set.add(num);
if (set.size() == map.size()) {
cnt++;
}
}
return cnt;
}
}
혼자 놀기의 달인 (연습문제) (0) | 2022.10.23 |
---|---|
택배상자 (연습문제) (0) | 2022.10.23 |
콜라 문제 (연습문제) (0) | 2022.10.23 |
삼총사 (연습문제) (0) | 2022.10.23 |
연속 부분 수열 합의 개수 (연습문제) (0) | 2022.10.23 |
댓글 영역