Skip to content

Instantly share code, notes, and snippets.

@optimistiks
optimistiks / 1-find-median-data-stream.js
Last active April 2, 2023 13:13
Find Median from a Data Stream
// Implement a data structure that’ll store a dynamically growing list of integers and provide access to their median in O(1)
class medianOfStream {
// elements smaller than current median (max-heap)
heapSmall = new Heap();
// elements larger than current median (min-heap)
heapLarge = new Heap();
length() {
// total length is a sum of both heaps lengths
return this.heapSmall.length() + this.heapLarge.length();
@optimistiks
optimistiks / 1-sliding-window-median.js
Last active April 5, 2023 12:59
Given an integer array nums and an integer k, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position, return the median of the current window.
// nums array of numbers, k sliding window size
// return median of every sliding window
// basically we keep two heaps,
// left heap is a max heap where all the numbers lt or equal to median are kept
// right heap is a min heap where all the numbers gt than median are kept
// this way sliding window median is always either left heap top, or avg of two tops
// elements who exit the sliding window are ignored unless they pop to the top of either heap
// then we drop them (we keep track of them leaving the window using a hashmap)
// we also need to make sure that right heap contains exactly half of the current sliding window numbers,
// so we rebalance the heaps if for example an element exited the window from the left heap, and entered to the right heap
@optimistiks
optimistiks / 1-schedule-tasks.js
Created April 5, 2023 13:32
Given a set of n number of tasks, implement a task scheduler method, to run in O(n log n) time that finds the minimum number of machines required to complete these n tasks.
// tasksList is an array of tuples [startTime, endTime]
export function tasks(tasksList) {
// keep our tasks in min-heap, with task with earliest start time on top
const tasksHeap = new TupleHeap();
// keep our machines in min heap, with machine whose task finishes earliest on top
const machinesHeap = new Heap();
// push all our tasks into min heap
for (let i = 0; i < tasksList.length; ++i) {
tasksHeap.push(tasksList[i]);
@optimistiks
optimistiks / merge-sorted-arrays.js
Created April 7, 2023 16:15
Given two sorted integer arrays, nums1 and nums2, and the number of data elements in each array, m and n, implement a function that merges the second array into the first one. You have to modify nums1 in place.
export function mergeSorted(nums1, m, nums2, n) {
let p = nums1.length - 1;
let p1 = m - 1;
let p2 = n - 1;
// our main condition is all elements from nums2 are merged into nums1
// meaning p2 (index in num2) became < 0
while (p2 >= 0) {
const num1 = nums1[p1];
const num2 = nums2[p2];
@optimistiks
optimistiks / 1-smallest-in-sorted-lists.js
Last active April 7, 2023 16:57
Given m number of sorted lists in ascending order and an integer k, find the kth smallest number among all the given lists.
export function kSmallestNumber(lists, k) {
const heap = new MinHeap();
// initialize by pushing first element of each list into heap
for (let i = 0; i < lists.length; ++i) {
const list = lists[i];
if (list[0] != null) {
heap.offer([list[0], i, 0]);
}
}
@optimistiks
optimistiks / 1-k-smallest-pairs.js
Created April 9, 2023 16:18
Given two arrays and an integer k, find k pairs of numbers with the smallest sum so that in each pair, each array contributes one number to the pair.
export function kSmallestPairs(list1, list2, k) {
let result = [];
const heap = new MinHeap();
// we initialize our heap by pairing the first element of list2
// with every element of list1
// since our arrays are sorted, our heap will contain our first min pair
for (let i = 0; i < list1.length; ++i) {
const a = list1[i];
const b = list2[0];
@optimistiks
optimistiks / 1-merge-k-sorted-lists.js
Created April 9, 2023 17:27
Given an array of k sorted linked lists, your task is to merge them into a single sorted list.
function mergeTwoLists(list1, list2) {
let a = list1.head;
let b = list2.head;
let ll = null;
let tail = null;
while (a !== null || b !== null) {
let nextNode = null;
@optimistiks
optimistiks / 1-kth-smallest-in-sorted-matrix.js
Created April 9, 2023 17:41
Find the kth smallest element in an (n x n) matrix, where each row and column of the matrix is sorted in ascending order.
export function kthSmallestNumber(matrix, k) {
const heap = new MinHeap();
// initialize by pushing first element of each row into heap
// since rows and cols are sorted, these are first n smallest elements in the matrix,
// where n is the number of rows / cols
for (let i = 0; i < matrix.length; ++i) {
heap.offer([matrix[i][0], i, 0]);
}
@optimistiks
optimistiks / median-of-two-sorted-arrays.js
Created April 12, 2023 21:14
You’re given two sorted integer arrays, nums1 and nums2, of size m and n, respectively. Your task is to return the median of the two sorted arrays. The overall run time complexity should be O(log(m+n)).
export function findMedian(nums1, nums2) {
const totalLength = nums1.length + nums2.length;
const leftHalfLength = Math.floor(totalLength / 2);
const listA = nums1.length < nums2.length ? nums1 : nums2;
const listB = nums1.length < nums2.length ? nums2 : nums1;
let l = 0;
let r = listA.length - 1;
@optimistiks
optimistiks / 1-kth-largest-from-stream.js
Created April 13, 2023 21:29
Given an infinite stream of integers, nums, design a class to find the kth largest element in a stream.
class KthLargest {
// constructor to initialize heap and add values in it
constructor(k, nums) {
// we add all elements into the heap, and then pop until we have k elements
// those k elements are k largest elements from the stream,
// and the minimum of those elements is in the top of the heap,
// and this is the element we need - the kth largest element
const heap = new MinHeap(nums);
while (heap.size() > k) {
heap.poll();