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
var Queue = (()=>{
const map = new WeakMap();
let _items = []
class Queue{
constructor(...items){
//initialize the items in queue
map.set(this, []);
_items = map.get(this);
// enqueuing the items passed to the constructor
this.enqueue(...items)
@anish000kumar
anish000kumar / Stack.js
Last active July 27, 2018 15:21
Stack implementation in javascript (es6)
let Stack = (()=>{
let map = new WeakMap();
let _items = [];
class Stack {
constructor(...items){
// let's store our items array inside the weakmap
map.set(this, []);
// let's get the items array from map, and store it in _items for easy access elsewhere
@anish000kumar
anish000kumar / hackerearth_js_boilerplate.js
Last active October 23, 2018 20:04
hackerearch js boilerplate
process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";
process.stdin.on("data", function (input) {
stdin_input += input; // Reading input from STDIN
});
process.stdin.on("end", function () {
@anish000kumar
anish000kumar / numsort.js
Last active November 8, 2018 13:52
Sort array of numbers
numArray.sort((a, d) => a - d); // For ascending sort
numArray.sort((a, d) => d - a); // For descending sort
@anish000kumar
anish000kumar / reverseLinkedList.js
Last active November 9, 2018 17:44
reverse a linked list
function reverseLinkedList(head){
let prev = null;
let next = null;
while(head){
next = head.next;
head.next = prev;
prev = head;
@anish000kumar
anish000kumar / UnionFind.py
Last active July 18, 2020 22:09
Quick Disjoint Set in python (Union Find with path compression)
class Node:
def __init__(self, value):
self.value = value
self.rank = 1
self.parent = self
class DisjointSet:
def __init__(self):
self.mapping = {}
@anish000kumar
anish000kumar / Trie.py
Last active July 19, 2020 04:23
Trie implementation in python
class Node:
def __init__(self, val=None):
self.val = val
self.children = {}
self.count = 0. # number of times words with this prefix
self.end = 0 # number of times this word has been inserted
class Trie:
@anish000kumar
anish000kumar / Rejection Sampling.py
Last active August 24, 2020 09:38
Using rejection sampling to generate random number
# The rand7() API is already defined for you.
# def rand7():
# @return a random integer in the range 1 to 7
class Solution:
def generate(self):
row, col = rand7(), rand7()
return ((row-1) * 7 ) + col
def rand10(self):
@anish000kumar
anish000kumar / power.py
Created August 29, 2020 23:35
Pow(x, n)
def myPow(self, x: float, n: int) -> float:
ans, power = 1, abs(n)
while power:
if power % 2 == 0:
x *= x
power //= 2
else:
ans *= x
power -= 1
def getClosest(nums, k):
if k < nums[0]: return nums[0]
if k > nums[len(nums)-1]: return nums[len(nums)-1]
left, right = 0 , len(nums)-1
while left <= right:
mid = left + (right-left)//2
if nums[mid] > k: