Skip to content

Instantly share code, notes, and snippets.

View hawaijar's full-sized avatar
🎯
Focusing

Mayengbam Sushil K hawaijar

🎯
Focusing
View GitHub Profile
@hawaijar
hawaijar / Heap.test.ts
Created August 24, 2022 08:12
MinHeap Test cases
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);
@hawaijar
hawaijar / index.ts
Created August 24, 2022 08:11
MinHeap
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;
}
@hawaijar
hawaijar / index.test.ts
Created August 22, 2022 05:07
Permutations test cases
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",
@hawaijar
hawaijar / index.ts
Created August 22, 2022 05:06
Permutations
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));
@hawaijar
hawaijar / index.test.ts
Created August 21, 2022 06:53
Test cases for Quick, Merge sorts
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]", () => {
@hawaijar
hawaijar / index.ts
Created August 21, 2022 06:53
Quick Sort, Merge Sort
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) {
@hawaijar
hawaijar / BinaryTree.test.ts
Created August 20, 2022 05:37
BinaryTreeTraversal Test cases
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];
@hawaijar
hawaijar / BinaryTree.ts
Created August 20, 2022 05:37
Binary Tree Traversals
export class BinaryTreeNode {
value: number;
left: BinaryTreeNode | null;
right: BinaryTreeNode | null;
constructor(value: number) {
this.value = value;
this.left = null;
this.right = null;
}
@hawaijar
hawaijar / index.ts
Created August 18, 2022 16:41
Shortest Path from A to B in a Maze
// write a function that takes X by X array of arrays of numbers
// as well two x/y combinations and have it return the shortest
// length (you don't need to track the actual path) from point A
// to point B.
//
// the numbers in the maze array represent as follows:
// 0 – open space
// 1 - closed space, cannot pass through. a wall
// 2 - one of the two origination points
interface Point {
@hawaijar
hawaijar / index.ts
Created August 17, 2022 04:32
Kadane's Algorithm
//Given an array of integers (containing positive and negative value),
// find the maximum sum of a contiguous SubArray.
// Detail: https://github.com/hawaijar/FireLeetcode/blob/feature/algoexpert/Famous%20Algorithms/KadaneAlgorithm/README.md
(() => {
function kadaneAlgorithm(array: number[]) {
// base case
if (array.length === 0) {
return 0;
}
if (array.length <= 1) {