Skip to content

Commit a561d25

Browse files
update post '(Leetcode) 23 - Merge k Sorted Lists 풀이'
1 parent b63b035 commit a561d25

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
lines changed

_posts/2024-02-19-leetcode-23.md

+97-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
---
22
layout: post
3-
title: (Leetcode) 23 - Merge k Sorted Lists
3+
title: (Leetcode) 23 - Merge k Sorted Lists 풀이
44
categories: [스터디-알고리즘]
5-
tags: [파이썬, 알고리즘, python, algorithm, Leetcode, heap, quicksort]
5+
tags:
6+
[파이썬, 알고리즘, python, algorithm, Leetcode, heap, quicksort, 자바, Java]
67
date: 2024-02-19 20:00:00 +0900
78
image:
89
path: /assets/images/2024-02-19-leetcode-23/thumbnail.png
@@ -18,7 +19,7 @@ image:
1819

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

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

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

201202
![300만위 안으로 들어왔다](/assets/images/2024-02-19-leetcode-23/ranking_over_top_3M.png)
203+
204+
## 자바로 다시 풀기 (24.07.30)
205+
206+
### 풀이 1
207+
208+
lists를 순회하면서 가장 작은 수를 가진 노드를 찾고, 그 노드를 mergedList 에 추가한다.
209+
210+
```java
211+
class Solution {
212+
public ListNode mergeKLists(ListNode[] lists) {
213+
ListNode mergedList = new ListNode();
214+
215+
ListNode current = mergedList;
216+
217+
while (true) {
218+
boolean done = true;
219+
int minIndex = 0;
220+
int currentMin = Integer.MAX_VALUE;
221+
222+
for (int i = 0; i < lists.length; i++) {
223+
ListNode node = lists[i];
224+
if (node == null) {
225+
continue;
226+
}
227+
228+
if (node.val < currentMin) {
229+
minIndex = i;
230+
currentMin = node.val;
231+
done = false;
232+
}
233+
}
234+
235+
if (done) {
236+
break;
237+
}
238+
239+
current.next = lists[minIndex];
240+
lists[minIndex] = lists[minIndex].next;
241+
242+
current = current.next;
243+
}
244+
245+
return mergedList.next;
246+
}
247+
}
248+
```
249+
250+
#### TC, SC
251+
252+
문제에서 다음과 같이 정의가 되어있다.
253+
254+
```
255+
k == lists.length
256+
```
257+
258+
추가적으로 n을 list 들의 item 수의 총합 이라고 정의하였을 때
259+
260+
시간복잡도는 `O(n * k)`, 공간복잡도는 `O(n)` 이다.
261+
262+
### 풀이 2: stream 사용해서 풀기
263+
264+
우선 다 하나의 리스트에 합친 후 정렬한다.
265+
266+
```java
267+
class Solution {
268+
public ListNode mergeKLists(ListNode[] lists) {
269+
List<ListNode> mergedListNode = new ArrayList<>();
270+
for (ListNode listNode : lists) {
271+
ListNode current = listNode;
272+
while (current != null) {
273+
mergedListNode.add(current);
274+
current = current.next;
275+
}
276+
}
277+
278+
ListNode listNode = new ListNode();
279+
final ListNode[] current = {listNode};
280+
mergedListNode.stream().sorted(Comparator.comparingInt(node -> node.val))
281+
.forEach(node -> {
282+
current[0].next = node;
283+
current[0] = current[0].next;
284+
});
285+
286+
return listNode.next;
287+
}
288+
}
289+
```
290+
291+
예상과는 다르게 오히려 이 방식이 더 적은 실행시간으로 완료되었다.
292+
293+
#### TC, SC
294+
295+
n을 list 들의 item 수의 총합 이라고 정의하였을 때, 시간복잡도는 `O(nlogn)`, 공간복잡도는 `O(n)` 이다.

_posts/2024-07-29-leetcode-76.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ n == t.length
116116

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

119-
## 버전 2 :성능 우선 버전
119+
## 버전 2: 성능 우선 버전
120120

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

0 commit comments

Comments
 (0)