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 islandPerimeter = function(grid) { | |
const countWateryNeighbors = (row, col) => { | |
return [[row+1, col],[row-1, col],[row, col+1],[row, col-1]] | |
.map(([r, c]) => { | |
if(r < 0 || r >= grid.length || c < 0 || c >= grid[0].length) return 1; | |
if(grid[r][c] === 0) return 1; | |
return 0; | |
}) | |
.reduce((a, b) => a + b); | |
}; |
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 plusOne = function(digits) { | |
let [carry, i] = [1, result.length-1]; | |
while(carry && i >=0){ | |
const n = result[i] + carry; | |
carry = (n === 10) ? 1 : 0; | |
digits[i--] = n % 10; | |
} | |
if(carry) digits.unshift(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
var nthUglyNumber = function(n) { | |
const uglies = [1]; | |
const heap = new Heap((a, b) => a - b); | |
heap.push(1); | |
while(uglies.length < n){ | |
const lastUgly = uglies[uglies.length-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
/** | |
* @param {number} x | |
* @param {number} y | |
* @return {number} | |
*/ | |
var hammingDistance = function(x, y) { | |
let [diffs, result] = [x ^ y, 0]; | |
while(diffs){ | |
result += diffs & 1; | |
diffs = diffs >> 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
/** | |
* Definition for a binary tree node. | |
* function TreeNode(val) { | |
* this.val = val; | |
* this.left = this.right = null; | |
* } | |
*/ | |
/** | |
* @param {TreeNode} root | |
*/ |
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 myAtoi = function(str) { | |
str = str.trim(); | |
if(!str.length) return 0; | |
let negative = false; | |
const chars = []; | |
if(!str[0].match(/[0-9\+\-]/)) return 0; | |
let 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
/** | |
* Definition for a binary tree node. | |
* function TreeNode(val, left, right) { | |
* this.val = (val===undefined ? 0 : val) | |
* this.left = (left===undefined ? null : left) | |
* this.right = (right===undefined ? null : right) | |
* } | |
*/ | |
/** | |
* @param {TreeNode} root |
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
/* | |
Idea: Count up the steps we can make one by one. | |
- O(n) I think... | |
*/ | |
var arrangeCoins = function(n) { | |
let rows = 0; | |
let prevRowSize = 0; | |
while(n > 0){ | |
if(n >= ++prevRowSize) rows++; |
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[][]} board | |
* @param {string[]} words | |
* @return {string[]} | |
*/ | |
var findWords = function(board, words) { | |
const result = new Set(); | |
const key = (r, c) => `${r}:${c}`; |
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 {number} n | |
* @return {number} | |
*/ | |
var numTrees = function(n) { | |
const memo = new Array(n+1).fill(0).map(() => new Array(n+1).fill(-1)); | |
const count = (l, r) => { | |
if(r <= l) return 1; | |
if(memo[l][r] !== -1) return memo[l][r]; |