This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
OlderNewer