[Version]
⦁ 2024.04.07 / [Algorithm / Programmers] 할인 행사
import java.util.*;
class Solution {
public int solution(String[] wantedItems, int[] wantedQuantities, String[] discounts) {
int answer = 0;
int totalCount = Arrays.stream(wantedQuantities).sum();
Map<String, Integer> wantedMap = createMap(wantedItems, wantedQuantities);
for (int i = 0; i <= discounts.length - totalCount; i++) {
Map<String, Integer> currentMap = new HashMap<>(wantedMap);
for (int j = i; j < i + totalCount; j++) {
String discountedItem = discounts[j];
if (!currentMap.containsKey(discountedItem)) {
break;
}
int count = currentMap.get(discountedItem);
if (count <= 0) {
break;
}
currentMap.put(discountedItem, count - 1);
}
if (isAllSold(currentMap)) {
answer++;
}
}
return answer;
}
private Map<String, Integer> createMap(String[] keys, int[] values) {
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < keys.length; i++) {
map.put(keys[i], values[i]);
}
return map;
}
public static boolean isAllSold(Map<String, Integer> map) {
return map.values().stream().allMatch(count -> count == 0);
}
}
'Algorithm > 프로그래머스' 카테고리의 다른 글
[Algorithm / Programmers] 완전탐색-피로도 (0) | 2024.04.08 |
---|---|
[Algorithm / Programmers] 2019 카카오 개발자 겨울 인턴십-튜플 (0) | 2024.04.08 |
[Algorithm / Programmers] 연속 부분 수열 합의 개수 (0) | 2024.04.07 |
[Algorithm / Programmers] 2018 KAKAO BLIND RECRUITMENT-[1차] 캐시 (0) | 2024.04.07 |
[Algorithm / Programmers] 2017 팁스타운-짝지어 제거하기 (0) | 2024.04.04 |