|
1 | 1 | ---
|
2 | 2 | layout: post
|
3 |
| -title: (Leetcode) 23 - Merge k Sorted Lists |
| 3 | +title: (Leetcode) 23 - Merge k Sorted Lists 풀이 |
4 | 4 | categories: [스터디-알고리즘]
|
5 |
| -tags: [파이썬, 알고리즘, python, algorithm, Leetcode, heap, quicksort] |
| 5 | +tags: |
| 6 | + [파이썬, 알고리즘, python, algorithm, Leetcode, heap, quicksort, 자바, Java] |
6 | 7 | date: 2024-02-19 20:00:00 +0900
|
7 | 8 | image:
|
8 | 9 | path: /assets/images/2024-02-19-leetcode-23/thumbnail.png
|
|
18 | 19 |
|
19 | 20 | [https://leetcode.com/problems/merge-k-sorted-lists/description/](https://leetcode.com/problems/merge-k-sorted-lists/description/)
|
20 | 21 |
|
21 |
| -이번 문제는 heap으로 푸는 문제이다. |
| 22 | +이번 문제는 heap으로 푸는 문제이다. |
22 | 23 | 하지만 문제를 딱 보았을 때 sort로도 풀 수 있을것 같은데? 라는 생각이 들어서 두 가지 방법으로 모두 풀어보았다.
|
23 | 24 |
|
24 | 25 | ## heap으로 풀어보기
|
@@ -199,3 +200,96 @@ class Solution:
|
199 | 200 | - 300만위 안으로 들어왔다.
|
200 | 201 |
|
201 | 202 | 
|
| 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)` 이다. |
0 commit comments