๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[๋ฐฑ์ค€ 11003] ์ตœ์†Ÿ๊ฐ’ ์ฐพ๊ธฐ : Deque ๋ณธ๋ฌธ

Study/์•Œ๊ณ ๋ฆฌ์ฆ˜, ๊ตฌํ˜„ ๋ฌธ์ œํ’€์ด

[๋ฐฑ์ค€ 11003] ์ตœ์†Ÿ๊ฐ’ ์ฐพ๊ธฐ : Deque

Shinjio 2024. 10. 29. 22:56

๐Ÿ”Ž ๋ฌธ์ œ

https://www.acmicpc.net/problem/11003


๐ŸŽ ํ’€์ด

์ฝ”ํ…Œ ์ค€๋น„๋ฅผ ์œ„ํ•ด Do it! ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ฑ…์„ ๋ณด๋ฉด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ๋งŒ๋‚œ ๋ฌธ์ œ๋กœ, Deque๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ๋Š” ๋ฌธ์ œ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์ฝ”ํ…Œ ์ดˆ๋ณด์ž์ธ ๋‚˜๋Š” ์ฒ˜์Œ ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์ด์ค‘ for๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ‘ธ๋Š” ๋ฐฉ์‹์„ ์ƒ๊ฐํ–ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ์ด์ค‘ For์œผ๋กœ ํ’€ ๊ฒฝ์šฐ ์‹œ๊ฐ„ ์ œํ•œ์— ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— O(n)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„ ๋ฐฉ์‹์„ ์ฐพ์•„์•ผํ–ˆ๋‹ค. 

 

์ด ๋ฌธ์ œ์—์„œ ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์ธ Deque๋ž€ ๋ฌด์—‡์ผ๊นŒ?

Deque(๋ฑ)์ด๋ž€, ์–‘ ๋์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋กœ LinkedList,  ArrayDeque ๋“ฑ์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค. 

addFirst(), removeFirst() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋งจ ์•ž์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…, ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๊ณ ,

addLast(), removeLast() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋งจ ๋’ค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…, ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ์•„๋ž˜์˜ ๊ทœ์น™์„ ์ดํ•ดํ•ด์•ผ ํ’€์ด ๋ฐฉ์‹์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. 

1. deque๋Š” ์ตœ์†Œ๊ฐ’ ์ˆœ(์ž‘์€ ์ˆซ์ž ์ˆœ)์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค. 

2. deque์˜ ํฌ๊ธฐ๋Š” ์œˆ๋„์šฐ์˜ ํฌ๊ธฐ๋ฅผ ๋„˜์–ด์„œ๋ฉด ์•ˆ๋œ๋‹ค. 

 

์ด ๊ทœ์น™์„ ํ† ๋Œ€๋กœ ๋ง๋กœ์จ ๋ฌธ์ œ ํ’€์ด ๋ฐฉ์‹์„ ์„ค๋ช…ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค. 

  • Deque์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ์ฒด(class)์˜ ๊ตฌ์กฐ๋Š” index์™€ value๋ฅผ ํฌํ•จํ•œ๋‹ค. 
  • ๊ธฐ๋ณธ์ ์œผ๋กœ Deque.addLast()๋ฅผ ํ†ตํ•ด ์ƒˆ๋กญ๊ฒŒ ๋“ค์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ๋‹ค. 
  • ํ•˜์ง€๋งŒ, ๋งˆ์ง€๋ง‰ ์œ„์น˜์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๋•Œ ๋งŒ์•ฝ ์ƒˆ๋กญ๊ฒŒ ๋“ค์–ด์˜ค๋Š” ๊ฐ’๋ณด๋‹ค ๊ธฐ์กด์— Last์— ์ €์žฅ๋œ ๊ฐ’์ด ๋” ํฌ๋‹ค๋ฉด, ํ•ด๋‹น ๊ฐ’์€ removeLast()๋ฅผ ํ†ตํ•ด ์‚ญ์ œํ•œ๋‹ค. → ์ž‘์€ ์ˆซ์ž ์ˆœ์œผ๋กœ ์ •๋ ฌ
  • ๊ทธ๋ฆฌ๊ณ , ์ƒˆ๋กญ๊ฒŒ ๋“ค์–ด์˜จ ๋ฐ์ดํ„ฐ์˜ index์™€ First์— ์ €์žฅ๋œ index์˜ ์ฐจ์ด๊ฐ€ ์œˆ๋„์šฐ์˜ ํฌ๊ธฐ๋ฅผ ๋„˜์–ด์„œ๋ฉด First์˜ ๊ฐ’์„ ์ง€์šด๋‹ค. 
  • ์œ„์˜ ๊ทœ์น™์ด ์ ์šฉ๋œ deque๋ผ๋ฉด ์ตœ์†Œ๊ฐ’์€ ๋งจ ์•ž(First)์— ์กด์žฌํ•˜๋Š” ๊ฐ’์ด๋ฏ€๋กœ getFirst()๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค. 

 

