1 minute read



문제 풀이




작성 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.*;

class Solution {
    public int solution(String[] want, int[] number, String[] discount) {
        int answer = 0;

        Map<String, Integer> org = new HashMap<>();
        for(int i=0; i<want.length; i++) {
            org.put(want[i], number[i]);
        }

        int cnt = 0;
        while(cnt <= (discount.length-10)) {
            Map<String, Integer> tmp = new HashMap<>();
            for(int i=cnt; i<cnt+10; i++) {
                tmp.put(discount[i], tmp.getOrDefault(discount[i], 0)+1);
            }
            cnt++;
            boolean possiable = true;
            for(String key : org.keySet()) {
                if(tmp.get(key) == null) {
                    possiable = false;
                    break;
                } else if(tmp.get(key) < org.get(key)) {
                    possiable = false;
                    break;
                }
            }
            if(possiable) answer++;
        }  
        return answer;
    }

    public static void main(String[] args) {
        Solution sol = new Solution();
        
        String[] want = new String[]{"banana", "apple", "rice", "pork", "pot"};
        int[] number = new int[]{3, 2, 2, 2, 1};
        String[] discount = new String[]{"chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana"};
        // String[] want = new String[]{"apple"};
        // int[] number = new int[]{10};
        // String[] discount = new String[]{"banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana"};

        sol.solution(want, number, discount);
    }
}

회고



출처


  • 해당 문제의 저작권은 문제를 만든이에게 있으며 자세한 내용은 문제 링크에서 참조바랍니다.