This file contains hidden or 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
function isValid(s:string ):boolean{ | |
interface matchingBrac{ | |
[key:string]:string} | |
const open="{[("; | |
const close="}])" | |
const matchingBrackets:matchingBrac = { | |
')': '(', |
This file contains hidden or 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
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++ | |
} |
This file contains hidden or 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
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> = []; | |
This file contains hidden or 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
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]]); |
This file contains hidden or 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
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] |
This file contains hidden or 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
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; |
This file contains hidden or 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 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) | |
} | |
} |
This file contains hidden or 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
// 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] |
NewerOlder