Skip to content

Commit 0731901

Browse files
authored
Merge pull request neetcode-gh#490 from dipti95/TypeScript_Solution
TypeScript solution 1046 ,23
2 parents a72aca1 + e5fa42a commit 0731901

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
class MaxHeap {
2+
heap: number[]
3+
constructor(array: number[]) {
4+
this.heap = this.buildHeap(array)
5+
}
6+
7+
buildHeap(array: number[]) {
8+
let parentIdx = Math.floor((array.length - 2) / 2)
9+
for (let i = parentIdx; i >= 0; i--) {
10+
this.siftDown(i, array.length - 1, array)
11+
}
12+
return array
13+
}
14+
15+
siftDown(idx: number, endIdx: number, heap: number[]) {
16+
let childOneIdx = 2 * idx + 1
17+
18+
while (childOneIdx <= endIdx) {
19+
let childTwoIdx = 2 * idx + 2 <= endIdx ? 2 * idx + 2 : -1
20+
let swapIdx
21+
if (childTwoIdx !== -1 && heap[childOneIdx] < heap[childTwoIdx]) {
22+
swapIdx = childTwoIdx
23+
} else swapIdx = childOneIdx
24+
if (heap[swapIdx] > heap[idx]) {
25+
this.swap(swapIdx, idx, heap)
26+
idx = swapIdx
27+
childOneIdx = 2 * idx + 1
28+
} else return
29+
}
30+
}
31+
32+
siftUp(idx: number, heap: number[]) {
33+
let parentIdx = Math.floor((idx - 1) / 2)
34+
while (heap[parentIdx] < heap[idx] && idx > 0) {
35+
this.swap(parentIdx, idx, heap)
36+
idx = parentIdx
37+
parentIdx = Math.floor((idx - 1) / 2)
38+
}
39+
}
40+
41+
peek() {
42+
return this.heap[0]
43+
}
44+
45+
remove() {
46+
this.swap(this.heap.length - 1, 0, this.heap)
47+
const removeValue = this.heap.pop()
48+
this.siftDown(0, this.heap.length - 1, this.heap)
49+
return removeValue
50+
}
51+
52+
size() {
53+
return this.heap.length
54+
}
55+
56+
insert(value: number) {
57+
this.heap.push(value)
58+
this.siftUp(this.heap.length - 1, this.heap)
59+
}
60+
swap(i: number, j: number, arr: number[]) {
61+
let ele = arr[i]
62+
arr[i] = arr[j]
63+
arr[j] = ele
64+
}
65+
}
66+
67+
function lastStoneWeight(stones: number[]): number {
68+
const heap = new MaxHeap(stones)
69+
70+
while (heap.size() > 1) {
71+
const stone1 = heap.remove()
72+
const stone2 = heap.remove()
73+
74+
heap.insert(stone1 - stone2)
75+
}
76+
77+
return heap.peek()
78+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function mergeKLists(lists: Array<ListNode | null>): ListNode | null {
14+
if (lists.length === 0) return null
15+
16+
while (lists.length > 1) {
17+
let resultList = []
18+
19+
for (let i = 0; i < lists.length; i += 2) {
20+
let list1 = lists[i]
21+
let list2
22+
if (i + 1 > lists.length) list2 = null
23+
else list2 = lists[i + 1]
24+
resultList.push(mergeList(list1, list2))
25+
}
26+
27+
lists = resultList
28+
}
29+
return lists[0] || null
30+
}
31+
32+
function mergeList(
33+
list1: ListNode | null,
34+
list2: ListNode | null
35+
): ListNode | null {
36+
let dummyNode: ListNode | null = new ListNode()
37+
let tail = dummyNode
38+
39+
while (list1 && list2) {
40+
if (list1.val < list2.val) {
41+
tail.next = list1
42+
list1 = list1.next
43+
} else {
44+
tail.next = list2
45+
list2 = list2.next
46+
}
47+
tail = tail.next
48+
}
49+
tail.next = list1 || list2
50+
51+
return dummyNode.next
52+
}

0 commit comments

Comments
 (0)