Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10-wkdghdwns199 #44

Merged
merged 6 commits into from
Mar 3, 2024
Merged

10-wkdghdwns199 #44

merged 6 commits into from
Mar 3, 2024

Conversation

wkdghdwns199
Copy link
Collaborator

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

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

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1์‹œ๊ฐ„ - ์™œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋œจ๋Š”๊ฑฐ์•ผ..? ๋ผ๊ณ  ๋ณด๋‹ค๊ฐ€ ์•Œ๊ณ ๋ณด๋‹ˆ O(n) ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง€๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ทธ๋ ‡๋‹ค..!

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ถฉ๊ฒฉ๊ณผ ๊ณตํฌ์˜ ์‹œ๊ฐ„์ดˆ๊ณผ...
image

Escape ๋ฅผ ๋ชปํ•˜๋Š” ๋ฌธ์ œ์˜€๋Š”์ค„ ์•Œ์•˜๋Š”๋ฐ, ๊ทธ๋ ‡์ง€ ์•Š์•˜๊ณ  ๊ฒฐ๋ก ๋ถ€ํ„ฐ ์ด์•ผ๊ธฐ ํ•˜๋ฉด list ์—์„œ์˜ pop(0) ํ•จ์ˆ˜๋Š” O(n) ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚œ ๊ฒƒ์ž…๋‹ˆ๋‹ค...! (์ด๊ฑฐ ๋•Œ๋ฌธ์— 1์‹œ๊ฐ„ ๊ฑธ๋ฆผ..)

๋‹ค๋ฅธ ๋ฌธ์ œ์—์„œ๋„ ๊ตณ์ด ํ•„์š”ํ•œ ์ž‘์—…์ด ์•„๋‹ˆ๋ฉด deque ๋ฅผ ์จ์„œ popleft() ํ•จ์ˆ˜๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค...!

์‹œ๊ฐ„ ๋ณต์žก๋„์— ๊ฑธ๋ฆฐ ์ฝ”๋“œ

import sys
N = int(input())
queue = list()
for _ in range(N):
    _str = sys.stdin.readline().split()
    if 'push' == _str[0] :
        queue.append(_str[1])
    elif 'pop' == _str[0] :
        if len(queue) == 0 : print(-1)

        # 0 ๋ฒˆ์งธ ์š”์†Œ๋ฅผ pop ํ•˜๋ฉด ๋’ค์— ์žˆ๋˜ ์š”์†Œ๋“ค์„ ํ•˜๋‚˜์”ฉ ๋‹น๊ฒจ์˜ค๋Š” ์ž‘์—… ๋•Œ๋ฌธ์— O(n) ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค.
        else : print(queue.pop(0)) 
    elif 'size' == _str[0] :
        print(len(queue))
    elif 'empty' == _str[0] :
        if len(queue) == 0 : print(0)
        else : print(1)
    elif 'front' == _str[0] :
        if len(queue) == 0 : print(-1)
        else : print(queue[0])
    elif 'back' == _str[0] :
        if len(queue) == 0 : print(-1)
        else : print(queue[len(queue)-1])
    

ํ•ด๊ฒฐ ์ฝ”๋“œ

import sys
from collections import deque

# import sys ๋กœ readline() ๋ฅผ ํ•ด์•ผ ํ•˜๊ณ , deque ๋กœ popleft() ๋ฅผ ํ•ด์•ผ ์‹œ๊ฐ„ ์ดˆ๊ณผ์— ๊ฑธ๋ฆฌ์ง€ ์•Š๋Š”๋‹ค.

N = int(input())
queue = deque()
for _ in range(N):
    _str = sys.stdin.readline().split()
    if 'push' == _str[0] :
        queue.append(_str[1])
    elif 'pop' == _str[0] :
        if len(queue) == 0 : print(-1)
        else : print(queue.popleft())
    elif 'size' == _str[0] :
        print(len(queue))
    elif 'empty' == _str[0] :
        if len(queue) == 0 : print(1)
        else : print(0)
    elif 'front' == _str[0] :
        if len(queue) == 0 : print(-1)
        else : print(queue[0])
    elif 'back' == _str[0] :
        if len(queue) == 0 : print(-1)
        else : print(queue[len(queue)-1])
        

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

pop(0) ์ด O(n) ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง€๋Š” ์ด์œ 

  • 0๋ฒˆ์งธ ์š”์†Œ๋ฅผ pop ํ•˜๊ณ  1,2,3... n ๋ฒˆ์งธ ์š”์†Œ๋“ค์„ ์•ž์œผ๋กœ ๋‹น๊ฒจ์˜ค๋Š” ๊ณผ์ •์—์„œ O(n) ์˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค..!

