From 3dbba604de2f7ae2d0b5bfdeb1e9e5ce719d5b4f Mon Sep 17 00:00:00 2001 From: Glazomer Date: Fri, 23 Oct 2020 23:29:28 +0300 Subject: [PATCH 1/5] Made it from first time somehow --- ContainerWithMostWater.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 ContainerWithMostWater.py diff --git a/ContainerWithMostWater.py b/ContainerWithMostWater.py new file mode 100644 index 0000000..aa4a803 --- /dev/null +++ b/ContainerWithMostWater.py @@ -0,0 +1,21 @@ +class Solution: + def maxArea(self, h: List[int]) -> int: + # Here, instead of trying to do N^2 brute force, we go smart way + # We search for the highest peak from side were we have lowest peak. + # If for example left peak is higher than right, we want to find same hight or more peak on right, so we have possibility to beat our result. + # When we search the higher peak, we hope that length decreasing (r - l) if gonna be beaten by peak growth + # (h[l] or h[r] will be much greater than lMax or rMax respectivly) + res = 0 + l, r = 0, len(h) - 1 + lMax = rMax = 0 + while l < r: + lMax = max(h[l], lMax) + rMax = max(h[r], rMax) + res = max(min(h[l],h[r]) * (r - l), res) + if rMax > lMax: + l += 1 + else: + r -= 1 + + return res + # To be honest, I just got that solution in the head somehow... From 1fca9babdebe83bf84d78d041111e6280bad9142 Mon Sep 17 00:00:00 2001 From: Glazomer Date: Sun, 25 Oct 2020 21:00:41 +0300 Subject: [PATCH 2/5] My js implementation from min heap --- heap.ts | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.ts | 10 ++++++++ 2 files changed, 82 insertions(+) create mode 100644 heap.ts create mode 100644 test.ts diff --git a/heap.ts b/heap.ts new file mode 100644 index 0000000..1ba7982 --- /dev/null +++ b/heap.ts @@ -0,0 +1,72 @@ +export default class Heap { + #heap: number[]; + + // shortcuts + #parent = (i: number): number => Math.floor((i - 2) / 2); + #left = (i: number): number => i * 2 + 1; + #right = (i: number): number => i * 2 + 2; + + constructor(heap: number[] = []) { + this.#heap = heap; + for (let i = heap.length - 1; i >= 0; --i) { + this.#heapifyBottom(i); + } + } + + pop(): number { + const heap = this.#heap, + result = heap[0], + last = heap.pop(); + if (last && heap.length) { + // checking var 'last', so tsling do not swear... + heap[0] = last; + this.#heapifyBottom(0); + } + return result; + } + + push(num: number): void { + this.#heap.push(num); + this.#heapifyTop(this.#heap.length - 1); + } + + #heapifyBottom = (i: number): void => { + const heap = this.#heap; + + while (1) { + const l = this.#left(i), + r = this.#right(i); + if (r < heap.length && heap[r] < heap[i] && heap[r] < heap[l]) { + [heap[i], heap[r]] = [heap[r], heap[i]]; + i = r; + } else if (l < heap.length && heap[l] < heap[i] && heap[l] < heap[r]) { + [heap[i], heap[l]] = [heap[l], heap[i]]; + i = l; + } else { + break; + } + } + }; + + #heapifyTop = (i: number): void => { + const heap = this.#heap; + + while (0 < i) { + const p = this.#parent(i); + if (0 <= p && heap[i] < heap[p]) { + [heap[p], heap[i]] = [heap[i], heap[p]]; + } + i = p; + } + }; + + get [Symbol.toStringTag](): string { + return 'Heap'; + } + get [Symbol.iterator](): () => IterableIterator { + return this.#heap[Symbol.iterator].bind(this.#heap); + } + get heap(): number[] { + return [...this.#heap]; + } +} diff --git a/test.ts b/test.ts new file mode 100644 index 0000000..68a6c34 --- /dev/null +++ b/test.ts @@ -0,0 +1,10 @@ +// @ts-ignore +import Heap from './heap.ts'; +const h = new Heap([14, 12, 6, 23, 11, 19, 17, 3, 4]); +console.log(h.heap); +console.log(h.pop()); +console.log(h.pop()); +console.log(h.pop()); +h.push(7); +console.log(h.pop()); +console.log(h.pop()); From df211b79cf9ba2173717d12f93e9996415184a08 Mon Sep 17 00:00:00 2001 From: Glazomer Date: Wed, 28 Oct 2020 21:54:36 +0300 Subject: [PATCH 3/5] removed bugs --- MaxHeap.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ MinHeap.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ MinHeap.ts | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 204 insertions(+) create mode 100644 MaxHeap.js create mode 100644 MinHeap.js create mode 100644 MinHeap.ts diff --git a/MaxHeap.js b/MaxHeap.js new file mode 100644 index 0000000..fd21f9c --- /dev/null +++ b/MaxHeap.js @@ -0,0 +1,68 @@ +export default class Heap { + #heap; + + #parent = (i) => Math.floor((i - 2) / 2); + #left = (i) => i * 2 + 1; + #right = (i) => i * 2 + 2; + + constructor(heap = []) { + this.#heap = heap; + heap.reduceRight((_, __, i) => this.#heapifyBottom(i)); + } + + pop() { + const heap = this.#heap, + result = heap[0], + last = heap.pop(); + if (heap.length) { + heap[0] = last; + this.#heapifyBottom(0); + } + return result; + } + + push(num) { + this.#heap.push(num); + this.#heapifyTop(this.#heap.length - 1); + } + + #heapifyBottom = (i) => { + const heap = this.#heap; + + while (1) { + const l = this.#left(i), + r = this.#right(i); + if (heap[r] > heap[i] && !(heap[r] < heap[l])) { + [heap[i], heap[r]] = [heap[r], heap[i]]; + i = r; + } else if (heap[l] > heap[i] && !(heap[l] < heap[r])) { + [heap[i], heap[l]] = [heap[l], heap[i]]; + i = l; + } else { + break; + } + } + }; + + #heapifyTop = (i) => { + const heap = this.#heap; + + while (0 < i) { + const p = this.#parent(i); + if (0 <= p && heap[i] > heap[p]) { + [heap[p], heap[i]] = [heap[i], heap[p]]; + } + i = p; + } + }; + + get [Symbol.toStringTag]() { + return 'Heap'; + } + get [Symbol.iterator]() { + return this.#heap[Symbol.iterator].bind(this.#heap); + } + get heap() { + return [...this.#heap]; + } +} diff --git a/MinHeap.js b/MinHeap.js new file mode 100644 index 0000000..2825b86 --- /dev/null +++ b/MinHeap.js @@ -0,0 +1,68 @@ +export default class Heap { + #heap; + + #parent = (i) => Math.floor((i - 2) / 2); + #left = (i) => i * 2 + 1; + #right = (i) => i * 2 + 2; + + constructor(heap = []) { + this.#heap = heap; + heap.reduceRight((_, __, i) => this.#heapifyBottom(i)); + } + + pop() { + const heap = this.#heap, + result = heap[0], + last = heap.pop(); + if (heap.length) { + heap[0] = last; + this.#heapifyBottom(0); + } + return result; + } + + push(num) { + this.#heap.push(num); + this.#heapifyTop(this.#heap.length - 1); + } + + #heapifyBottom = (i) => { + const heap = this.#heap; + + while (1) { + const l = this.#left(i), + r = this.#right(i); + if (heap[r] < heap[i] && !(heap[r] > heap[l])) { + [heap[i], heap[r]] = [heap[r], heap[i]]; + i = r; + } else if (heap[l] < heap[i] && !(heap[l] > heap[r])) { + [heap[i], heap[l]] = [heap[l], heap[i]]; + i = l; + } else { + break; + } + } + }; + + #heapifyTop = (i) => { + const heap = this.#heap; + + while (0 < i) { + const p = this.#parent(i); + if (0 <= p && heap[i] < heap[p]) { + [heap[p], heap[i]] = [heap[i], heap[p]]; + } + i = p; + } + }; + + get [Symbol.toStringTag]() { + return 'Heap'; + } + get [Symbol.iterator]() { + return this.#heap[Symbol.iterator].bind(this.#heap); + } + get heap() { + return [...this.#heap]; + } +} diff --git a/MinHeap.ts b/MinHeap.ts new file mode 100644 index 0000000..792fb59 --- /dev/null +++ b/MinHeap.ts @@ -0,0 +1,68 @@ +export default class Heap { + #heap: number[]; + + #parent = (i: number): number => Math.floor((i - 2) / 2); + #left = (i: number): number => i * 2 + 1; + #right = (i: number): number => i * 2 + 2; + + constructor(heap: number[] = []) { + this.#heap = heap; + heap.reduceRight((_, __, i: number): any => this.#heapifyBottom(i)); + } + + pop(): number { + const heap = this.#heap, + result = heap[0], + last = heap.pop(); + if (heap.length) { + heap[0] = last; + this.#heapifyBottom(0); + } + return result; + } + + push(num: number): void { + this.#heap.push(num); + this.#heapifyTop(this.#heap.length - 1); + } + + #heapifyBottom = (i: number): void => { + const heap = this.#heap; + + while (1) { + const l = this.#left(i), + r = this.#right(i); + if (heap[r] < heap[i] && !(heap[r] > heap[l])) { + [heap[i], heap[r]] = [heap[r], heap[i]]; + i = r; + } else if (heap[l] < heap[i] && !(heap[l] > heap[r])) { + [heap[i], heap[l]] = [heap[l], heap[i]]; + i = l; + } else { + break; + } + } + }; + + #heapifyTop = (i: number): void => { + const heap = this.#heap; + + while (0 < i) { + const p = this.#parent(i); + if (0 <= p && heap[i] < heap[p]) { + [heap[p], heap[i]] = [heap[i], heap[p]]; + } + i = p; + } + }; + + get [Symbol.toStringTag](): string { + return 'Heap'; + } + get [Symbol.iterator](): () => IterableIterator { + return this.#heap[Symbol.iterator].bind(this.#heap); + } + get heap(): number[] { + return [...this.#heap]; + } +} From 68ca8eef1393faf977f963fce7e50e6a598c4c1d Mon Sep 17 00:00:00 2001 From: Glazomer Date: Wed, 28 Oct 2020 21:57:10 +0300 Subject: [PATCH 4/5] A file was deleted --- heap.ts | 72 --------------------------------------------------------- 1 file changed, 72 deletions(-) delete mode 100644 heap.ts diff --git a/heap.ts b/heap.ts deleted file mode 100644 index 1ba7982..0000000 --- a/heap.ts +++ /dev/null @@ -1,72 +0,0 @@ -export default class Heap { - #heap: number[]; - - // shortcuts - #parent = (i: number): number => Math.floor((i - 2) / 2); - #left = (i: number): number => i * 2 + 1; - #right = (i: number): number => i * 2 + 2; - - constructor(heap: number[] = []) { - this.#heap = heap; - for (let i = heap.length - 1; i >= 0; --i) { - this.#heapifyBottom(i); - } - } - - pop(): number { - const heap = this.#heap, - result = heap[0], - last = heap.pop(); - if (last && heap.length) { - // checking var 'last', so tsling do not swear... - heap[0] = last; - this.#heapifyBottom(0); - } - return result; - } - - push(num: number): void { - this.#heap.push(num); - this.#heapifyTop(this.#heap.length - 1); - } - - #heapifyBottom = (i: number): void => { - const heap = this.#heap; - - while (1) { - const l = this.#left(i), - r = this.#right(i); - if (r < heap.length && heap[r] < heap[i] && heap[r] < heap[l]) { - [heap[i], heap[r]] = [heap[r], heap[i]]; - i = r; - } else if (l < heap.length && heap[l] < heap[i] && heap[l] < heap[r]) { - [heap[i], heap[l]] = [heap[l], heap[i]]; - i = l; - } else { - break; - } - } - }; - - #heapifyTop = (i: number): void => { - const heap = this.#heap; - - while (0 < i) { - const p = this.#parent(i); - if (0 <= p && heap[i] < heap[p]) { - [heap[p], heap[i]] = [heap[i], heap[p]]; - } - i = p; - } - }; - - get [Symbol.toStringTag](): string { - return 'Heap'; - } - get [Symbol.iterator](): () => IterableIterator { - return this.#heap[Symbol.iterator].bind(this.#heap); - } - get heap(): number[] { - return [...this.#heap]; - } -} From 0df068eb29652a866cf6165181b93ac3a84b9cb6 Mon Sep 17 00:00:00 2001 From: Glazomer Date: Wed, 28 Oct 2020 23:00:34 +0300 Subject: [PATCH 5/5] Ranames --- MaxHeap.js | 2 +- MinHeap.js | 2 +- MinHeap.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/MaxHeap.js b/MaxHeap.js index fd21f9c..41fedf1 100644 --- a/MaxHeap.js +++ b/MaxHeap.js @@ -1,4 +1,4 @@ -export default class Heap { +export default class MaxHeap { #heap; #parent = (i) => Math.floor((i - 2) / 2); diff --git a/MinHeap.js b/MinHeap.js index 2825b86..0f3495f 100644 --- a/MinHeap.js +++ b/MinHeap.js @@ -1,4 +1,4 @@ -export default class Heap { +export default class MinHeap { #heap; #parent = (i) => Math.floor((i - 2) / 2); diff --git a/MinHeap.ts b/MinHeap.ts index 792fb59..7dad94f 100644 --- a/MinHeap.ts +++ b/MinHeap.ts @@ -1,4 +1,4 @@ -export default class Heap { +export default class MinHeap { #heap: number[]; #parent = (i: number): number => Math.floor((i - 2) / 2);