Skip to content

Instantly share code, notes, and snippets.

View MohammedALREAI's full-sized avatar
😉
I may be slow to respond.

Mohammed Al-Reai MohammedALREAI

😉
I may be slow to respond.
View GitHub Profile
@MohammedALREAI
MohammedALREAI / leetcode two Number Sum.ts
Last active February 15, 2021 13:40
leetcode two Number Sum typescript
// TimeColextity nlog(n) space (1)
function twoNumberSum(array:Array<number>, targetSum:number):Array<number|undefined>{
// we need to sort array
let sortArr= array.sort((a,b)=>a-b);
let left =0;
let right=sortArr.length-1;
while(left<right){
let sumNumber=sortArr[left]+sortArr[right]
@MohammedALREAI
MohammedALREAI / NodeDepth
Created February 14, 2021 21:56
NodeDepth Algrothmic
class TreeNode {
val: number
left: TreeNode | null
right: TreeNode | null
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = (val===undefined ? 0 : val)
this.left = (left===undefined ? null : left)
this.right = (right===undefined ? null : right)
}
}
@MohammedALREAI
MohammedALREAI / Binary Search leetcode.ts
Created February 15, 2021 13:39
Binary Search leetcode
function search(nums: number[], target: number): number {
let left=0;
let right=nums.length-1
while (left <= right) {
const middle = Math.floor((left + right) / 2);
const potentialMatch = nums[middle];
if (target === potentialMatch) {
return middle;
@MohammedALREAI
MohammedALREAI / 509. Fibonacci Number.ts
Created February 15, 2021 18:44
509. Fibonacci Number lettcode
function fib(n: number): number {
const arr = [1, 1];
let counter = 3;
while (counter <= n) {
const nextFib = arr[0] + arr[1];
arr[0] = arr[1];
arr[1] = nextFib;
counter++;
}
return n< 1 ? 0: n===1? 1: arr[1]
@MohammedALREAI
MohammedALREAI / threeNumberSum.ts
Created February 16, 2021 06:27
threeNumberSum AlgoExpo
function threeNumberSum(array:Array<number>, targetSum:number):Array<Array<number>> {
array= array.sort((a, b) => a - b);
const triplets = [];
for (let i = 0; i < array.length - 2; i++) {
let left = i + 1;
let right = array.length - 1;
while (left < right) {
const currentSum = array[i] + array[left] + array[right];
if (currentSum === targetSum) {
triplets.push([array[i], array[left], array[right]]);
@MohammedALREAI
MohammedALREAI / smallestDifference.ts
Created February 16, 2021 07:45
smallestDifference AlgoExport
function smallestDifference(arr1:Array<number>,arr2:Array<number>):Array<number>{
arr1=arr1.sort((a,b)=>a-b)
arr2=arr2.sort((a,b)=>a-b)
let p1=0
let p2=0
let smallest = Infinity;
let current = Infinity;
let smallestPair:Array<number> = [];
@MohammedALREAI
MohammedALREAI / moveToEnd.ts
Created February 16, 2021 08:28
moveToEnd AlgoExport
function moveToEnd(arr:Array<number>,target:number){
if(arr.includes(target)) return arr
let p1=0
let p2=arr.length-1
while(p1<p2){
while(arr[p2]===target) p2--;
if(arr[p1]=== target ){
swap(arr,p1,p2);
p1++
}
@MohammedALREAI
MohammedALREAI / balancedBrackets.ts
Created February 20, 2021 08:05
balancedBrackets leetcode
function isValid(s:string ):boolean{
interface matchingBrac{
[key:string]:string}
const open="{[(";
const close="}])"
const matchingBrackets:matchingBrac = {
')': '(',
@MohammedALREAI
MohammedALREAI / firstNotRepeatingCharacter.ts
Created February 28, 2021 11:24
firstNotRepeatingCharacter typescript
interface Imap{
[key:string]:number
}
function firstNotRepeatingCharacter(s: string="abacabad"): string {
let map:Imap ={}
for (let e of s){
if(e in map){
@MohammedALREAI
MohammedALREAI / firstDuplicate.ts
Created February 28, 2021 11:25
firstDuplicate google
function firstDuplicate(a: number[]=[2, 1, 3, 5, 3, 2]): number {
let set = new Set()
for (let e of a){
if (set.has(e))
return e
else
set.add(e)
}