์œ„์—์„œ ํ’€์ดํ•œ ๋‚ด์šฉ์„ java ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•œ ๋‚ด์šฉ์ด๋‹ค. 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Deque;
import java.util.LinkedList;

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int countOfNumber = Integer.parseInt(st.nextToken());
        int sizeOfWindow = Integer.parseInt(st.nextToken());

        st = new StringTokenizer(br.readLine());
        Deque<Node> deque = new LinkedList<>();

        for(int i = 0 ; i < countOfNumber ; i++){
            int now = Integer.parseInt(st.nextToken());

            while(!deque.isEmpty() && deque.getLast().value > now){
                deque.removeLast();
            }
            deque.addLast(new Node(now, i));
            if(i - deque.getFirst().index >= sizeOfWindow){
                deque.removeFirst();
            }
            bw.write(deque.getFirst().value + " ");
        }
        bw.flush();
        bw.close();
    }

    static class Node{
        int value;
        int index;

        Node(int value, int index){
            this.value = value;
            this.index = index;
        }
    }
}

 

์ฝ”๋“œ์™€ ํ’€์ดํ•œ ๋‚ด์šฉ์„ ๋น„๊ตํ•˜๋ฉฐ ์„ค๋ช…ํ•˜์ž๋ฉด,

- Deque์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ์ฒด(class)์˜ ๊ตฌ์กฐ๋Š” index์™€ value๋ฅผ ํฌํ•จํ•œ๋‹ค. 

 Node class๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜ํ•˜๊ณ , Deque์—์„œ๋Š” ์ž‘์„ฑํ•œ Node ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค. 

    static class Node{
        int value;
        int index;

        Node(int value, int index){
            this.value = value;
            this.index = index;
        }
    }

 

- ๊ธฐ๋ณธ์ ์œผ๋กœ Deque.addLast()๋ฅผ ํ†ตํ•ด ์ƒˆ๋กญ๊ฒŒ ๋“ค์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ๋‹ค. 

- ํ•˜์ง€๋งŒ, ๋งˆ์ง€๋ง‰ ์œ„์น˜์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๋•Œ ๋งŒ์•ฝ ์ƒˆ๋กญ๊ฒŒ ๋“ค์–ด์˜ค๋Š” ๊ฐ’๋ณด๋‹ค ๊ธฐ์กด์— Last์— ์ €์žฅ๋œ ๊ฐ’์ด ๋” ํฌ๋‹ค๋ฉด, ํ•ด๋‹น ๊ฐ’์€ removeLast()๋ฅผ ํ†ตํ•ด ์‚ญ์ œํ•œ๋‹ค. → ์ž‘์€ ์ˆซ์ž ์ˆœ์œผ๋กœ ์ •๋ ฌ

- ๊ทธ๋ฆฌ๊ณ , ์ƒˆ๋กญ๊ฒŒ ๋“ค์–ด์˜จ ๋ฐ์ดํ„ฐ์˜ index์™€ First์— ์ €์žฅ๋œ index์˜ ์ฐจ์ด๊ฐ€ ์œˆ๋„์šฐ์˜ ํฌ๊ธฐ๋ฅผ ๋„˜์–ด์„œ๋ฉด First์˜ ๊ฐ’์„ ์ง€์šด๋‹ค. 

- ์œ„์˜ ๊ทœ์น™์ด ์ ์šฉ๋œ deque๋ผ๋ฉด ์ตœ์†Œ๊ฐ’์€ ๋งจ ์•ž(First)์— ์กด์žฌํ•˜๋Š” ๊ฐ’์ด๋ฏ€๋กœ getFirst()๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค. 

for(int i = 0 ; i < countOfNumber ; i++){
    int now = Integer.parseInt(st.nextToken());

    while(!deque.isEmpty() && deque.getLast().value > now){
        deque.removeLast();
    }
    deque.addLast(new Node(now, i));
    if(i - deque.getFirst().index >= sizeOfWindow){
        deque.removeFirst();
    }
    bw.write(deque.getFirst().value + " ");
}

Deque๋ผ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ์— ๋Œ€ํ•ด ๋ชฐ๋ž์„ ๋• ๋ฌด์ฒ™ ์–ด๋ ต๊ฒŒ ๋Š๊ปด์ง„ ๋ฌธ์ œ์˜€์ง€๋งŒ, ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผ ํ•  ์ง€ ์•Œ๊ณ ๋‚˜์ง€ ํ’€์ด ์ž์ฒด๋Š” ์–ด๋ ต์ง€ ์•Š์•˜๋‹ค. ์ด๋ฒˆ ๊ธฐํšŒ๋ฅผ ํ†ตํ•ด ์ž˜ ์ •๋ฆฌํ•ด์„œ ์œ ์‚ฌํ•œ ๋ฌธ์ œ๋ฅผ ํ’€์ด ์—†์ด ํ’€ ์ˆ˜ ์žˆ๋„๋ก ์—ฐ์Šตํ•ด์•ผ๊ฒ ๋‹ค.