Skip to content

Instantly share code, notes, and snippets.

@optimistiks
optimistiks / reverse-linked-list-between.js
Created March 3, 2023 21:14
Reverse the list’s nodes from position left to position right and return the reversed list.
export function reverseBetween(head, left, right) {
// node that precedes our sublist (left - 1)
let subListPrev = null;
// node that is start of our sublist (left)
let subListHead = null;
// node that is end of our sublist (right)
let subListTail = null;
// node that is after our sublist (right + 1)
let subListNext = null;
@optimistiks
optimistiks / reverse-linked-list-groups.js
Last active March 3, 2023 21:18
Reverse the nodes of the linked list, k nodes at a time. If the number of nodes is not a multiple of k, the nodes left in the end will remain in their original order.
export function reverseLinkedList(head, k) {
let i = 0;
let resultHead = head;
// node preceding our sublist
let subPrev = null;
// node after our sublist
let subNext = head.next;
// start of our sublist
@optimistiks
optimistiks / longest-substring-repeats.js
Created March 3, 2023 21:20
Find the longest substring without repeating characters, and return it's length.
export function findLongestSubstring(inputString) {
// in this map we store last seen indexes of each unique character in the inputString
const map = {};
// this is the maximum seen length of the substring so far
let length = 0;
// this is the leftmost element of the max len substring seen so far
let start = 0;
for (let i = 0; i < inputString.length; ++i) {
@optimistiks
optimistiks / max-in-sliding-window.js
Created March 3, 2023 21:22
Given an integer array and a window of size w, find the current maximum value in the window as it slides through the entire array.
export function findMaxSlidingWindow(nums, w) {
// the resulting array containing maximum values of each sliding window
const result = [];
if (w > nums.length) {
// if window size is greater than nums length,
// we consider length as window size (single window)
w = nums.length;
}
@optimistiks
optimistiks / min-window-subsequence.js
Created March 3, 2023 21:23
Given strings str1 and str2, find the minimum (contiguous) substring subStr of str1, such that every character of str2 appears in subStr in the same order as it is present in str2. If there are multiple minimum-length windows, return the one with the leftmost starting index.
export function findMinWindowSubsequence(str1, str2) {
// initialize two pointers
// pointer 1 follows str1
let indexStr1 = 0;
// pointer2 follows str2
let indexStr2 = 0;
// this is our resulting minimum substring
let minSubstr = "";
// start iterating str1
for (let i = 0; i < str1.length; ++i) {
@optimistiks
optimistiks / min-window-substring.js
Created March 3, 2023 21:25
Given two strings s and t, find the smallest window substring of t. The smallest window substring is the shortest sequence of characters in s that includes all of the characters present in t. The frequency of each character in this sequence should be greater than or equal to the frequency of each character in t. The order of the characters doesn…
export function minWindowSubstring(s, t) {
if (t === "") {
return "";
}
// count of each unique character in string t
const tCount = {};
// count of each unique character in the current sliding window
const sCount = {};
// count of unique characters in t
@optimistiks
optimistiks / repeated-dna-sequences.js
Created March 3, 2023 21:27
Given a string s that represents a DNA sequence, and a number k, return all the contiguous sequences (substrings) of length k that occur more than once in the string. The order of the returned subsequences does not matter.
// how many distinct characters are used in the string (our constant for the hash function)
const a = 4;
// characters mapped to numbers to use in the hash function
const values = {
A: 1,
C: 2,
G: 3,
T: 4,
};
@optimistiks
optimistiks / reverse-nodes-even-groups.js
Last active March 24, 2023 00:36
Reverse nodes in even length groups
export function reverseEvenLengthGroups(head) {
if (!head.next) {
return head;
}
// we skip the first node since it's an odd group 1, and start with the second node and the second group
// this is the max number of nodes in a group we're currently iterating (can be less nodes if it's the last group)
let currentGroupLength = 2;
// this is the number of nodes we visited in the current group
let currentGroupNodesCount = 0;
@optimistiks
optimistiks / swap-nodes-in-pairs.js
Created March 29, 2023 15:31
Given a singly linked list, swap every two adjacent nodes of the linked list. After the swap, return the head of the linked list.
export function swapPairs(head) {
let newHead = head;
// first we initialize our first pair of nodes to swap,
// and that is head node and the head.next node
// node preceding the pair to swap
let prev = null;
// first node of the pair to swap
let first = head;
@optimistiks
optimistiks / 1-find-maximum-capital.js
Last active April 2, 2023 12:25
Find maximum possible capital
// c is my capital
// k is times I want to invest
// capitals is list of min capitals of projects to invest
// profits is how much profit a project brings
export function maximumCapital(c, k, capitals, profits) {
// in this heap we are going to store capitals that
// are required by projects to invest into them
// top element is always the cheapest project to invest to
const capHeap = new TupleHeap();