Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

[2023-06-18] dohyun #69 #72

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions BOJ/카드2/dohyun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""

풀이시간
- 약 5분

접근법
- 모든 동작 방식이 deqeue 로 구현가능
- 특정 조건 만족까지 반복되는 행위 -> 반복문/재귀로도 풀이 가능

회고
- 쉬웠던 것 같아서 재귀 풀이도 하나 넣어봤습니다!!
- 재귀문이 시간이 2배 이상 걸리는데 원래 그런걸까용?

"""

### 반복문 풀이
from collections import deque

N = int(input()) # 정수 N 입력받음
queue = deque([i for i in range(1, N+1)]) # 카드 덱 구성

while True:
if len(queue)==1:
Copy link
Contributor

Choose a reason for hiding this comment

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

요렇게 쓰는 것 보다는

while len(queue)!=1:
    ...

print(queue[0])

이렇게 쓰는 편이 더 깔끔할 것 같습니다!

print(queue[0])
break
queue.popleft() # 맨 위에 있는 카드 버림
out = queue.popleft() # 그 다음 위에 있는 카드를 선택
queue.append(out) # 해당 카드를 맨 밑으로 옮김
Copy link
Contributor

Choose a reason for hiding this comment

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

요것도

queue.append(queue.popleft())

요렇게 자주 씁니다! popleft & pop은 원소를 제거함과 동시에 그 원소를 반환해주니까요!



### 재귀 풀이
import sys
sys.setrecursionlimit(10 ** 6) # 재귀 깊이 제한을 풀어줌 (재귀문제에서 필수라고 함)
from collections import deque

N = int(input())
queue = deque([i for i in range(1, N+1)])

def run(cards):
Copy link
Contributor

Choose a reason for hiding this comment

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

재귀로 풀어볼 생각은 1도 못했는데 재밌네요!!
재귀를 활용하게 되면 재귀가 거듭되면서 수행한 작업들을 메모리 영역에 모두 들고 있어야 해서 오버헤드가 더 심해집니다! 그래서 시간이나 메모리가 더 소요되는 것 같아요

if len(cards) == 1:
print(cards[0])
else:
cards.popleft()
out = cards.popleft()
cards.append(out)
run(cards)

run(queue)