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
import { findNthFib, minChange, sumPossible } from "../FibAndCoinIssues"; | |
describe("Testing Finding Nth Fibonacci", () => { | |
test("findNthFib(0) == 0", () => { | |
expect(findNthFib(0)).toBe(0); | |
}); | |
test("findNthFib(5) == 5", () => { | |
expect(findNthFib(5)).toBe(5); | |
}); | |
test("findNthFib(35) == 9227465", () => { |
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
const cache = [0, 1]; | |
export function findNthFib(n: number, cache = [0, 1]) { | |
// check if the number is already calculated | |
if (n in cache) { | |
return cache[n]; | |
} | |
cache[n] = findNthFib(n - 1, cache) + findNthFib(n - 2, cache); | |
return cache[n]; | |
} |
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 class MinHeap { | |
private length: number = 0; | |
private data: number[] = []; | |
private getParentIndex(idx: number): number { | |
return Math.floor((idx - 1) / 2); | |
} | |
private getLeftIndex(idx: number): number { | |
return 2 * idx + 1; | |
} |
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
import { MinHeap } from "../MinHeap"; | |
const minHeap = new MinHeap(); | |
describe("Testing MinHeap", () => { | |
minHeap.clearHeap(); | |
let array = [30, 1, 2, 5, 7, 10, 20, 11, 1, 22, 2, 5, 17, 10, 20, -11]; | |
minHeap.buildHeapFromArray(array); | |
let sortedArray = array.sort((a, b) => a - b); |
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 class MinHeap { | |
private length: number = 0; | |
private data: number[] = []; | |
private getParentIndex(idx: number): number { | |
return Math.floor((idx - 1) / 2); | |
} | |
private getLeftIndex(idx: number): number { | |
return 2 * idx + 1; | |
} |
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
import { | |
permutationsOfString, | |
permutationsOfNumbers, | |
findNextGreaterPermutation, | |
} from "../Permutations/index"; | |
describe("Permutations of String", () => { | |
it("permutationsOfString('ABC'): ['ABC','ACB','BAC','BCA','CAB', 'CBA']", () => { | |
expect(permutationsOfString("ABC")).toEqual([ | |
"ABC", |
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 permutationsOfString(str: string): string[] { | |
const result: string[] = []; | |
function permute(prefix: string = "", string: string) { | |
if (prefix.length === str.length) { | |
result.push(prefix); | |
return; | |
} | |
for (let i = 0; i < string.length; i++) { | |
permute(prefix + string[i], string.slice(0, i) + string.slice(i + 1)); |
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
import { quickSort, mergeSort } from "../index"; | |
describe("QuickSort Testing", () => { | |
it("should sort array[1,3,4,5,2,1]", () => { | |
const array = [1, 3, 4, 5, 2, 1]; | |
const sortedArray = array.sort((a, b) => a - b); | |
quickSort(array); | |
expect(array).toEqual(sortedArray); | |
}); | |
it("should sort array[5, 4, 3, 2, 1, 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
function swap(array: number[], i: number, j: number) { | |
[array[i], array[j]] = [array[j], array[i]]; | |
} | |
// One way of doing partition (left to right scanning) | |
function partition1(array: number[], low: number, high: number): number { | |
let index = low - 1; // always points to the last element <= pivot | |
const pivotIndex = high; | |
const pivot = array[pivotIndex]; | |
for (let i = low; i < high; i++) { | |
if (array[i] <= pivot) { |
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
import { | |
BinaryTreeNode, | |
insertLevelOrder, | |
preOrder, | |
inOrder, | |
postOrder, | |
} from "../BinaryTree"; | |
let array1 = [1, 2, 3, 4, 5, 6, 7]; | |
let array2 = [1, 2, 3, 4, 5, 6, 6, 6, 6]; |
NewerOlder