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
/** | |
* Valid Parentheses | |
* @param {string} s | |
* @return {boolean} | |
*/ | |
const validParentheses = (s) => { | |
if (s === null || !s.length) return true; | |
const chars = s.split(''); | |
const stack = []; |
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
const twoSum = (numbers:number[], k: number): Boolean => { | |
const rec:Record<number,number>= {}; | |
for(const n of numbers){ | |
rec[n] = n; | |
if(rec.hasOwnProperty(k-n)) return true; | |
} | |
return false; |
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
const uniqueNumber = (arr:number[]):number => arr.reduce((x,y) => x^y); |
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
const couldBeMadeNonDecreasing = (arr:number[]):Boolean => { | |
let decreasingPairs = 0 | |
for(let i in arr){ | |
if(arr[i] > arr[+i+1]){ | |
decreasingPairs++ | |
} | |
} | |
return decreasingPairs <= 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
class MaxStack { | |
private stack: number[] = new Array<number>(); | |
private maxStack: number[] = new Array<number>(); | |
private length: number = 0; | |
public push(item: number): void { | |
let previousMax = this.maxStack[this.length-1] || 0; | |
this.maxStack[this.length] = item > previousMax ? item : previousMax; | |
this.stack[this.length++] = item; |
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
// no recursion | |
const waysToClimbStairs = (n:number):number => { | |
const a:number[] = [1,1]; | |
if(n>1){ | |
for(let i = 2; i <= n ; i++){ | |
a[i] = a[i-1] + a[i-2]; | |
} | |
}; | |
return a[a.length - 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
// without recursion 💪 | |
const waysToClimbStairs = (n:number):number => { | |
const a:number[] = [1,1]; | |
if(n>1){ | |
for(let i = 2; i <= n ; i++){ | |
a[i] = a[i-1] + a[i-2]; | |
} | |
}; | |
return a[a.length - 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
const findPythagoreanTriplets = (arr:number[]):boolean => { | |
if(arr.length<3) return false; | |
const sorted = arr | |
.map(v => v*v) | |
.sort((a,b) => a-b); | |
let [l,r,i] = [0, sorted.length -2, sorted.length -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
const getEditDistance = function(a: string, b: string):number{ | |
if(!a.length) return b.length; | |
if(!b.length) return a.length; | |
const matrix:number[][] = []; | |
// fill first row and column | |
for(let i = 0; i <= b.length; i++){ | |
matrix[i] = [i]; |
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
const word_search = (arr:string[][], word: string) : boolean => { | |
if(word.length > arr.length) return false; | |
let horizontal = ''; | |
let vertical:{ [key: string]: string } = {}; | |
for(let i=0; i<arr.length;i++){ | |
for(let j=0; j<arr[i].length;j++){ | |
horizontal+= arr[i][j]; |
OlderNewer