Skip to content

Instantly share code, notes, and snippets.

View carefree-ladka's full-sized avatar
🏠
Working from home

Pawan Kumar carefree-ladka

🏠
Working from home
View GitHub Profile
@carefree-ladka
carefree-ladka / SlidingWindow.js
Last active March 30, 2024 13:15
SlidingWindow MinSumSubArray
const nums = [3, 5, 2, 1, 7]
const getMinSum = (nums, k) => {
let min = Number.MAX_VALUE
let curr = 0
let start = 0
let end = 0
for (let windowEnd = 0; windowEnd < nums.length; windowEnd++) {
const counter = (num, duration) => {
let current = 1;
const increment = () => {
console.log(current);
current >= num ? clearInterval(timer) : current++
};
const timer = setInterval(increment, duration);
};
@carefree-ladka
carefree-ladka / pokemon.js
Created May 5, 2024 03:41
List all pokemons by based on their gender
//https://pokeapi.co/api/v2/pokemon?limit=100
//https://pokeapi.co/api/v2/gender/1
//https://pokeapi.co/api/v2/pokemon/1000
const fetcher = async (url) => {
try {
const res = await fetch(url)
if (res.ok || res.status === 200) return await res.json()
}
catch (e) {
class Node {
constructor(val = 0, left = null, right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
const root = new Node(6)
root.left = new Node(3)
@carefree-ladka
carefree-ladka / Trie.js
Last active August 21, 2024 09:24
Trie implementation in JavaScript with Autocomplete feature
class TrieNode {
constructor() {
this.children = {}
this.eow = false
this.word = ""
}
}
class Trie {
@carefree-ladka
carefree-ladka / QuickSelect.js
Created May 13, 2024 15:06
QUICK SELECT ALGORITHM
const quickSelect = (nums, k, kthMax) => {
if (!nums.length) return;
const P = nums[Math.floor(Math.random() * nums.length)];
const left = [];
const mid = [];
const right = [];
nums.forEach(x => {
@carefree-ladka
carefree-ladka / Flatten.js
Created May 17, 2024 12:23
Object flatten in JS
const getAllKeys = (obj) => {
const result = [];
const queue = [{ path: [], obj }];
while (queue.length) {
const { path, obj: currentObj } = queue.shift();
for (const [key, value] of Object.entries(currentObj)) {
const newPath = path.concat(key);
if (typeof value === 'object' && value !== null) {
queue.push({ path: newPath, obj: value });
@carefree-ladka
carefree-ladka / NryTree.js
Created May 18, 2024 12:39
Nry Tree Implementation
class Node {
constructor(val) {
this.val = val;
this.children = [];
}
addChild = (child) => this.children.push(child)
}
@carefree-ladka
carefree-ladka / Twitter.js
Created May 22, 2024 15:48
Twitter Like System
class Twitter {
constructor() {
this.users = {};
this.tweets = [];
this.followers = {};
}
postTweet(userId, content) {
const tweet = {
id: this.tweets.length,
@carefree-ladka
carefree-ladka / QuickSelect.js
Created June 11, 2024 14:16
Quick Select Algorithm for finding Kth Max Element
function partition(a, lo, hi) {
/*
//Improve partition choice for the worst case
const randomIndex = Math.floor(Math.random() * (hi - lo + 1)) + lo;
swap(a, lo, randomIndex); // Move the random pivot to the start
*/
let i = lo;
let j = hi + 1;
let P = a[lo];