Skip to content

Instantly share code, notes, and snippets.

View anish000kumar's full-sized avatar
🎯
Focusing

Anish Kumar anish000kumar

🎯
Focusing
  • Gurgaon, India
View GitHub Profile
@anish000kumar
anish000kumar / parentPostOrder.js
Last active August 28, 2021 05:29
PostOrder traversal using parent pointer
function Node(value, parent = null){
this.value = value
this.left = null
this.right = null
this.parent = parent
}
// create tree
@anish000kumar
anish000kumar / parentPreOrder.js
Created August 28, 2021 04:33
PreOrder traversal using parent pointer
function Node(value, parent = null){
this.value = value
this.left = null
this.right = null
this.parent = parent
}
// create tree
@anish000kumar
anish000kumar / parentInOrder.js
Created August 28, 2021 04:32
inOrder traversal with parent pointer
function Node(value, parent = null){
this.value = value
this.left = null
this.right = null
this.parent = parent
}
// create tree
@anish000kumar
anish000kumar / FenwickTree.js
Created June 4, 2021 05:39
FenwickTree Javascript implementation
class FenwickTree{
constructor(array){
this.source = [...array]
this.length = this.source.length;
this.fenwick = new Array(array.length+1).fill(0)
this.build();
}
build(){
for(let i = 0; i < this.length; i++)
@anish000kumar
anish000kumar / animationLoop.js
Last active June 3, 2021 11:51
Animation loop
function animate({ timing, duration, draw }){
return new Promise((resolve, reject) => {
const start = performance.now();
function animation(time){
const progress = Math.min((time-start)/duration, 1);
const visualProgress = timing ? timing(progress): progress;
draw(visualProgress);
if(progress < 1) requestAnimationFrame(animation)
@anish000kumar
anish000kumar / calendar.js
Created June 3, 2021 07:08
Calendar component in pure JS
class Calendar {
constructor(selector){
this.MONTHS= ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
this.root = document.querySelector(selector);
this.activeDateTime = new Date();
this.render();
this.attachEvents();
}
@anish000kumar
anish000kumar / coordinate compression.py
Last active December 7, 2020 16:34
coordinate compression
def compress(arr):
arr.sort();
compress_map = {}
val = 1
for el in arr:
if el not in compress_map:
compress_map[el] = val
val += 1
@anish000kumar
anish000kumar / MergeSort linkedList.py
Last active December 7, 2020 10:11
MergeSort linkedList
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def findMiddle(self, head):
slow, fast = head, head
while fast and fast.next and fast.next.next:
@anish000kumar
anish000kumar / Minimum swaps to sort an array.py
Created December 7, 2020 05:50
Minimum swaps to sort an array
def minSwaps(arr, N):
newArr = [ (arr[i], i) for i in range(len(arr)) ]
newArr.sort()
ans = 0
for i in range(len(newArr)):
j = newArr[i][1]
while newArr[i][1] != newArr[j][1]:
@anish000kumar
anish000kumar / Sliding window maximum.py
Created December 3, 2020 15:24
Sliding window maximum
from collections import deque
def maxSlidingWindow(nums, window_size):
start, ans = 0, []
q = deque();
for end in range(len(nums)):
while q and nums[end] > nums[q[-1]]: q.pop()