Skip to content

Instantly share code, notes, and snippets.

View remi-bruguier's full-sized avatar
🎯

Rémi BRUGUIER remi-bruguier

🎯
View GitHub Profile
/**
* 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)){
@remi-bruguier
remi-bruguier / kalAsh.ts
Created July 6, 2020 19:29
Trouvez une chaîne de 9 caractères (seulement constituées des lettres "acdegilmnoprstuw") telle que hash(la_string) soit 956446786872726 si le hash est défini par le code suivant :
/* 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
} */
@remi-bruguier
remi-bruguier / buyAndSell.ts
Created June 13, 2020 20:43
You are given an array. Each element represents the price of a stock on that particular day. Calculate and return the maximum profit you can make from buying and selling that stock only once.
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;
}
}
@remi-bruguier
remi-bruguier / word_search.ts
Created June 7, 2020 18:08
You are given a 2D array of characters, and a target string. Return whether or not the target word exists in the matrix. Unlike a standard word search, the word must be either going left-to-right, or top-to-bottom in the matrix.
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];
@remi-bruguier
remi-bruguier / getEditDistance.ts
Created May 29, 2020 20:09
Given two strings, determine the edit distance between them. The edit distance is defined as the minimum number of edits (insertion, deletion, or substitution) needed to change one string to the other.
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];
@remi-bruguier
remi-bruguier / findPythagoreanTriplets.ts
Created May 25, 2020 20:32
Given a list of numbers, find if there exists a pythagorean triplet in that list. A pythagorean triplet is 3 variables a, b, c where a² + b² = c²
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];
// 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];
}
// 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];
}
@remi-bruguier
remi-bruguier / MaxStack.ts
Created May 21, 2020 15:06
Implement a class for a stack that supports all the regular functions (push, pop) and an additional function of max() which returns the maximum element in the stack (return None if the stack is empty). Each method should run in constant time.
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;
@remi-bruguier
remi-bruguier / couldBeMadeNonDecreasing.ts
Last active May 18, 2020 06:47
You are given an array of integers in an arbitrary order. Return whether or not it is possible to make the array non-decreasing by modifying at most 1 element to any value.
const couldBeMadeNonDecreasing = (arr:number[]):Boolean => {
let decreasingPairs = 0
for(let i in arr){
if(arr[i] > arr[+i+1]){
decreasingPairs++
}
}
return decreasingPairs <= 1
}