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

[2023-08-07] sumin #93#101

Merged
ksumini merged 1 commit into
mainfrom
sumin-#93
Aug 7, 2023
Merged

[2023-08-07] sumin #93#101
ksumini merged 1 commit into
mainfrom
sumin-#93

Conversation

@ksumini
Copy link
Copy Markdown
Contributor

@ksumini ksumini commented Aug 5, 2023

PR Summary

풀이시간: 30분

<제한사항>

  • operations는 최대 1,000,000이하인 문자열 배열로 주어진다.
    -> 최대 O(nlogn)으로 해결해야 한다.
  • 최댓값/최솟값을 삭제하는 연산에서 최댓값/최솟값이 둘 이상인 경우, 하나만 삭제한다.
  • 빈 큐에 데이터를 삭제하라는 연산이 주어질 경우, 해당 연산은 무시한다.

<solution>

  • 파이썬의 heapq 라이브러리를 사용해 우선순위큐를 만든다.
  • 이 때, 파이썬의 heapq는 기본적으로 최소힙이기 때문에, 최소힙과 최대힙을 각각 만들어 주어진 연산을 수행한다.
    1) I 숫자: 최소힙과 최대힙 둘 다에 주어진 숫자를 삽입한다.
    2) D 1: 최대힙에서 최댓값을 삭제한다.
    3) D -1: 최소힙에서 최솟값을 삭제한다.
    -> 가장 중요한 포인트는 visited라는 방문배열을 만들어 해당 노드가 처리됐는지 확인할 수 있도록 한다.
    최대힙에서 삭제된 숫자가 최소힙에도 반영되게, 최소힙에서 삭제된 숫자가 최대힙에도 반영되게 하기 위함.

<시간복잡도>
n은 연산의 수, k는 힙에 저장된 원소의 개수라고 할 때,
삽입/삭제의 경우 모두 O(log(k))가 걸리기 때문에 전체 시간복잡도는 O(n * log k)가 된다.

<번외>
이 문제의 경우, 백준에도 테스트 케이스가 훨씬 많은 똑같은 이름의 거의 동일한 문제가 있다.
실제로 프로그래머스에서는 단순 구현이나 nsmallest, nlargest를 사용해 TLE가 나거나 반례가 있는 코드들도 정답처리 돼있다;
이 문제를 여러 번 풀어보고 또 다시 정리하며, 이 풀이가 정해에 가장 가까운 풀이일 거라고 생각한다.

ISSUE NUMBER

@ksumini ksumini self-assigned this Aug 5, 2023
@ksumini ksumini linked an issue Aug 5, 2023 that may be closed by this pull request
Copy link
Copy Markdown
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.

수민님의 코멘트를 보고 다시 백준 문제로 풀이해보고 싶어서 늦어지는 바람에 이 문제의 수민님 풀이에 대한 리뷰가 늦어졌네요 죄송합니다 ㅠㅠ 수민님의 경험 덕분에 항상 많은 것을 얻어가는 것 같습니다! 고생하셨어요!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

저의 경우에도 거의 동일한 로직으로 해결할 수 있었는데, 정해에 가까운 풀이라니 괜시리 뿌듯하네요! 수민님의 경험덕분에 항상 많이 얻어가는 것 같습니다!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

접근방식이 같은 것 같아서 읽기에 수월했습니다. 고생하셨습니다!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

최종 정답 출력 전에 모든 유효하지 않은 값들을 없애버리는 방법으로 정상적인 peek연산을 할 수 있게 만든 점 좋은 것 같습니다!

Copy link
Copy Markdown
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.

수민님 덕분에 제대로 다시 풀이 할 수 있었던 것 같습니다. 감사합니다! 프로그래머스 테스트 케이스가 정말 빈약하다는 것을 백준을 테스트 케이스 사용하면서 느낄 수 있었습니다. 힙과 우선순위 큐를 모두 배울 수 있어 좋았습니다. ㅎㅎ

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

접근방식이 같은 것 같아서 읽기에 수월했습니다. 고생하셨습니다!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

걸국 처리되는 값이 최종적인 최소, 최대 값이기 때문에 2,3번 케이스에서 while문을 통해 삭제하지 않아도 괜찮을 것 같습니다.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

49번째 라인과 57번째 라인의 while문이 없어도 된다는 뜻일까요..?! 제가 이해를 잘 못했어요!ㅜㅜ

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

스터디에서 해결 완료~!

@ksumini ksumini merged commit c58db76 into main Aug 7, 2023
@ksumini ksumini deleted the sumin-#93 branch August 7, 2023 09:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Programmers] 이중우선순위큐

3 participants