Skip to content

TypeScript solution 1046 ,23 #490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions typescript/1046-Last-Stone-Weight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
class MaxHeap {
heap: number[]
constructor(array: number[]) {
this.heap = this.buildHeap(array)
}

buildHeap(array: number[]) {
let parentIdx = Math.floor((array.length - 2) / 2)
for (let i = parentIdx; i >= 0; i--) {
this.siftDown(i, array.length - 1, array)
}
return array
}

siftDown(idx: number, endIdx: number, heap: number[]) {
let childOneIdx = 2 * idx + 1

while (childOneIdx <= endIdx) {
let childTwoIdx = 2 * idx + 2 <= endIdx ? 2 * idx + 2 : -1
let swapIdx
if (childTwoIdx !== -1 && heap[childOneIdx] < heap[childTwoIdx]) {
swapIdx = childTwoIdx
} else swapIdx = childOneIdx
if (heap[swapIdx] > heap[idx]) {
this.swap(swapIdx, idx, heap)
idx = swapIdx
childOneIdx = 2 * idx + 1
} else return
}
}

siftUp(idx: number, heap: number[]) {
let parentIdx = Math.floor((idx - 1) / 2)
while (heap[parentIdx] < heap[idx] && idx > 0) {
this.swap(parentIdx, idx, heap)
idx = parentIdx
parentIdx = Math.floor((idx - 1) / 2)
}
}

peek() {
return this.heap[0]
}

remove() {
this.swap(this.heap.length - 1, 0, this.heap)
const removeValue = this.heap.pop()
this.siftDown(0, this.heap.length - 1, this.heap)
return removeValue
}

size() {
return this.heap.length
}

insert(value: number) {
this.heap.push(value)
this.siftUp(this.heap.length - 1, this.heap)
}
swap(i: number, j: number, arr: number[]) {
let ele = arr[i]
arr[i] = arr[j]
arr[j] = ele
}
}

function lastStoneWeight(stones: number[]): number {
const heap = new MaxHeap(stones)

while (heap.size() > 1) {
const stone1 = heap.remove()
const stone2 = heap.remove()

heap.insert(stone1 - stone2)
}

return heap.peek()
}
52 changes: 52 additions & 0 deletions typescript/23-Merge-k-Sorted-Lists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

function mergeKLists(lists: Array<ListNode | null>): ListNode | null {
if (lists.length === 0) return null

while (lists.length > 1) {
let resultList = []

for (let i = 0; i < lists.length; i += 2) {
let list1 = lists[i]
let list2
if (i + 1 > lists.length) list2 = null
else list2 = lists[i + 1]
resultList.push(mergeList(list1, list2))
}

lists = resultList
}
return lists[0] || null
}

function mergeList(
list1: ListNode | null,
list2: ListNode | null
): ListNode | null {
let dummyNode: ListNode | null = new ListNode()
let tail = dummyNode

while (list1 && list2) {
if (list1.val < list2.val) {
tail.next = list1
list1 = list1.next
} else {
tail.next = list2
list2 = list2.next
}
tail = tail.next
}
tail.next = list1 || list2

return dummyNode.next
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy