할인 행상

심규원·2024년 8월 23일

https://school.programmers.co.kr/learn/courses/30/lessons/131127

import java.util.*;
class Solution {
    public int solution(String[] want, int[] number, String[] discount) {
        int answer = 0;
        
        Map<String, Integer> map =  new HashMap<>();
        int totalSize = 0;
        for(int i = 0; i < want.length; i++){
            map.put(want[i], number[i]);
            totalSize += number[i];
        }

        for(int i = 0; i <= discount.length - totalSize; i++){
            Map<String, Integer> tempMap = new HashMap<>(map);
            for(int j = i; j < totalSize + i; j++){
                if(tempMap.containsKey(discount[j])){
                    tempMap.put(discount[j], tempMap.get(discount[j]) - 1);
                    if(tempMap.get(discount[j]) == 0){
                        tempMap.remove(discount[j]);
                    }
                }
            }
            if(tempMap.isEmpty()) answer++;
            
        }
        
        return answer;
    }
}

어렵진 않다.
근데 할인 구매 가능한 가장 빠른 날인줄 알고 착각해서 시간 낭비..
실제로는 할인 구매 가능한 '모든' 날의 합이 답이다.

속도는 2중 For문이라 약간 느린편..

0개의 댓글