-
Notifications
You must be signed in to change notification settings - Fork 1
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
10-wkdghdwns199 #44
Conversation
๊ทธ๋์ ๋ณดํต ์ ๋ ํ์ด์ฌ์์ ์คํ ํ๋ฅผ ๊ตฌํํ ์ผ์ด ์์ผ๋ฉด ๊ทธ๋ฆฌ๊ณ input์ sys๋ฅผ ๋นผ๋จน์ผ์
จ์ผ๋ฉด import sys
input = sys.stdin.readline ์ ํด๋์ผ๋ฉด inputํจ์๊ฐ ์๋์ผ๋ก sys๋ก ๋์๊ฐ ์๊ฐ์ด๊ณผ๊ฐ ์๋ฉ๋๋ค!!! |
์ด์ ๋ถํฐ ์ ๋ ํด์ผ๊ธ์ต๋๋ค.. |
There was a problem hiding this 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())
}
Kotlin ์ธ์ ๊ฐ ํ ๋ฒ ๋ง ๋ณด๊ฒ ๋ ๊ฑฐ ๊ฐ์๋ฐ ๋ฌด์ต๋ค์ |
๐ ๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/18258
โ๏ธ ์์๋ ์๊ฐ
1์๊ฐ - ์ ์๊ฐ์ด๊ณผ๊ฐ ๋จ๋๊ฑฐ์ผ..? ๋ผ๊ณ ๋ณด๋ค๊ฐ ์๊ณ ๋ณด๋ O(n) ์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ ํจ์๋ฅผ ์ฌ์ฉํด์ ๊ทธ๋ ๋ค..!
โจ ์๋ ์ฝ๋
์ถฉ๊ฒฉ๊ณผ ๊ณตํฌ์ ์๊ฐ์ด๊ณผ...
Escape ๋ฅผ ๋ชปํ๋ ๋ฌธ์ ์๋์ค ์์๋๋ฐ, ๊ทธ๋ ์ง ์์๊ณ ๊ฒฐ๋ก ๋ถํฐ ์ด์ผ๊ธฐ ํ๋ฉด list ์์์ pop(0) ํจ์๋ O(n) ์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๊ธฐ ๋๋ฌธ์ ์๊ฐ ์ด๊ณผ๊ฐ ๋ ๊ฒ์ ๋๋ค...! (์ด๊ฑฐ ๋๋ฌธ์ 1์๊ฐ ๊ฑธ๋ฆผ..)
๋ค๋ฅธ ๋ฌธ์ ์์๋ ๊ตณ์ด ํ์ํ ์์ ์ด ์๋๋ฉด deque ๋ฅผ ์จ์ popleft() ํจ์๋ฅผ ์ฐ๋ ๊ฒ์ด ๋ฐ๋์งํ๊ฒ ์ต๋๋ค...!
์๊ฐ ๋ณต์ก๋์ ๊ฑธ๋ฆฐ ์ฝ๋
ํด๊ฒฐ ์ฝ๋
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
pop(0) ์ด O(n) ์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ ์ด์
๊ทธ๋ผ deque ์ popeleft() ๋ ์ ์ค๋ ๊ฑธ๋ฆฌ์ง ์๋๊ฐ?