This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
[2023-06-18] dohyun #69 #72
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
print(queue[0]) | ||
break | ||
queue.popleft() # 맨 위에 있는 카드 버림 | ||
out = queue.popleft() # 그 다음 위에 있는 카드를 선택 | ||
queue.append(out) # 해당 카드를 맨 밑으로 옮김 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
요렇게 쓰는 것 보다는
이렇게 쓰는 편이 더 깔끔할 것 같습니다!