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

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

merged 1 commit into from
Jun 29, 2023

Conversation

limstonestone
Copy link
Contributor

PR Summary

풀이시간

  • 약 5분

접근법

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

회고

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

ISSUE NUMBER

Copy link
Contributor

@y2r1m y2r1m left a comment

Choose a reason for hiding this comment

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

재귀로도 풀이하신 게 인상 깊네요 ㅎㅎ 근데 시간이 2배 이상 걸린다니.. 저도 이유가 궁금하네요!!

@ksumini
Copy link
Contributor

ksumini commented Jun 18, 2023

image

재귀를 쓰면 함수를 호출할 때마다 운영체제 CS에서 배웠던 메모리 공간의 스택 영역에 계속 쌓이게 돼요!!
그럼 고정된 메모리 사용량을 가지는 반복문에 비해서 스택 메모리 사용량이 늘어나고 속도 저하가 발생할 수 밖에 없겠죠?? (아마도?! 틀릴 수도 있음 ㅎㅎ)

Copy link
Contributor

@zsmalla zsmalla left a comment

Choose a reason for hiding this comment

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

큐에 대한 이해를 완벽하게 잘 하신 것 같고, 재귀에 대해서도 이제 이해를 하시게 된 것 같아서 제가 다 뿌듯하네요!

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])

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

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은 원소를 제거함과 동시에 그 원소를 반환해주니까요!

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

@limstonestone limstonestone linked an issue Jun 19, 2023 that may be closed by this pull request
Copy link
Member

@Woo-Yeol Woo-Yeol left a comment

Choose a reason for hiding this comment

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

전체적인 풀이가 비슷하네요 이번에 재귀로 접근하시는 걸 보고 재귀 복습을 확실히 하신 것 같아 감탄이 나옵니다!! 고생하셨어요!

@limstonestone limstonestone merged commit 70b0ecf into main Jun 29, 2023
@limstonestone limstonestone deleted the dohyun-#69 branch June 29, 2023 16:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BOJ] 카드2
5 participants