Skip to content

Instantly share code, notes, and snippets.

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;
@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 ]
@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 / 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 / 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 / checkCycleInLikedList.js
Created May 16, 2021 16:18
[JS] Check if a linked list has a cycle
// Using strategy of slower and faster pointers
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
function hasCycle(head) {
@CarlaTeo
CarlaTeo / bucketSort.js
Created May 16, 2021 20:29
[JS] Bucket sort
// Bucket Sort: O(N) method to sort natural numbers
function bucketSort(array) {
const buckets = [];
array.forEach(number => {
if(!buckets[number]) buckets[number] = 1;
else buckets[number] += 1;
})
const sortedArray = [];
buckets.forEach((counter, number) => {
@CarlaTeo
CarlaTeo / mergeSort.js
Created May 16, 2021 20:53
[JS] Merge Sort
function mergeSort(array) {
if(array.length <= 1) return array;
const half = Math.floor(array.length / 2);
const left = array.slice(0, half);
const right = array.slice(half);
const sortedLeft = mergeSort(left);
const sortedRight = mergeSort(right);
return merge(sortedLeft, sortedRight);
@CarlaTeo
CarlaTeo / quickSort.js
Created May 16, 2021 21:03
[JS] Quick Sort
function quickSort(array) {
if(array.length <= 1) return array;
const pivot = array[0];
const [left, right] = pivotDivision(array.slice(1), pivot);
return [...quickSort(left), pivot, ...quickSort(right)];
}
function pivotDivision(array, pivot) {
const left = [];
@CarlaTeo
CarlaTeo / heapSort.js
Last active June 11, 2021 17:17
[JS] Heap sort
function heapSort(array) {
const heap = heapify(array);
let lengthToConsider = array.length;
for(let i = array.length - 1; i > 0; i--) {
swap(array, i, 0);
lengthToConsider--;
descendHeap(array, lengthToConsider, 0);
}
}