๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

baekjoon. ํšŒ์ „ ์ดˆ๋ฐฅ (2531) | hashmap์‚ฌ์šฉ [java]

ttoance 2022. 10. 17. 01:23

์—ฌ๊ธฐ ๋ฌธ์ œ์ง‘์—์„œ ํšŒ์ „์ดˆ๋ฐฅ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค.

ใ„ด ๋ฌธ์ œ์ง‘ : https://www.acmicpc.net/workbook/view/8708

ใ„ด ๋ฌธ์ œ : https://www.acmicpc.net/problem/2531

 

 

๋ฌธ์ œ๊ฐ€ ์กฐ๊ธˆ ๊ธธ๊ฒŒ ์„ค๋ช…์ด ๋˜์–ด์žˆ๋Š”๋ฐ ์š”์•ฝํ•˜๋ฉด 

- ์—ฐ์†์œผ๋กœ ๋จน์„ ์ˆ˜ ์žˆ๋Š” ์ดˆ๋ฐฅ์— ์ตœ๋Œ€ํ•œ ์ˆซ์ž๊ฐ€ ๊ณ ์œ ํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๋ฉด ๋œ๋‹ค. 

- ๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ ์ด ์ดˆ๋ฐฅ์— ์ฟ ํฐ์ดˆ๋ฐฅ์ด ๋“ค์–ด๊ฐ€์žˆ์œผ๋ฉด ๊ทธ๋Œ€๋กœ ๋‘๊ณ , ์•„๋‹ˆ๋ผ๋ฉด ์ฟ ํฐ์ดˆ๋ฐฅ์„ +1ํ•˜๋ฉด ๋œ๋‹ค. 

 

์ดˆ๋ฐฅ ๊ทธ๋ฆ‡ ๊ฐฏ์ˆ˜ (n)์— ๋งค๋ฒˆ for๋ฌธ์„ ๋Œ๋ฉด์„œ ์—ฐ์† ์ˆซ์ž (k)๋ฅผ ์ฒดํฌํ•˜๋ฉด ๋˜๋Š”๋ฐ ๊ทธ๋Ÿด ๊ฒฝ์šฐ 

n์ตœ๋Œ€ 30,000์— k 3,000๊นŒ์ง€ ํ•ด์„œ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ๊ฒƒ ๊ฐ™์•„์„œ ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์•ผ ๋˜์—ˆ์—ˆ๋‹ค. 

 

๊ทธ๋ž˜์„œ ์ดˆ๋ฐฅ ๊ทธ๋ฆ‡ ๊ฐฏ์ˆ˜ (n)์—์„œ ๊ฐ ์ดˆ๋ฐฅ๋งˆ๋‹ค HashMap์œผ๋กœ ๋ช‡๋ฒˆ์˜ ์ดˆ๋ฐฅ์ด ๋ช‡ ๊ฐœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ์ €์žฅํ–ˆ๋‹ค๊ฐ€ 

๋‹ค์Œ ์ดˆ๋ฐฅ์œผ๋กœ ์˜ฎ๊ธฐ๋ฉด์„œ ๋งจ ์ฒซ ์ดˆ๋ฐฅ์„ ์ง€์šฐ๊ณ  ์ƒˆ๋กœ์šด ์ดˆ๋ฐฅ์„ ๋„ฃ์œผ๋ฉด์„œ ๊ณ„์‚ฐํ–ˆ๋‹ค. 

 

์ •๋ฆฌํ•˜๋ฉด 

1. ํšŒ์ „ ์ดˆ๋ฐฅ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋’ค์—์„œ๋ถ€ํ„ฐ k๊ฐœ์˜ ์ดˆ๋ฐฅ์„ (์ดˆ๋ฐฅ์˜ ์ข…๋ฅ˜, ๊ฐฏ์ˆ˜) ํ˜•ํƒœ๋กœ map์— ๋„ฃ๋Š”๋‹ค. 

2. ์ฒ˜์Œ๋ถ€ํ„ฐ k๊ฐœ ์ „๊นŒ์ง€๋Š” ๋’ค์—์„œ ํ•˜๋‚˜๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ฒ˜์Œ๊บผ๋ฅผ ๋„ฃ๋Š”๋‹ค. 

