forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
156 changed files
with
6,391 additions
and
7 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = function (nums, target) { | ||
let map = {}; | ||
for (let i = 0; i < nums.length; i++) { | ||
if (target - nums[i] in map) { | ||
return [map[target - nums[i]], i]; | ||
} else { | ||
map[nums[i]] = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string} p | ||
* @return {boolean} | ||
*/ | ||
var isMatch = function(s, p) { | ||
var lenS = s.length; | ||
var lenP = p.length; | ||
var map = {}; | ||
|
||
return check(0, 0); | ||
|
||
function check(idxS, idxP) { | ||
if (map[idxS + ':' + idxP] !== undefined) { | ||
return map[idxS + ':' + idxP]; | ||
} | ||
|
||
if (idxS > lenS) { | ||
return false; | ||
} | ||
|
||
if (idxS === lenS && idxP === lenP) { | ||
return true; | ||
} | ||
|
||
if (p[idxP] === '.' || p[idxP] === s[idxS]) { | ||
map[idxS + ':' + idxP] = p[idxP + 1] === '*' ? | ||
check(idxS + 1, idxP) || check(idxS, idxP + 2) : | ||
check(idxS + 1, idxP + 1); | ||
} else { | ||
map[idxS + ':' + idxP] = p[idxP + 1] === '*' ? | ||
check(idxS, idxP + 2) : false; | ||
} | ||
|
||
return map[idxS + ':' + idxP]; | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* 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} p | ||
* @param {TreeNode} q | ||
* @return {boolean} | ||
*/ | ||
var isSameTree = function (p, q) { | ||
if (!p && !q) return true; | ||
if (!p || !q || p.val !== q.val) return false; | ||
return isSameTree(p.right, q.right) && isSameTree(p.left, q.left); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* 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 | ||
* @return {number[][]} | ||
*/ | ||
var levelOrder = function (root) { | ||
if (!root) return []; | ||
|
||
const result = []; | ||
const queue = [root]; | ||
|
||
while (queue.length) { | ||
const numNodes = queue.length; | ||
const temp = []; | ||
for (let i = 0; i < numNodes; i++) { | ||
const subtree = queue.shift(); | ||
temp.push(subtree.val); | ||
if (subtree.left !== null) queue.push(subtree.left); | ||
if (subtree.right !== null) queue.push(subtree.right); | ||
} | ||
result.push(temp); | ||
} | ||
|
||
return result; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* 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 | ||
* @return {number} | ||
*/ | ||
var maxDepth = (root) => { | ||
let maxDepth = 0; | ||
let DFS = (node, depth) => { | ||
if (!node) return maxDepth; | ||
if (depth > maxDepth) maxDepth = depth; | ||
DFS(node.right, depth + 1); | ||
DFS(node.left, depth + 1); | ||
}; | ||
DFS(root, 1); | ||
return maxDepth; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
class Heap { | ||
constructor(stones) { | ||
this.heap = stones; | ||
this.size = stones.length; | ||
this.heapify(0); | ||
} | ||
right(pos) { | ||
return 2 * pos + 2; | ||
} | ||
left(pos) { | ||
return 2 * pos + 1; | ||
} | ||
isleaf(pos) { | ||
if (2 * pos + 1 >= this.size) return true; | ||
return false; | ||
} | ||
swap(a, b) { | ||
let temp = this.heap[a]; | ||
this.heap[a] = this.heap[b]; | ||
this.heap[b] = temp; | ||
} | ||
fix(pos) { | ||
if (this.isleaf(pos)) return; | ||
let left = this.left(pos); | ||
let right = this.right(pos); | ||
let bigger = left; | ||
if (right < this.size) | ||
bigger = this.heap[left] > this.heap[right] ? left : right; | ||
if (this.heap[pos] < this.heap[bigger]) { | ||
this.swap(pos, bigger); | ||
this.fix(bigger); | ||
} | ||
} | ||
heapify(pos) { | ||
if (this.isleaf(pos)) return; | ||
this.heapify(this.left(pos)); | ||
this.heapify(this.right(pos)); | ||
this.fix(pos); | ||
} | ||
delete() { | ||
this.swap(0, --this.size); | ||
this.fix(0); | ||
return this.heap[0]; | ||
} | ||
insert(val) { | ||
this.size++; | ||
this.heap[this.size - 1] = val; | ||
this.heapify(0); | ||
} | ||
peek() { | ||
return this.heap[0]; | ||
} | ||
} | ||
/** | ||
* @param {number[]} stones | ||
* @return {number} | ||
*/ | ||
var lastStoneWeight = function (stones) { | ||
//use a max heap to pop off the top 2 stones at each time | ||
//if result is 0 we can do nothing | ||
//if the result is a new weight we can push it back to the heap | ||
const heap = new Heap(stones); | ||
while (heap.size > 1) { | ||
let x = heap.peek(); | ||
heap.delete(); | ||
let y = heap.peek(); | ||
heap.delete(); | ||
const res = x - y; | ||
if (res > 0) heap.insert(res); | ||
} | ||
if (heap.size) return heap.peek(); | ||
return 0; | ||
}; |
10 changes: 10 additions & 0 deletions
10
javascript/105-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
function buildTree(preorder, inorder) { | ||
if (!preorder.length || !inorder.length) return null; | ||
|
||
let root = new TreeNode(preorder[0]); | ||
let mid = inorder.indexOf(preorder[0]); | ||
|
||
root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid)); | ||
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1)); | ||
return root; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* https://leetcode.com/problems/container-with-most-water/ | ||
* Time O(N) | Space(1) | ||
* @param {number[]} height | ||
* @return {number} | ||
*/ | ||
var maxArea = function(height) { | ||
let [ left, right, max ] = [ 0, (height.length - 1), 0 ]; | ||
|
||
while (left < right) { | ||
const [ leftHeight, rightHeight ] = getHeights(height, left, right); | ||
const area = getArea(height, left, right); | ||
|
||
max = Math.max(max, area); | ||
|
||
const isRightGreater = leftHeight <= rightHeight; | ||
if (isRightGreater) left++; | ||
|
||
const isRightLess = rightHeight < leftHeight; | ||
if (isRightLess) right--; | ||
} | ||
|
||
return max; | ||
}; | ||
|
||
const getHeights = (height, left, right) => [ height[left], height[right] ]; | ||
|
||
const getArea = (height, left, right) => { | ||
const [ leftHeight, rightHeight ] = getHeights(height, left, right); | ||
const _height = Math.min(leftHeight, rightHeight); | ||
const width = right - left; | ||
|
||
return _height * width; | ||
}; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* 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 | ||
* @return {boolean} | ||
*/ | ||
var isBalanced = function (root) { | ||
const getHeight = (root) => { | ||
if (!root) return [-1, true]; | ||
|
||
const [leftHeight, leftBalanced] = getHeight(root.left); | ||
const [rightHeight, rightBalanced] = getHeight(root.right); | ||
|
||
const balanced = | ||
leftBalanced && | ||
rightBalanced && | ||
Math.abs(leftHeight - rightHeight) < 2; | ||
|
||
return [1 + Math.max(leftHeight, rightHeight), balanced]; | ||
}; | ||
|
||
const balanced = getHeight(root)[1]; | ||
|
||
return balanced; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var longestCommonSubsequence = function (text1, text2) { | ||
let m = text1.length, | ||
n = text2.length, | ||
DP = new Array(m + 1).fill(0).map((_) => new Array(n + 1).fill(0)); | ||
|
||
for (let x = m - 1; x >= 0; x--) | ||
for (let y = n - 1; y >= 0; y--) { | ||
if (text1[x] === text2[y]) { | ||
DP[x][y] = 1 + DP[x + 1][y + 1]; | ||
} else { | ||
DP[x][y] = Math.max(DP[x + 1][y], DP[x][y + 1]); | ||
} | ||
} | ||
|
||
return DP[0][0]; | ||
}; |
Oops, something went wrong.