본문 바로가기
알고리즘/프로그래머스

[프로그래머스] 할인 행사(Java)

by hxxyeoniii 2025. 1. 5.

링크 : https://school.programmers.co.kr/learn/courses/30/lessons/131127?language=java

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr


문제 풀이

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<want.length; i++) {
            map.put(want[i], number[i]);
        }
        
        Map<String, Integer> newMap;
        
        for(int i=0; i<=discount.length-10; i++) {
            newMap = new HashMap<>();
            newMap.putAll(map);
            
            for(int j=i; j<i+10; j++) {
                String str = discount[j];
                
                if(newMap.containsKey(str)) {
                    if(newMap.get(str) > 1) {
                        newMap.put(str, newMap.get(str) - 1);    
                    } else if(newMap.get(str) - 1 == 0) {
                        newMap.remove(str);
                    }
                }
            
                if(newMap.size() == 0) {
                    answer++;
                }
            }
        }
        return answer;                
    }
}