map 자료구조를 활용헤 문제를 풀이 하였다.
10일 을 기준으로 10일 이전은 그냥 더하고 11일 부터는 10일 전 즉 1일날의 discount 항목을 제거하면서 마치 sliding window 처럼 생각해서 풀이 하였다.
그리고 각 날짜 마다 조건을 만족하는지 확인하였다.
import java.util.*;
class Solution {
public int solution(String[] want, int[] number, String[] discount) {
int answer = 0;
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i< discount.length; i++) {
map.put(discount[i], map.getOrDefault(discount[i], 0) + 1);
if (i >= 10)
map.put(discount[i - 10], map.getOrDefault(discount[i - 10], 0) - 1);
boolean isPossible = true;
for (int j = 0; j < want.length; j++) {
if (map.getOrDefault(want[j], 0) < number[j]) {
isPossible = false;
break;
}
}
if (isPossible) {
answer++;
}
}
return answer;
}
}