3. ๊ทธ ๋‹ค์Œ ๋ถ€ํ„ฐ๋Š” ์ง€๊ธˆ ๋Œ๊ณ  ์žˆ๋Š” ๊ฑฐ์—์„œ k๊ฐœ ์ „๊บผ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ง€๊ธˆ๊บผ๋ฅผ ๋„ฃ๋Š”๋‹ค. 

4. ํšŒ์ „ ์ดˆ๋ฐฅ ๊ฐฏ์ˆ˜๋Š” ์ด map์˜ size์—๋‹ค ์ฟ ํฐ์˜ ์—ฌ๋ถ€์— ๋”ฐ๋ผ +1์„ ๋”ํ•œ๋‹ค. 

 

๋‹ค๋ฅธ ํ•ด์„ค ํ’€์ด๋ฅผ ์‚ดํŽด๋ณด๋ฉด ์กฐ๊ธˆ ๋” ๋‹จ์ˆœํ•˜๊ฒŒ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•๋“ค์ด ์žˆ์—ˆ๋‹ค. 

- ์œ„์˜ 2๋ฒˆ,3๋ฒˆ์„ ๋‚˜๋ˆ ์„œ ๊ฐ€์ ธ๊ฐ€์ง€ ์•Š๊ณ  ์ฒ˜์Œ๋ถ€ํ„ฐ ๋’ค์— ๊ฐ’์„ ๋„ฃ์–ด๋†“๊ณ  2๋ฒˆ 3๋ฒˆ์€ for๋ฌธ์„ ํƒœ์› ๋‹ค. https://code-lab1.tistory.com/m/182

- ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋ฅผ ํ†ตํ•ด์„œ ๋กœ์ง์„ ๋‹จ์ˆœํ•˜๊ฒŒ ๊ฐ€์ ธ๊ฐ„๋‹ค.  https://hunucho.tistory.com/m/312

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.HashMap;


public class Main {
    static int[] dishArr;
    static HashMap<Integer,Integer> temp;
    static int c;
    
    public static void main(String args[]) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        // ํšŒ์ „ ์ดˆ๋ฐฅ ๋ฒจํŠธ์— ๋†“์ธ ์ ‘์‹œ์˜ ์ˆ˜ N, ์ดˆ๋ฐฅ์˜ ๊ฐ€์ง“์ˆ˜ d, ์—ฐ์†ํ•ด์„œ ๋จน๋Š” ์ ‘์‹œ์˜ ์ˆ˜ k, ์ฟ ํฐ ๋ฒˆํ˜ธ c
        int N = Integer.parseInt(st.nextToken());
        int d = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());
        c = Integer.parseInt(st.nextToken());
        
        dishArr = new int[N];
        // ์ ‘์‹œ ์„ธํŒ… 
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            dishArr[i] = Integer.parseInt(st.nextToken());
        }
        
        temp = new HashMap();
        
        // ์ดˆ๋ฐฅ์ด ์›ํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹œ์ž‘์ „ ๋งˆ์ง€๋ง‰ ๋„ฃ์–ด๋‘๊ธฐ 
        for (int i = 0; i < k; i++) {
            add(dishArr[N-1-i]);
        }
        
        int size = Integer.MIN_VALUE;
        size = max(size);
        
        for (int i = 0; i < N; i++) {
            
            // ์ œ๊ฑฐํ•  ์ดˆ๋ฐฅ ์ •ํ•˜๊ธฐ : ์›ํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ถ„๊ธฐํƒœ์›€ 
            int deleted;
            if (i < k) {
                deleted = dishArr[N - k + i];
            }
            else {
                deleted = dishArr[i - k ]; 
            }
            
            // ์•ž์˜ ์ดˆ๋ฐฅ ์ œ๊ฑฐ 
            remove(deleted);
            
            int dish = dishArr[i];
            add(dish);
            
            size = max(size);
        }
        
        System.out.printf("%d \n", size);
    }
    
    static void remove(int deleted) {
        if (temp.get(deleted) == 1) {
            temp.remove(deleted);
        }
        else {
            temp.put(deleted, temp.get(deleted) - 1);
        }
    }
    
    static void add(int dish) {
        if (! temp.containsKey(dish)) {
            temp.put(dish, 1);
        }
        else {
            temp.put(dish, temp.get(dish) + 1);
        }
    }
    
    static int max(int size) {
        int free = 0; 
        if (! temp.containsKey(c)) {
            free = 1;
        }
        
        int max = Math.max(temp.size() + free, size);
        return max;
    }
}
๋ฐ˜์‘ํ˜•