Skip to content

Commit

Permalink
update post '(Leetcode) 23 - Merge k Sorted Lists 풀이'
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-jonghoonpark committed Jul 30, 2024
1 parent b63b035 commit a561d25
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 4 deletions.
100 changes: 97 additions & 3 deletions _posts/2024-02-19-leetcode-23.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
layout: post
title: (Leetcode) 23 - Merge k Sorted Lists
title: (Leetcode) 23 - Merge k Sorted Lists 풀이
categories: [스터디-알고리즘]
tags: [파이썬, 알고리즘, python, algorithm, Leetcode, heap, quicksort]
tags:
[파이썬, 알고리즘, python, algorithm, Leetcode, heap, quicksort, 자바, Java]
date: 2024-02-19 20:00:00 +0900
image:
path: /assets/images/2024-02-19-leetcode-23/thumbnail.png
Expand All @@ -18,7 +19,7 @@ image:

[https://leetcode.com/problems/merge-k-sorted-lists/description/](https://leetcode.com/problems/merge-k-sorted-lists/description/)

이번 문제는 heap으로 푸는 문제이다.
이번 문제는 heap으로 푸는 문제이다.
하지만 문제를 딱 보았을 때 sort로도 풀 수 있을것 같은데? 라는 생각이 들어서 두 가지 방법으로 모두 풀어보았다.

## heap으로 풀어보기
Expand Down Expand Up @@ -199,3 +200,96 @@ class Solution:
- 300만위 안으로 들어왔다.

![300만위 안으로 들어왔다](/assets/images/2024-02-19-leetcode-23/ranking_over_top_3M.png)

## 자바로 다시 풀기 (24.07.30)

### 풀이 1

lists를 순회하면서 가장 작은 수를 가진 노드를 찾고, 그 노드를 mergedList 에 추가한다.

```java
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
ListNode mergedList = new ListNode();

ListNode current = mergedList;

while (true) {
boolean done = true;
int minIndex = 0;
int currentMin = Integer.MAX_VALUE;

for (int i = 0; i < lists.length; i++) {
ListNode node = lists[i];
if (node == null) {
continue;
}

if (node.val < currentMin) {
minIndex = i;
currentMin = node.val;
done = false;
}
}

if (done) {
break;
}

current.next = lists[minIndex];
lists[minIndex] = lists[minIndex].next;

current = current.next;
}

return mergedList.next;
}
}
```

#### TC, SC

문제에서 다음과 같이 정의가 되어있다.

```
k == lists.length
```

추가적으로 n을 list 들의 item 수의 총합 이라고 정의하였을 때

시간복잡도는 `O(n * k)`, 공간복잡도는 `O(n)` 이다.

### 풀이 2: stream 사용해서 풀기

우선 다 하나의 리스트에 합친 후 정렬한다.

```java
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
List<ListNode> mergedListNode = new ArrayList<>();
for (ListNode listNode : lists) {
ListNode current = listNode;
while (current != null) {
mergedListNode.add(current);
current = current.next;
}
}

ListNode listNode = new ListNode();
final ListNode[] current = {listNode};
mergedListNode.stream().sorted(Comparator.comparingInt(node -> node.val))
.forEach(node -> {
current[0].next = node;
current[0] = current[0].next;
});

return listNode.next;
}
}
```

예상과는 다르게 오히려 이 방식이 더 적은 실행시간으로 완료되었다.

#### TC, SC

n을 list 들의 item 수의 총합 이라고 정의하였을 때, 시간복잡도는 `O(nlogn)`, 공간복잡도는 `O(n)` 이다.
2 changes: 1 addition & 1 deletion _posts/2024-07-29-leetcode-76.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ n == t.length

시간 복잡도는 `O(n * m)`, 공간 복잡도는 `O(n)` 이다.

## 버전 2 :성능 우선 버전
## 버전 2: 성능 우선 버전

사실 이 버전은 실제 어플리케이션에서는 쓰지 않을지도 모르겠다. 이해하는데 좀 더 시간이 걸릴 것 같기 때문이다.

Expand Down

0 comments on commit a561d25

Please sign in to comment.