Home ๐Ÿฃ ๋ฐฑ์ค€ Q11003 - ์ตœ์†Œ๊ฐ’ ์ฐพ๊ธฐ (java)
Post
Cancel

๐Ÿฃ ๋ฐฑ์ค€ Q11003 - ์ตœ์†Œ๊ฐ’ ์ฐพ๊ธฐ (java)


๋ฌธ์ œ





์Šˆ๋„ ์ฝ”๋“œ


1
2
3
4
5
6
7
8
9
10
11
12
	N(๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜) L(์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฒ”์œ„)
	Deque<Node> mydeque(๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„ ๋ฑ ์ž๋ฃŒ๊ตฌ์กฐ)
	for(N๋งŒํผ ๋ฐ˜๋ณต)
	{
		now(ํ˜„์žฌ ๋ฐ์ดํ„ฐ ๊ฐ’)
		 ๋ฑ์˜ ๋งˆ์ง€๋ง‰ ์œ„์น˜์—์„œ๋ถ€ํ„ฐ now๋ณด๋‹ค ํฐ ๊ฐ’์€ ๋ฑ์—์„œ ์ œ๊ฑฐํ•˜๊ธฐ
		 ๋ฑ์˜ ๋งˆ์ง€๋ง‰ ์œ„์น˜์— now๊ฐ’ ์ €์žฅํ•˜๊ธฐ
		 ๋ฑ์˜ 1๋ฒˆ์งธ ์œ„์น˜์—์„œ๋ถ€ํ„ฐ L์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ๊ฐ’(now index-L <= index)์„ ๋ฑ์—์„œ ์ œ๊ฑฐํ•˜๊ธฐ
		 ๋ฑ์˜ 1๋ฒˆ์งธ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅํ•˜๊ธฐ
	}
	๋ฑ์— ์ €์žฅํ•  ๋…ธ๋“œ ํด๋ž˜์Šค ๋ณ„๋„ ์ƒ์„ฑํ•˜๊ธฐ
	๋…ธ๋“œ ํด๋ž˜์Šค์—๋Š” index(์ž์‹ ์˜ ์œ„์น˜), value(์ž์‹ ์˜ ๊ฐ’) ๋‹ด


๋ฑ(Deque) ์—ฐ์‚ฐ
์™ผ์ชฝ์—์„œ ํ•จ์ˆ˜๊ฐ€ ์‚ฝ์ž…, ์‚ญ์ œ : addFirst(), removeFirst()
์˜ค๋ฅธ์ชฝ์—์„œ ํ•จ์ˆ˜๊ฐ€ ์‚ฝ์ž…, ์‚ญ์ œ : addLast(), removeLast()



์ฝ”๋“œ


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.io.*;
import java.util.*;

public class Q11003_์ตœ์†Œ๊ฐ’์ฐพ๊ธฐ {
  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 N = Integer.parseInt(st.nextToken());
    int L = Integer.parseInt(st.nextToken());

    st = new StringTokenizer(br.readLine());
    Deque<Node> myDeque = new LinkedList<>();
    for(int i = 0 ; i < N; i++) {
      int now = Integer.parseInt(st.nextToken());
      // 1๏ธโƒฃ (0,1)
      // 2๏ธโƒฃ (1,5)
      // 3๏ธโƒฃ (2,2)
      // 4๏ธโƒฃ (3,3)
      // 5๏ธโƒฃ (4,6)
      // ...

      // ์ƒˆ๋กœ๋“ค์–ด์˜ฌ ๊ฐ’๋ณด๋‹ค ๋” ํฐ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ์ œ๊ฑฐ -> ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜๊ฒŒ ๋จ
      while(!myDeque.isEmpty() && myDeque.getLast().value > now) {
        myDeque.removeLast();
      }
      // -> 3๏ธโƒฃ(1,5) > (2,2)   ->    (1,5) ์ œ๊ฑฐ   ->(0,1), (2,2)

      myDeque.addLast(new Node(i, now)); // ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋’ค์— ์ถ”๊ฐ€ํ•ด์คŒ
      // -> 1๏ธโƒฃ(0,1)
      // -> 2๏ธโƒฃ(0,1), (1,5)
      // -> 4๏ธโƒฃ(0,1), (2,2), (3,3)
      // -> 5๏ธโƒฃ(0,1), (2,2), (3,3), (4,6)

      // ํ•„์š”์—†๋Š” ์•ž๋ถ€๋ถ„์˜ ์ˆซ์ž๋“ค ์—†์• ๊ธฐ
      if(myDeque.getFirst().index <= i - L) { // ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค๊ฐ€ L์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ๋‹ค๋ฉด ์ œ๊ฑฐ
        myDeque.removeFirst(); // ๋งจ ์•ž์— ์žˆ๋Š” ๋…ธ๋“œ ์‚ญ์ œ
      }
      // -> 5๏ธโƒฃ ๋ฑ์˜ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค(0) <= 4-L(3)    ->    (0,1) ์ œ๊ฑฐ

      bw.write(myDeque.getFirst().value + " "); // ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋งจ ์•ž์— ์žˆ๋Š” ๊ฒƒ์ด ์ตœ์†Œ๊ฐ’
    }
    bw.flush();
    bw.close();
  }
  
  static class Node {
    public int index;
    public int value;

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

/*
---- ์˜ˆ์ œ ์ž…๋ ฅ 1 -----
12 3
1 5 2 3 6 2 3 7 3 5 2 6
----- ์˜ˆ์ œ ์ถœ๋ ฅ 1 -----
1 1 1 2 2 2 2 2 3 3 2 2
 */

This post is licensed under CC BY 4.0 by the author.

/Data structure/ ๐Ÿ’ฌ Stack, Queue

๐Ÿฃ ๋ฐฑ์ค€ Q12891 - DNA ๋น„๋ฐ€๋ฒˆํ˜ธ (java)