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
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) |
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
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 |
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
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 () { |
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
numArray.sort((a, d) => a - d); // For ascending sort | |
numArray.sort((a, d) => d - a); // For descending sort |
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 reverseLinkedList(head){ | |
let prev = null; | |
let next = null; | |
while(head){ | |
next = head.next; | |
head.next = prev; | |
prev = 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
class Node: | |
def __init__(self, value): | |
self.value = value | |
self.rank = 1 | |
self.parent = self | |
class DisjointSet: | |
def __init__(self): | |
self.mapping = {} | |
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
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: |
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
# 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): |
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
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 |
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
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: |
OlderNewer