You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
이중 힙 구현에서 삭제 연산의 시간 복잡도를 개선할 필요가 있습니다. 현재 구현은 O(log n) 삭제 연산 시 추가 순회로 인해 실제 성능이 저하될 수 있습니다.
if(value==="1"){// MAX heap에서 삭제while(maxHeap.size()>0){constx=maxHeap.pop();if(deleted.has(x.id))continue;else{deleted.add(x.id);break;}}}elseif(value==="-1"){// MIN heap에서 삭제while(minHeap.size()>0){constx=minHeap.pop();if(deleted.has(x.id))continue;else{deleted.add(x.id);break;}}}
+const deleteFromHeap = (heap, isMax) => {+ while (heap.size() > 0) {+ const x = heap.pop();+ if (!deleted.has(x.id)) {+ deleted.add(x.id);+ break;+ }+ }+};+
for (const operation of operations) {
const [op, value] = operation.split(" ");
if (op === "I") {
const num = Number(value);
minHeap.push({ id, value: num });
maxHeap.push({ id, value: num });
id++;
} else if (op === "D") {
- if (value === "1") {- // MAX heap에서 삭제- while (maxHeap.size() > 0) {- const x = maxHeap.pop();- if (deleted.has(x.id)) continue;- else {- deleted.add(x.id);- break;- }- }- } else if (value === "-1") {- // MIN heap에서 삭제- while (minHeap.size() > 0) {- const x = minHeap.pop();- if (deleted.has(x.id)) continue;- else {- deleted.add(x.id);- break;- }- }- }+ value === "1" + ? deleteFromHeap(maxHeap, true) + : deleteFromHeap(minHeap, false);
}
}
Suggestion importance[1-10]: 8
__
Why: The suggestion effectively extracts a common deletion logic into a separate function deleteFromHeap, reducing code duplication and improving the overall maintainability of the code. The abstraction makes the code more modular and easier to understand.
Medium
중복 코드 제거 및 결과 반환 로직 개선
현재 popValid 함수는 중복 코드를 포함하고 있습니다. 이 함수를 더 간결하고 효율적으로 리팩토링할 수 있습니다. 또한 최종 결과 반환 로직을 개선하여 코드의 가독성을 높일 수 있습니다.
Why: The suggestion reduces code duplication in the popValid function and simplifies the result return logic. The improved code is more concise and readable, with a cleaner approach to handling edge cases.
Medium
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
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.
User description
오늘도 멋져요 👍✨
PR Type
Enhancement
Description
프로그래머스 42628 이중우선순위큐 문제 해결
최소/최대 힙을 사용한 복잡한 알고리즘 구현
고유 ID를 통한 삭제 추적 메커니즘 개발