코테 기초

Deque 사용

서버관리자 페페 2022. 12. 5. 22:23

-

import java.io.*;
import java.util.Deque;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {

        // ISC - L1
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int L = Integer.parseInt(st.nextToken());

        // L2
        st = new StringTokenizer(br.readLine());
        Deque<Node> mydeque = new LinkedList<>();

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

            while (!mydeque.isEmpty() && mydeque.getLast().value > now) {
                mydeque.removeLast();
            }

            mydeque.addLast(new Node(now, i));

            if (mydeque.getFirst().index <= i - L) {
                mydeque.removeFirst();
            }

            // OEC
            bw.write(mydeque.getFirst().value + " ");
        }

        bw.flush();
        bw.close();
    }

    static class Node {
        public int value;
        public int index;

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

addfirst

getfirst().index or value

Removefirst

 

 

'코테 기초' 카테고리의 다른 글

NumberformatException  (0) 2022.12.06
static 사용  (0) 2022.12.06
switch > case 형식  (0) 2022.12.05
외부 모듈과 외부 인자 공급  (0) 2022.12.05
SLIDING WINDOW  (0) 2022.12.05