Skip to content

Instantly share code, notes, and snippets.

@lienista
lienista / leetcode-15-three-sum-to-zero-1.js
Last active October 11, 2018 22:54
(Algorithms in Javascript) Leetcode 15. 3Sum - Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets.
const threeSum = (nums) => {
let len = nums.length;
if(len < 3) return [];
nums.sort(function(a,b){
return a-b;
})
if(nums[0] > 0 || nums[0] + nums[1] + nums[2] > 0) return [];
if(len === 3) {
if(nums[0] + nums[1] + nums[2] === 0) return [nums];
else return [];
@lienista
lienista / leetcode-15-three-sum-to-zero.js
Last active October 11, 2018 22:54
(Algorithms in Javascript) Leetcode 15. 3Sum - Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets.
const threeSum = (nums) => {
let len = nums.length;
if(len < 3) return [];
nums.sort(function(a,b){
return a-b;
})
if(nums[0] > 0 || nums[0] + nums[1] + nums[2] > 0) return [];
if(len === 3) {
if(nums[0] + nums[1] + nums[2] === 0) return [nums];
else return [];
@lienista
lienista / leetcode-64-minimum-path-sum.js
Last active October 11, 2018 22:55
(Algorithms in Javascript) Leetcode 64. Minimum Path Sum - Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time.
const minPathSum = (grid) => {
let [m, n] = [grid.length, grid[0].length];
if(m+n === 0) return 0;
let result = [grid[0][0]];
for (let j = 1; j < n; j++) { // initialize
result[j] = result[j-1] + grid[0][j];
}
for(let i=1; i<m; i++){
for(let j=0; j<n; j++){
@lienista
lienista / leetcode-11-container-with-most-water.js
Last active October 11, 2018 22:55
(Algorithms in Javascript) Leetcode 11. Container With Most Water - Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container c…
const maxArea = function(height) {
let max = 0,
left = 0,
right = height.length - 1;
let term, thisArea, width;
while (left < right) {
width = right - left;
if (height[left] < height[right]) {
thisArea = height[left] * width;
left++;
@lienista
lienista / leetcode-42-trapping-rain-water-1.js
Last active April 3, 2022 17:57
(Algorithms in Javascript) Leetcode 42. Trapping Rain Water - Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
const trap = (number) => {
let len = number.length;
if(!len) return 0;
let result = 0;
let left = 0, right = len - 1;
let maxLeft = 0, maxRight = 0;
while(left<=right){
if(number[left] <= number[right]){
if(number[left] >=maxLeft) maxLeft = number[left];
@lienista
lienista / leetcode-42-trapping-rain-water.js
Last active October 11, 2018 22:56
(Algorithms in Javascript) Leetcode 42. Trapping Rain Water - Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
const trap = (number) => {
let len = number.length;
if(!len) return 0;
let result = 0;
let maxLeft = [];
maxLeft[0] = number[0];
let maxRight = [];
maxRight[len-1] = number[len-1];
for(let i=1; i<len; i++) {
@lienista
lienista / leetcode-628-highest-product-of-3-integers-1.js
Last active October 11, 2018 22:56
(Algorithms in Javascript) Leetcode 628. Maximum Product of Three Numbers - Given an integer array, find three numbers whose product is maximum and output the maximum product.
const maximumProduct = function(nums) {
let len = nums.length;
let maxProduct = 1;
if(len===0) return 0;
if(len<=3) {
for(var i=0; i<len; i++){
maxProduct *= nums[i];
}
return maxProduct;
}
@lienista
lienista / leetcode-628-highest-product-of-3-integers.js
Last active October 11, 2018 22:56
(Algorithms in Javascript) Leetcode 628. Maximum Product of Three Numbers - Given an integer array, find three numbers whose product is maximum and output the maximum product.
const maximumProduct = (nums) => {
let len = nums.length;
let maxProduct = 1;
if(len===0) return 0;
if(len<=3) {
for(let i=0; i<len; i++){
maxProduct *= nums[i];
}
return maxProduct;
@lienista
lienista / leetcode-343-integer-break.js
Last active October 11, 2018 22:57
(Algorithms in Javascript) Leetcode 343. Integer Break - Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.
const integerBreak = (n) => {
if(n<=3) return n-1;
if(n%3===0) return Math.pow(3, n/3);
if(n%3===1) return 4*Math.pow(3, (n-4)/3);
return 2*Math.pow(3,parseInt(n/3));
}
@lienista
lienista / leetcode-268-missing-number.js
Last active August 3, 2020 11:45
(Algorithms in Javascript) Leetcode 268. Missing Number - Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
const missingNumber = (nums) => {
let len = nums.length;
if(len===0) return 0;
result = 0;
for (let i=0; i<len; i++) {
result += nums[i] - i;
}
return len - result;
};