File tree Expand file tree Collapse file tree 3 files changed +73
-0
lines changed
Expand file tree Collapse file tree 3 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 1+ a = int (input ())
2+ sts = list (map (int , input ().split ()))
3+ stack = []
4+ target = 1
5+
6+ for st in sts :
7+ stack .append (st )
8+ while stack and stack [- 1 ] == target :
9+ stack .pop ()
10+ target += 1
11+
12+ if stack :
13+ print ('Sad' )
14+ else :
15+ print ('Nice' )
Original file line number Diff line number Diff line change 1+ # ๋ง์ง๋ง ๋จ์ ์นด๋ ๋ฒํธ ๊ตฌํ๋ ๋ฌธ์
2+ # ๊ฐ์ฅ ๋จผ์ ์นด๋์ ๊ฐ์๋ฅผ ๋ถ๋ฌ์จ ๋ค, dequeํจ์๋ฅผ ์ด์ฉํด ๋น ํ๋ฅผ ์์ฑํ๋ค
3+ import sys
4+ from collections import deque
5+
6+ a = int (sys .stdin .readline ())
7+ deq = deque ()
8+
9+ # ์ดํ, for๋ฌธ์ ํตํด 1๋ถํฐ a๊น์ง์ ์(์นด๋ ๋ฒํธ)๋ฅผ ํ์ ๋ฃ๋๋ค
10+ for i in range (a ):
11+ deq .append (i + 1 )
12+ # while๋ฌธ์ ํตํด deq๊ฐ 1์ด ๋ ๋๊น์ง ๊ฐ์ฅ ์ ์ฐจ๋๋ฅผ ๋ฒ๋ฆฌ๊ณ , ์ ์ผ ์ ์นด๋๋ฅผ ๊ฐ์ฅ ์๋๋ก ์ฎ๊ธด๋ค
13+ while len (deq ) > 1 :
14+ deq .popleft ()
15+ deq .append (deq .popleft ())
16+
17+ print (deq .pop ())
Original file line number Diff line number Diff line change 1+ # sys๋ก ๋ช
๋ น ์๋ฅผ ๋ฐ์ ๋ค, deque๋ฅผ ํตํด ๋น์ด ์๋ ํ๋ฅผ ์์ฑํ๋ค
2+ # ๋ช
๋ น ์ฒ๋ฆฌ์ ํ์ํ ๊ธฐ๋ฅ์ ๊ตฌํํ ๋ค, ์
๋ ฅ๋ ๋ช
๋ น ์์ธ a ๋งํผ for๋ฌธ์ ํตํด ๋ฐ๋ณตํ๋ค
3+
4+ import sys
5+ from collections import deque
6+
7+ a = int (sys .stdin .readline ())
8+ deq = deque ()
9+ for _ in range (a ):
10+ b = sys .stdin .readline ().split ()
11+
12+ # deq์ ์ถ๊ฐ
13+ if b [0 ] == 'push' :
14+ deq .append (int (b [1 ]))
15+ # ๊ฐ์ฅ ์์ ์ ์ถ๋ ฅ
16+ if b [0 ] == 'pop' :
17+ if deq :
18+ print (deq .popleft ())
19+ else :
20+ print (- 1 )
21+ # ํ์ ๋ ์ ์ ๊ฐ์ ์ถ๋ ฅ
22+ if b [0 ] == 'size' :
23+ print (len (deq ))
24+ # ๋น์๋ค๋ฉด 0, ์๋๋ฉด 1
25+ if b [0 ] == 'empty' :
26+ if deq :
27+ print ('0' )
28+ else :
29+ print ('1' )
30+ # ํ์ ๊ฐ์ฅ ์ ์ ์ ์ถ๋ ฅ, ์ ์๊ฐ ์์ ๊ฒฝ์ฐ -1 ์ถ๋ ฅ
31+ if b [0 ] == 'front' :
32+ if deq :
33+ print (deq [0 ])
34+ else :
35+ print (- 1 )
36+ # ํ์ ๊ฐ์ฅ ๋ค ์ ์ ์ถ๋ ฅ, ์์ ๊ฒฝ์ฐ -1 ์ถ๋ ฅ
37+ if b [0 ] == 'back' :
38+ if deq :
39+ print (deq [- 1 ])
40+ else :
41+ print (- 1 )
You canโt perform that action at this time.
0 commit comments