Skip to content

Commit 5500c32

Browse files
committed
Added more solns to Tries and Heaps
1 parent 05a1415 commit 5500c32

8 files changed

+774
-92
lines changed

.ipynb_checkpoints/1D Dynamic Programming-checkpoint.ipynb

+57-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "dc1e0dea",
6+
"metadata": {},
7+
"source": [
8+
"# Easy"
9+
]
10+
},
311
{
412
"cell_type": "markdown",
513
"id": "c338ceed",
@@ -141,28 +149,65 @@
141149
]
142150
},
143151
{
144-
"cell_type": "code",
145-
"execution_count": null,
146-
"id": "9e61dc65",
152+
"cell_type": "markdown",
153+
"id": "21330a90",
147154
"metadata": {},
148-
"outputs": [],
149-
"source": []
155+
"source": [
156+
"# Medium"
157+
]
150158
},
151159
{
152-
"cell_type": "code",
153-
"execution_count": null,
154-
"id": "21c91ae2",
160+
"cell_type": "markdown",
161+
"id": "daf1717a",
155162
"metadata": {},
156-
"outputs": [],
157-
"source": []
163+
"source": [
164+
"## House Robber\n",
165+
"\n",
166+
"* https://leetcode.com/problems/house-robber/\n",
167+
"***\n",
168+
"* Time Complexity: O(n)\n",
169+
" - you're basically just looping from 2 ... n\n",
170+
"* Space Complexity: O(1)\n",
171+
" - only using 2 pointers x and y to accumulate the maxes as we go along\n",
172+
"***\n",
173+
"* similar to Fibonacci\n",
174+
"* start from the base case:\n",
175+
" - [] = 0 b/c no houses\n",
176+
" - [1] = 1 b/c just 1 house to rob\n",
177+
" - [1, 2] = 2 since they're adjacent, we can either rob house 1 or house 2, not both so we take the max of those 2\n",
178+
" - [1, 2, 3] = 4 b/c, starting at index 1, we can either get loot from house[i] + houses we've robbed[i - 2] OR we just rob the previous house\n",
179+
" - so essentially, we rob the previous house OR the previous subarray of houses\n",
180+
" - that's why we need to do [x, y] = [Math.max(y, nums[i] + x]\n",
181+
" - and also the reason why we need to set y = Math.max(nums[0], nums[1]);\n",
182+
" * b/c it is the max of the previous subarray we've seen"
183+
]
158184
},
159185
{
160186
"cell_type": "code",
161-
"execution_count": null,
187+
"execution_count": 1,
162188
"id": "ce55a56e",
163189
"metadata": {},
164190
"outputs": [],
165-
"source": []
191+
"source": [
192+
"/**\n",
193+
" * @param {number[]} nums\n",
194+
" * @return {number}\n",
195+
" */\n",
196+
"var rob = function(nums) {\n",
197+
" if (nums.length <= 1) {\n",
198+
" return nums.length ? nums[0] : 0;\n",
199+
" }\n",
200+
" \n",
201+
" let x = nums[0];\n",
202+
" let y = Math.max(nums[0], nums[1]);\n",
203+
" \n",
204+
" for (let i = 2; i < nums.length; i++) {\n",
205+
" [x, y] = [y, Math.max(y, nums[i] + x)];\n",
206+
" }\n",
207+
" \n",
208+
" return y;\n",
209+
"};"
210+
]
166211
},
167212
{
168213
"cell_type": "code",

.ipynb_checkpoints/Heaps and Priority Queues-checkpoint.ipynb

+208-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "8160c7a8",
6+
"metadata": {},
7+
"source": [
8+
"# Easy"
9+
]
10+
},
311
{
412
"cell_type": "markdown",
513
"id": "cd94da5c",
@@ -270,33 +278,221 @@
270278
]
271279
},
272280
{
273-
"cell_type": "code",
274-
"execution_count": null,
275-
"id": "a4466c1d",
281+
"cell_type": "markdown",
282+
"id": "4c463d86",
276283
"metadata": {},
277-
"outputs": [],
278-
"source": []
284+
"source": [
285+
"# Medium"
286+
]
279287
},
280288
{
281-
"cell_type": "code",
282-
"execution_count": null,
283-
"id": "0ce3de02",
289+
"cell_type": "markdown",
290+
"id": "84914655",
284291
"metadata": {},
285-
"outputs": [],
286-
"source": []
292+
"source": [
293+
"## K Closest Points to Origin\n",
294+
"\n",
295+
"* https://leetcode.com/problems/k-closest-points-to-origin/description/\n",
296+
"***\n",
297+
"* Time Complexity: O(nlogk)\n",
298+
" - there are 2 parts to this:\n",
299+
" 1. we create a Max Priority Queue and add the first k elements to it\n",
300+
" - for the rest of the points, we compare it with the root. if the value is less than the root, we pop the root and enqueue the new coordinate\n",
301+
" 2. we then return all k values of the minHeap as the answer\n",
302+
" - the first step will be the dominant term, O(nlogk)\n",
303+
" * reason being, we have to check all n points in the points array and if we have to enqueue/dequeue, that process will be O(logk) b/c there are only k elements in the Max PQ\n",
304+
" - the second step will just be O(k) which is dropped\n",
305+
"* Space Complexity: O(k)\n",
306+
" - we create a Max Priority Queue of size k and we dequeue any values if we are adding more to it\n",
307+
"***\n",
308+
"* the reason why we use a Max PQ is b/c it is very easy to compare and drop the max value\n",
309+
" - if we encounter a coordinate smaller than the max, we can drop the max and add that coordinate PQ\n",
310+
" - when we do this, the next max will bubble up\n",
311+
" - in addition, we also do not add any values to the Max PQ greater than the max value if we already have k coordinates in it\n",
312+
" - these 2 things ensure that max values are weeded out and that no other max values can be added in\n",
313+
" - and if we maintain a PQ of size k, can we just return all the values in it as the answer without doing anything special to get it"
314+
]
287315
},
288316
{
289317
"cell_type": "code",
290318
"execution_count": null,
291319
"id": "f92bb982",
292320
"metadata": {},
293321
"outputs": [],
294-
"source": []
322+
"source": [
323+
"/**\n",
324+
" * @param {number[][]} points\n",
325+
" * @param {number} k\n",
326+
" * @return {number[][]}\n",
327+
" */\n",
328+
"\n",
329+
"function getParentIndex(index) {\n",
330+
" return Math.floor((index - 1) / 2);\n",
331+
"}\n",
332+
"\n",
333+
"function getLeftChildIndex(index) {\n",
334+
" return (index * 2) + 1;\n",
335+
"}\n",
336+
"\n",
337+
"function getRightChildIndex(index) {\n",
338+
" return (index * 2) + 2;\n",
339+
"}\n",
340+
"\n",
341+
"function hasLeftChild(array, index) {\n",
342+
" return getLeftChildIndex(index) < array.length;\n",
343+
"}\n",
344+
"\n",
345+
"function hasRightChild(array, index) {\n",
346+
" return getRightChildIndex(index) < array.length;\n",
347+
"}\n",
348+
"\n",
349+
"function swap(array, index1, index2) {\n",
350+
" const value = array[index1];\n",
351+
" array[index1] = array[index2];\n",
352+
" array[index2] = value;\n",
353+
"}\n",
354+
"\n",
355+
"function heapifyUp(array, compare) {\n",
356+
" let currentIndex = array.length - 1;\n",
357+
"\n",
358+
" while (currentIndex > 0) {\n",
359+
" let parentIndex = getParentIndex(currentIndex);\n",
360+
" if (compare(array[parentIndex], array[currentIndex]) > 0) {\n",
361+
" swap(array, parentIndex, currentIndex);\n",
362+
" currentIndex = parentIndex;\n",
363+
" } else {\n",
364+
" break;\n",
365+
" }\n",
366+
" }\n",
367+
"}\n",
368+
"\n",
369+
"function heapifyDown(array, compare) {\n",
370+
" let currentIndex = 0;\n",
371+
" while (hasLeftChild(array, currentIndex)) {\n",
372+
" let smallerChildIndex = getLeftChildIndex(currentIndex);\n",
373+
"\n",
374+
" if (hasRightChild(array, currentIndex)) {\n",
375+
" let rightChildIndex = getRightChildIndex(currentIndex);\n",
376+
" if (compare(array[smallerChildIndex], array[rightChildIndex]) > 0) {\n",
377+
" smallerChildIndex = rightChildIndex;\n",
378+
" }\n",
379+
" }\n",
380+
"\n",
381+
" if (compare(array[currentIndex], array[smallerChildIndex]) > 0) {\n",
382+
" swap(array, currentIndex, smallerChildIndex);\n",
383+
" currentIndex = smallerChildIndex;\n",
384+
" } else {\n",
385+
" break;\n",
386+
" }\n",
387+
" }\n",
388+
"\n",
389+
"}\n",
390+
"const array = Symbol(\"array\");\n",
391+
"const compare = Symbol(\"compare\");\n",
392+
"\n",
393+
"class BinaryHeap {\n",
394+
" constructor(comparator = (a, b) => a - b) {\n",
395+
" this[array] = [];\n",
396+
" this[compare] = comparator;\n",
397+
" }\n",
398+
" \n",
399+
" add(data) {\n",
400+
" this[array].push(data);\n",
401+
" heapifyUp(this[array], this[compare]);\n",
402+
" }\n",
403+
"\n",
404+
" isEmpty() {\n",
405+
" return this[array].length === 0;\n",
406+
" }\n",
407+
"\n",
408+
" peek() {\n",
409+
" if (this.isEmpty()) {\n",
410+
" throw new Error(\"Heap is empty.\");\n",
411+
" }\n",
412+
"\n",
413+
" return this[array][0];\n",
414+
" }\n",
415+
" \n",
416+
" poll() {\n",
417+
" if (this.isEmpty()) {\n",
418+
" throw new Error(\"Heap is empty.\");\n",
419+
" }\n",
420+
"\n",
421+
" if (this[array].length > 1) {\n",
422+
" const topValue = this[array][0];\n",
423+
"\n",
424+
" const replacementValue = this[array].pop();\n",
425+
" this[array][0] = replacementValue;\n",
426+
" heapifyDown(this[array], this[compare]);\n",
427+
"\n",
428+
" return topValue;\n",
429+
" } else {\n",
430+
" return this[array].pop();\n",
431+
" }\n",
432+
" \n",
433+
" }\n",
434+
" \n",
435+
" get size() {\n",
436+
" return this[array].length;\n",
437+
" }\n",
438+
"\n",
439+
" includes(value) {\n",
440+
" return this[array].includes(value);\n",
441+
" }\n",
442+
"\n",
443+
" clear() {\n",
444+
" this[array] = [];\n",
445+
" }\n",
446+
"\n",
447+
" [Symbol.iterator]() {\n",
448+
" return this.values();\n",
449+
" }\n",
450+
"\n",
451+
" values() {\n",
452+
" return this[array].values();\n",
453+
" }\n",
454+
" \n",
455+
" toString(){\n",
456+
" return [...this[array]].toString();\n",
457+
" }\n",
458+
"}\n",
459+
"\n",
460+
"\n",
461+
" var euDist = (coords) => {\n",
462+
" const [x, y] = coords;\n",
463+
" const powX = Math.pow(0 - x, 2);\n",
464+
" const powY = Math.pow(0 - y, 2);\n",
465+
" return Math.sqrt(powX + powY);\n",
466+
" }\n",
467+
"\n",
468+
"var kClosest = function(points, k) {\n",
469+
" const minHeap = new BinaryHeap((a, b) => euDist(b) - euDist(a))\n",
470+
" for (let point of points) {\n",
471+
" if (minHeap.size < k) {\n",
472+
" minHeap.add(point);\n",
473+
" }\n",
474+
" else if (euDist(point) < euDist(minHeap.peek())) {\n",
475+
" minHeap.poll();\n",
476+
" minHeap.add(point);\n",
477+
" }\n",
478+
" }\n",
479+
"\n",
480+
" return [...minHeap];\n",
481+
"};"
482+
]
483+
},
484+
{
485+
"cell_type": "markdown",
486+
"id": "6e24ee3d",
487+
"metadata": {},
488+
"source": [
489+
"## Task Scheduler"
490+
]
295491
},
296492
{
297493
"cell_type": "code",
298494
"execution_count": null,
299-
"id": "35a1e745",
495+
"id": "8c45c87d",
300496
"metadata": {},
301497
"outputs": [],
302498
"source": []

.ipynb_checkpoints/Stack-checkpoint.ipynb

+20-15
Original file line numberDiff line numberDiff line change
@@ -320,26 +320,31 @@
320320
" * @param {number[]} temperatures\n",
321321
" * @return {number[]}\n",
322322
" */\n",
323+
"\n",
323324
"var dailyTemperatures = function(temperatures) {\n",
324-
" // initialize the answer array by filling it with 0s\n",
325-
" let answer = Array.from({length: temperatures.length}, () => 0);\n",
326-
" let stack = [];\n",
327-
" \n",
328-
" \n",
325+
" const output = [];\n",
326+
" const stack = [];\n",
327+
"\n",
329328
" for (let i = 0; i < temperatures.length; i++) {\n",
330-
" // if the stack is empty, just push the current index\n",
331-
" // else, while the temperature[index at top of stack] < current temperature\n",
332-
" // update it\n",
333-
" // so if we haven't seen a larger temperature yet, just push it to the stack\n",
334-
" // else if we do see it, then update all values in the stack\n",
335-
" while (stack.length > 0 && temperatures[stack[stack.length - 1]] < temperatures[i]) {\n",
336-
" let index = stack.pop();\n",
337-
" answer[index] = i - index;\n",
329+
" output.push(0);\n",
330+
" while (stack.length > 0) {\n",
331+
" // check top of stack\n",
332+
" // if the current temp is greater than stack, then pop it off\n",
333+
" // and update the output\n",
334+
" let stackTop = stack[stack.length - 1];\n",
335+
" if (temperatures[i] > temperatures[stackTop]) {\n",
336+
" output[stackTop] = i - stackTop;\n",
337+
" stack.pop();\n",
338+
" }\n",
339+
" else {\n",
340+
" break;\n",
341+
" }\n",
338342
" }\n",
343+
"\n",
344+
" // push current index into the stack\n",
339345
" stack.push(i);\n",
340346
" }\n",
341-
" \n",
342-
" return answer;\n",
347+
" return output;\n",
343348
"};"
344349
]
345350
},

0 commit comments

Comments
 (0)