1. λ¬Έμ μ€λͺ
- μ
λ ₯
- n : μ²μ νμ ν κ°μλ₯Ό λνλ΄λ μ μ
- k : μ²μμ μ νλ νμ μμΉλ₯Ό λνλ΄λ μ μ
- μΆλ ₯
- μνν λͺ λ Ήμ΄λ€μ΄ λ΄κΈ΄ λ¬Έμμ΄ λ°°μ΄ cmdκ° λ§€κ°λ³μλ‘ μ£Όμ΄μ§ λ, λͺ¨λ λͺ λ Ήμ΄λ₯Ό μνν ν νμ μνμ μ²μ μ£Όμ΄μ§ νμ μνλ₯Ό λΉκ΅νμ¬ μμ λμ§ μμ νμ O, μμ λ νμ Xλ‘ νμνμ¬ λ¬Έμμ΄ ννλ‘ return
2. μ½λ
solution1.py
class Node:
def __init__(self):
self.removed = False
self.prev = None
self.next = None
def solution(n, k, cmd):
nodeArr = [Node() for _ in range(n)]
for i in range(1, n):
nodeArr[i-1].next = nodeArr[i]
nodeArr[i].prev = nodeArr[i-1]
curr = nodeArr[k]
mystack = []
for str in cmd:
if str[0] == 'U':
x = int(str[2:])
for _ in range(x):
curr = curr.prev
elif str[0] == 'D':
x = int(str[2:])
for _ in range(x):
curr = curr.next
elif str[0] == 'C':
mystack.append(curr)
curr.removed = True
up = curr.prev
down = curr.next
if up:
up.next = down #μ°κ²°
if down:
down.prev = up#μ°κ²°
curr = down
else:#λ§μ§λ§ κ°μ΄λ©΄
curr = up
else: #Z
node = mystack.pop()
node.removed = False
up = node.prev
down = node.next
if up:
up.next = node
if down:
down.prev = node
answer = ''
for i in range(n):
if nodeArr[i].removed: #True
answer+='X'
else:
answer+='O'
return answer
3. νκ³
- μ²μμλ μλ£κ΅¬μ‘°λ₯Ό μ¬μ©νμ§ μκ³ νλ €κ³ νλ€λ³΄λ κ³μ λ§νλ€.
- νμ΄λ₯Ό 보λ, μ°κ²°λ¦¬μ€νΈλ₯Ό νμ©ν΄ νμλ€.
- classλ‘ Node κ°μ²΄λ₯Ό μ μΈν΄μ μ νμ 보λ₯Ό μ
λ°μ΄νΈν μ μκ³ , μμ ν ν νμ¬ μμΉλ₯Ό μ
λ°μ΄νΈνκΈ° νΈνλ€.
- μμ νμλ νμ¬ μμΉκ° μλλ‘ λ΄λ €κ°λλ°, λ§μ§λ§ λ Έλκ° μμ λ λλ νμ¬μμΉλ₯Ό μλ‘ μ¬λ €μΌνλ€.
- μ°κ²°λ¦¬μ€νΈλ₯Ό νμ΄μ¬μμ ꡬννλ λ°©λ²μ λ°°μ κ³ μ¬λ―Έμμλ€.
- λ€μ λ²μλ νμ©ν μ μλλ‘ κ΅¬νμ°μ΅μ ν΄μΌκ² λ€.
4. μ°Έκ³
'Algorithm > νλ‘κ·Έλλ¨Έμ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[νλ‘κ·Έλλ¨Έμ€] νκ΄΄λμ§ μμ 건물 (Python) (0) | 2022.02.22 |
---|---|
[νλ‘κ·Έλλ¨Έμ€] kμ§μμμ μμ κ°μ ꡬνκΈ° (Python) (0) | 2022.02.16 |
[νλ‘κ·Έλλ¨Έμ€] nμ§μ κ²μ (Python) (0) | 2022.02.16 |
[νλ‘κ·Έλλ¨Έμ€] μ£Όμ°¨ μκΈ κ³μ° (Python) (0) | 2022.02.13 |
[νλ‘κ·Έλλ¨Έμ€] μ€νμ±ν λ°© (Python) (0) | 2022.02.12 |