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
| var numTeams = function(arr) { | |
| let result = 0 | |
| for (let i=0; i < arr.length - 2; i++) { | |
| for (let j=i+1; j < arr.length - 1; j++) { | |
| for (let k=j+1; k < arr.length; k++) { | |
| if (arr[i] < arr[j] && arr[j] < arr[k]) result++ | |
| if (arr[i] > arr[j] && arr[j] > arr[k]) result++ | |
| } | |
| } |
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
| var isValid = function(str) { | |
| const parenthesis = new Map([['{', '}'], ['(', ')'], ['[', ']']]) | |
| const stack = [] | |
| for (let i=0; i < str.length; i++) { | |
| const char = str[i] | |
| if (parenthesis.has(char)) { | |
| stack.push(char) | |
| } else { | |
| const e = stack.pop() |
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
| var findPairs = function(nums, k) { | |
| if (k < 0) return 0 | |
| if (k === 0) { | |
| return handleZeroCase(nums) | |
| } else { | |
| return handleGeneralCase(nums, k) | |
| } | |
| }; | |
| function handleZeroCase(nums) { |
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 allPathsSourceTarget(graph: number[][]): number[][] { | |
| return allPathsRecursive(graph, 0) | |
| } | |
| function allPathsRecursive(graph: number[][], v: number): number[][] { | |
| if (v == graph.length-1) return [[v]] | |
| let result: number[][] = [] | |
| const vertices = graph[v] | |
| for (let j=0; j < vertices.length; j++) { |
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
| var constructMaximumBinaryTree = function(nums) { | |
| return constructRec(nums, 0, nums.length - 1) | |
| }; | |
| // nums = [3,1] | |
| // leftIdx = 0 | |
| // rightIdx = 1 | |
| // maxIndex = 0 | |
| // root.left = constructRec(nums, 1,) | |
| var constructRec = function(nums, leftIdx, rightIdx){ | |
| console.log(leftIdx, rightIdx) |
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 checkStraightLine(coordinates: number[][]): boolean { | |
| let [c1, c2] = coordinates | |
| const targetSlope = calculateSlope(c1, c2) | |
| for (let i=2; i < coordinates.length; i++) { | |
| let c1 = coordinates[i-1], c2 = coordinates[i] | |
| const slope = calculateSlope(c1, c2) | |
| if (targetSlope != slope) return false | |
| } |
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
| /** | |
| * @param {character[]} tasks | |
| * @param {number} n | |
| * @return {number} | |
| */ | |
| var leastInterval = function(tasks, n) { | |
| if( n == 0 ) return tasks.length; | |
| let frecuencyArray = createFrecuencyArray(tasks); | |
| frecuencyArray = sortFrequencyArray(frecuencyArray); | |
| let remaining = n + 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 addBinary(a: string, b: string): string { | |
| let result: string = "" | |
| let carry: string = "0" | |
| const maxLength: number = Math.max(a.length, b.length) | |
| if (a.length < maxLength) a = addLeadingZeros(a, maxLength) | |
| if (b.length < maxLength) b = addLeadingZeros(b, maxLength) | |
| for (let i=maxLength-1; i >= 0; i--) { | |
| const b1: string = a[i] || "0" |
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
| var maxCoins = function(piles) { | |
| piles = piles.sort((a, b) => a-b) | |
| let count = 0 | |
| const length = piles.length | |
| for (let i=length/3; i < length; i += 2) { | |
| count += piles[i] | |
| } | |
| return count |
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
| var groupThePeople = function(groupSizes) { | |
| const map = new Map() | |
| for (let i=0; i < groupSizes.length; i++) { | |
| addPersonToGroup(map, groupSizes[i], i) | |
| } | |
| return concatGroups(map) | |
| } | |
| function addPersonToGroup(map, size, i) { |