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
/** | |
* Given a string, we want to remove 2 adjacent characters that are the same, and repeat the process with the new string until we can no longer perform the operation. | |
* | |
* @param base the original string | |
* | |
* @returns the `cleansed` string | |
*/ | |
const remove_adjacent_dup = (base:string): string => { | |
const adjacentDupRegex = new RegExp(/(\w)\1/g); | |
while(adjacentDupRegex.test(base)){ |
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
/* Int64 hash (String s) { | |
Int64 h = 7 | |
String letters = "acdegilmnoprstuw" | |
for(Int32 i = 0; i < s.length; i++) { | |
h = (h * 37 + letters.indexOf(s[i])) | |
} | |
return h | |
} */ | |
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 buy_and_sell = (values:number[]):number => { | |
let maxProfit = 0; | |
let minPrice = Number.MAX_SAFE_INTEGER; | |
for(let val of values){ | |
if(val<minPrice) { | |
minPrice = val; | |
}else if(val - minPrice > maxProfit){ | |
maxProfit = val - minPrice; | |
} | |
} |
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]; |
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 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
// 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
// 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
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
const couldBeMadeNonDecreasing = (arr:number[]):Boolean => { | |
let decreasingPairs = 0 | |
for(let i in arr){ | |
if(arr[i] > arr[+i+1]){ | |
decreasingPairs++ | |
} | |
} | |
return decreasingPairs <= 1 | |
} |
NewerOlder