Skip to content

Instantly share code, notes, and snippets.

@CarlaTeo
CarlaTeo / populationPeakYear.js
Created May 16, 2021 03:43
[JS] Given list of births and deaths find year with most people alive
// Given list of births and deaths find year with most people alive (in case of tie return any)
function maxPopulation(yearsOfBirthNDeath) {
const populationChange = [];
yearsOfBirthNDeath.forEach(([birth, death]) => {
if(populationChange[birth]) populationChange[birth] += 1;
else populationChange[birth] = 1;
const yearAfterDeath = death + 1;
if(populationChange[yearAfterDeath]) populationChange[yearAfterDeath] -= 1;
else populationChange[yearAfterDeath] = -1;
@CarlaTeo
CarlaTeo / binaryTreeLevelsAverage.js
Created May 16, 2021 01:32
[JS] Binary tree average level value
// Using BFS to solve
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null
}
}
function getBinaryTreeLevelsAverage(head) {
@CarlaTeo
CarlaTeo / trieAutocomplete.js
Last active June 28, 2021 00:06
[JS] Auto-complete feature using Trie
// Based on this problem: https://www.geeksforgeeks.org/auto-complete-feature-using-trie/
// We are given a Trie with a set of strings stored in it. Now the user types in a prefix of his search query, we need to give him all recommendations to auto-complete his query based on the strings stored in the Trie. We assume that the Trie stores past searches by the users.
// For example if the Trie store {“abc”, “abcd”, “aa”, “abbbaba”} and the User types in “ab” then he must be shown {“abc”, “abcd”, “abbbaba”}.
function searchAutocomplete(head, prefix) {
const words = [];
if(!prefix) return words;
let currentNode = head;
for(let letter of prefix) {
@CarlaTeo
CarlaTeo / symlink_patches.diff
Last active November 22, 2019 20:51 — forked from rodrigoalmeidaee/symlink_patches.diff
list of patches for jest to handle symlinks properly [ jest 24.7.1 ]
package com.example.gk.np;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;