💻 문제 출처 : 프로그래머스_할인 행사
import java.util.*;
class Solution {
public int solution(String[] want, int[] number, String[] discount) {
int answer = 0;
Map<String, Integer> wantIndex = new HashMap<>();
int[] count = Arrays.copyOf(number, number.length);
for(int i = 0; i < want.length; i++) {
wantIndex.put(want[i], i);
}
for(int i = 0; i <= discount.length - 10; i++) {
boolean possible = true;
for(int j = i; j < i + 10; j++) {
Integer index = wantIndex.get(discount[j]);
if(index == null || count[index] == 0) {
possible = false;
break;
}
count[index]--;
}
for(int k : count) {
if(k != 0) possible = false;
}
if(possible) answer++;
count = Arrays.copyOf(number, number.length);
}
return answer;
}
}
import java.util.*;
class Solution {
public int solution(String[] want, int[] number, String[] discount) {
int answer = 0;
Map<String, Integer> wantIndex = new HashMap<>();
for (int i = 0; i < want.length; i++) {
wantIndex.put(want[i], i);
}
int[] windowCount = new int[want.length];
for (int i = 0; i < 10 && i < discount.length; i++) {
if (wantIndex.containsKey(discount[i])) {
windowCount[wantIndex.get(discount[i])]++;
}
}
if (isValid(windowCount, number)) {
answer++;
}
for (int i = 10; i < discount.length; i++) {
if (wantIndex.containsKey(discount[i - 10])) {
windowCount[wantIndex.get(discount[i - 10])]--;
}
if (wantIndex.containsKey(discount[i])) {
windowCount[wantIndex.get(discount[i])]++;
}
if (isValid(windowCount, number)) {
answer++;
}
}
return answer;
}
private boolean isValid(int[] windowCount, int[] number) {
for (int i = 0; i < number.length; i++) {
if (windowCount[i] < number[i]) {
return false;
}
}
return true;
}
}