오늘은 여기까지

[프로그래머스] 할인 행사 - Java 자바 본문

Problem Solving

[프로그래머스] 할인 행사 - Java 자바

dev-99 2024. 10. 25. 09:30

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

 

프로그래머스

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

programmers.co.kr

 

해시 연습문제~

 

해시맵 하나에는 원래 구매 품목이랑 개수 정보를 담고, 다른 하나는 탐색하면서 개수 조절하는 용도이다.

 

 

전체코드

import java.util.*;

class Solution {
    public int solution(String[] want, int[] number, String[] discount) {
        int answer = 0;
        HashMap<String, Integer> map = new HashMap<>();
        for (int i = 0; i < want.length; i++) {
            map.put(want[i], number[i]);
        }
        
        for (int i = 0; i <= discount.length-10; i++) {
            HashMap<String, Integer> newMap = new HashMap<>();
            newMap.putAll(map);
            for (int j = i; j < i + 10; j++) {
                String product = discount[j];
                
                if (newMap.containsKey(product) && newMap.get(product) > 0) {
                    newMap.put(product, newMap.get(product) - 1);
                }
            }
            
            boolean flag = true;
            for (String key : newMap.keySet()) {
                if (newMap.get(key) != 0) flag = false;
            }
            if (flag) answer ++;
        }
        return answer;
    }
}