날 마다 물건을 한 품목 씩 할인을 한다 (1개밖에 못삼)
주인공은 자기가 사고픈거를 죄다 할인하는걸로 사고 싶고 연속적으로 사고싶음
이렇게 물건을 살수있는 날의 수를 구하여라
맵 두개와 투 포인터를 활용했다
result맵은 주인공이 살 것들
window맵은 날짜마다 변경되는 살 것들
while문을 돌면서 위 두 개의 맵을 비교해주면서 counting한다
import java.util.*;
class Solution {
public int solution(String[] want, int[] number, String[] discount) {
int answer = 0;
Map<String, Integer> result = new HashMap<>();
Map<String, Integer> window = new HashMap<>();
int start = 0;
int end = 9;
for(int i = 0; i < want.length; i++){
result.put(want[i], number[i]);
}
for(int i = 0 ; i < 10; i++){
window.put(discount[i], window.getOrDefault(discount[i], 0) + 1);
}
int leng = discount.length;
while(true){
if(check(result, window)){
answer++;
}
if(end + 1 == leng) break;
window.put(discount[start], window.get(discount[start++]) - 1);
window.put(discount[++end], window.getOrDefault(discount[end], 0) + 1);
}
return answer;
}
private static boolean check(Map<String, Integer> a, Map<String, Integer> b){
for(String key : a.keySet()){
if(!b.containsKey(key) || b.get(key) != a.get(key)){
return false;
}
}
return true;
}
}
투 포인터는 범위 지정해주는게 어렵다. 더 많이 풀어봐야겠다..!