๊ทธ๋Ÿผ deque ์˜ popeleft() ๋Š” ์™œ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ์ง€ ์•Š๋Š”๊ฐ€?

  • collections.deque๋Š” ์–‘์ชฝ์—์„œ์˜ ๋น ๋ฅธ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ๋ฅผ ์ง€์›ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.
  • ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ(doubly linked list)๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ์–ด ๋งจ ์•ž๊ณผ ๋งจ ๋์—์„œ์˜ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ๊ฐ€ ๋ชจ๋‘ ์ƒ์ˆ˜ ์‹œ๊ฐ„ ( O(1) )์— ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.

@alstjr7437
Copy link
Member

๊ทธ๋ž˜์„œ ๋ณดํ†ต ์ €๋Š” ํŒŒ์ด์ฌ์—์„œ ์Šคํƒ ํ๋ฅผ ๊ตฌํ˜„ํ•  ์ผ์ด ์žˆ์œผ๋ฉด
list -> ์Šคํƒ
deque -> ํ
๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค!!
๋กœpop(0)๋กœ ๋นผ๋Š” ๊ฒƒ์€ ์Šคํƒ์—์„œ ํ›„์ž… ์„ ์ถœ์— ์ œ์ผ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋บด๋Š” ๊ฒƒ๊ณผ ๊ฐ™์•„์„œ O(n)์ด ๋‚˜์™€์„œ ํ๋ฅผ ์‚ฌ์šฉํ• ๋• ๋ฑ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค!

๊ทธ๋ฆฌ๊ณ  input์„ sys๋ฅผ ๋นผ๋จน์œผ์…จ์œผ๋ฉด
์ „๋ถ€ ๊ณ ์น˜์ง€๋ง๊ณ  ์•ž๋ถ€๋ถ„์— ์ด ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค!

import sys
input = sys.stdin.readline

์„ ํ•ด๋†“์œผ๋ฉด inputํ•จ์ˆ˜๊ฐ€ ์ž๋™์œผ๋กœ sys๋กœ ๋Œ์•„๊ฐ€ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ์•ˆ๋‚ฉ๋‹ˆ๋‹ค!!!

@wkdghdwns199
Copy link
Collaborator Author

์ด์ œ๋ถ€ํ„ฐ ์ €๋ž˜ ํ•ด์•ผ๊ธ‹์Šต๋‹ˆ๋‹ค..

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deque ๋ฅผ ์จ๋„ ๊ณ„์† ์‹œ๊ฐ„์ดˆ๊ณผ๋‚˜์„œ ํž˜๋“ค์—ˆ๋„ค์š”..
kotlin ์ž…์ถœ๋ ฅ์ด ์˜ค๋ž˜๊ฑธ๋ ค์„œ ๊ทธ๋žฌ๋‚˜๋ด์š”.

BufferedWriter ๋กœ string buffer ์— ๋„ฃ๊ณ  ํ•œ๋ฒˆ์— ์ถœ๋ ฅํ•ด ์‹œ๊ฐ„์„ ๋‹จ์ถ•์‹œ์ผฐ์Šต๋‹ˆ๋‹น

import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val bw = BufferedWriter(OutputStreamWriter(System.out))
    val n = br.readLine().toInt()
    val deque = ArrayDeque<Int>()

    for (i in 0 until n) {
        val input = br.readLine().split(" ")
        if (input[0] == "push") {
            deque.add(input[1].toInt())
            continue
        }
        with(bw) {
            when (input[0]) {
                "pop" -> write(deque.removeFirstOrNull())
                "front" -> write(deque.firstOrNull())
                "back" -> write(deque.lastOrNull())
                "size" -> write(deque.size.toString())
                "empty" -> bw.write(if (deque.isEmpty()) "1" else "0")
                else -> throw IllegalArgumentException()
            }
            bw.newLine()
        }
    }
    bw.flush()
    bw.close()
}

private fun BufferedWriter.write(num: Int?) {
    if (num == null) {
        write("-1")
        return
    }
    write(num.toString())
}

@wkdghdwns199
Copy link
Collaborator Author

Kotlin ์–ธ์  ๊ฐ„ ํ•œ ๋ฒˆ ๋ง› ๋ณด๊ฒŒ ๋  ๊ฑฐ ๊ฐ™์€๋ฐ ๋ฌด์Šต๋„ค์š”

@wkdghdwns199 wkdghdwns199 removed the request for review from fnzksxl March 3, 2024 11:43
@wkdghdwns199 wkdghdwns199 merged commit ab76a92 into main Mar 3, 2024
2 checks passed
@wkdghdwns199 wkdghdwns199 deleted the 10-wkdghdwns199 branch March 3, 2024 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants