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
/** | |
* @param {number[][]} intervals | |
* @return {number[][]} | |
*/ | |
var merge = function(intervals) { | |
let temp = []; | |
intervals.sort((a, b) => a[0] - b[0]) | |
for(let i=0; i<intervals.length - 1; i++) { | |
if(intervals[i][1] >= intervals[i+1][0]) { | |
temp = intervals.splice(i+1, 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
/** | |
* @param {number[]} nums | |
* @return {number[]} | |
*/ | |
var productExceptSelf = function(nums) { | |
let prefix = 1; | |
let postfix = 1; | |
let res = []; | |
for(let i=0; i<nums.length; i++) { | |
res[i] = prefix; |
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
/** | |
* @param {number[]} prices | |
* @return {number} | |
*/ | |
var maxProfit = function(prices) { | |
let max = 0; | |
let buy = prices[0]; | |
for(let i=0; i<prices.length; i++) { | |
if(prices[i] > buy) max = Math.max(max, prices[i] - buy); |
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
/** | |
* @param {number[]} height | |
* @return {number} | |
*/ | |
var maxArea = function(height) { | |
let l = 0; | |
let r = height.length - 1; | |
let maxArea = 0; | |
let area = 0; |
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
/** | |
* @param {number[]} nums | |
* @param {number} target | |
* @return {number[]} | |
*/ | |
var twoSum = function(nums, target) { | |
let map = new Map(); | |
for(let i=0; i<nums.length; i++) { | |
let numToFind = target - nums[i]; | |
if(map.has(numToFind)) return [map.get(numToFind), 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
/** | |
* @param {number[]} nums | |
* @return {number} | |
*/ | |
var maxSubArray = function(nums) { | |
let sum = 0; | |
let maxSum = nums[0]; | |
for(let i = 0; i < nums.length; i++) { | |
sum += nums[i]; | |
if(sum > maxSum) maxSum = sum; |
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
/** | |
* @param {number[]} nums | |
* @return {number} | |
*/ | |
var removeDuplicates = function(nums) { | |
for(let i=0; i<nums.length - 1; i++) { | |
if(nums[i] === nums[i+1]) { | |
nums.splice(i+1, 1); | |
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
function secondLargest(arr) { | |
let largest = Number.MIN_VALUE; | |
let secondLargest = Number.MIN_VALUE; | |
for (let i = 0; i < arr.length; i++) { | |
if (arr[i] > largest) { | |
secondLargest = largest; | |
largest = arr[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
/** | |
* @param {number[]} nums | |
* @param {number} k | |
* @return {void} Do not return anything, modify nums in-place instead. | |
*/ | |
var rotate = function(nums, k) { | |
if(k > nums.length) k = k % nums.length; | |
reverse(nums, 0, nums.length - 1); | |
reverse(nums, 0, k - 1); |
NewerOlder