[프로그래머스] LEVEL2 할인 행사 JAVA

Pixel Dophin·2023년 7월 21일
0

프로그래머스

목록 보기
25/55

할인 행사

문제링크

풀이

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;
    }
}
profile
안녕 👋 성장하고픈 개발자 💻 입니다

0개의 댓글

관련 채용 정보