From 7869aef7b5ae377e99a2edeca194f6c2ba9d7543 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Wed, 12 Jun 2019 22:50:39 -0700 Subject: [PATCH 01/91] 701. Insert into a Binary Search Tree --- README.md | 3 +- bst.js | 47 +++++++++++++++++++ insert_into_a_binary_search_tree.js | 70 +++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 bst.js create mode 100644 insert_into_a_binary_search_tree.js diff --git a/README.md b/README.md index c074574..7bb3bd8 100644 --- a/README.md +++ b/README.md @@ -41,5 +41,6 @@ | 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | +| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | + -test1 diff --git a/bst.js b/bst.js new file mode 100644 index 0000000..287f37c --- /dev/null +++ b/bst.js @@ -0,0 +1,47 @@ +class Node { + constructor(val) { + this.val = val; + this.left = null; + this.right = null; + } + insert(val) { + if (val < this.val && this.left) { + this.left.insert(val); + } else if (val < this.val) { + this.left = new Node(val); + } else if (val > this.val && this.right) { + this.right.insert(val); + } else if (val > this.val) { + this.right = new Node(val); + } + } +} + +let search = function(node, val) { + if (!node) { + return null; + } + + if (node.val === val) { + return node; + } + + if (val < node.val) { + return search(node.left, val); + } else if (val > node.val) { + return search(node.right, val); + } + //return out; +}; + +let node = new Node(10); +node.insert(0); +node.insert(12); +node.insert(-1); +node.insert(4); +node.insert(12); +node.insert(11); +node.insert(20); +node.insert(3); + +search(node, 7); //? diff --git a/insert_into_a_binary_search_tree.js b/insert_into_a_binary_search_tree.js new file mode 100644 index 0000000..85f5b25 --- /dev/null +++ b/insert_into_a_binary_search_tree.js @@ -0,0 +1,70 @@ +// https://leetcode.com/problems/insert-into-a-binary-search-tree/ + +// 701. Insert into a Binary Search Tree + +// Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST. +// +// Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them. +// +// For example, +// +// Given the tree: +// 4 +// / \ +// 2 7 +// / \ +// 1 3 +// And the value to insert: 5 +// You can return this binary search tree: +// +// 4 +// / \ +// 2 7 +// / \ / +// 1 3 5 +// This tree is also valid: +// +// 5 +// / \ +// 2 7 +// / \ +// 1 3 +// \ +// 4 +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {number} val + * @return {TreeNode} + */ +var insertIntoBST = function(root, val) { + if (val < root.val) { + if (root.left) { + insertIntoBST(root.left, val); + } else { + root.left = new TreeNode(val); + } + } + + if (val > root.val) { + if (root.right) { + insertIntoBST(root.right, val); + } else { + root.right = new TreeNode(val); + } + } + + return root; +}; + +let node = new TreeNode(10); +node.left = new TreeNode(5); +node.right = new TreeNode(11); + +insertIntoBST(node, 12); //? From f681df52c139120d35d27132cfdd1586a63dede8 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sat, 15 Jun 2019 12:54:23 -0700 Subject: [PATCH 02/91] 563. Binary Tree Tilt --- README.md | 1 + binary_tree_tilt.js | 60 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 binary_tree_tilt.js diff --git a/README.md b/README.md index 7bb3bd8..ae085bf 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ | 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | | 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | +| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | diff --git a/binary_tree_tilt.js b/binary_tree_tilt.js new file mode 100644 index 0000000..0c6d862 --- /dev/null +++ b/binary_tree_tilt.js @@ -0,0 +1,60 @@ +function TreeNode(val) { + this.val = val; + this.left = this.right = null; +} + +// Given a binary tree, return the tilt of the whole tree. +// +// The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree node values. Null node has tilt 0. +// +// The tilt of the whole tree is defined as the sum of all nodes' tilt. +// +// Example: +// Input: +// 1 +// / \ +// 2 3 +// Output: 1 +// Explanation: +// Tilt of node 2 : 0 +// Tilt of node 3 : 0 +// Tilt of node 1 : |2-3| = 1 +// Tilt of binary tree : 0 + 0 + 1 = 1 +// Note: +// +// The sum of node values in any subtree won't exceed the range of 32-bit integer. +// All the tilt values won't exceed the range of 32-bit integer. + +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var findTilt = function(root) { + let tilt = 0; + function postOrder(root) { + if (root === null) { + return 0; + } + let left = postOrder(root.left); + let right = postOrder(root.right); + tilt += Math.abs(left - right); + return root.val + left + right; + } + postOrder(root); + return tilt; +}; + +let node = new TreeNode(1); +node.left = new TreeNode(2); +node.right = new TreeNode(3); +node.left.left = new TreeNode(4); +node.right.left = new TreeNode(5); + +findTilt(node); //? From 18a1d79a6a80bc98fe7398f4022704d21ea990d1 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sat, 15 Jun 2019 20:55:04 -0700 Subject: [PATCH 03/91] Tree taverse added --- .idea/workspace.xml | 263 +++++++++++++++++++++----------------------- traverse.js | 47 ++++++++ 2 files changed, 170 insertions(+), 140 deletions(-) create mode 100644 traverse.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 048412a..cbe5bac 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,14 @@ + + + - + + + + @@ -17,27 +23,19 @@ - + - - - - - + + - - - - - - - - - + + + + @@ -54,7 +52,11 @@ toLo + depth = counter + + $PROJECT_DIR$ + - + @@ -254,21 +233,8 @@ - - - - 1556684407626 - - - 1556684491968 - 1556768240387 @@ -599,11 +565,25 @@ - - @@ -612,7 +592,6 @@ - @@ -621,13 +600,14 @@ - + + @@ -643,8 +623,6 @@ @@ -702,24 +682,6 @@ - - - - - - - - - - - - - - - - - - @@ -974,28 +936,52 @@ - + - - + + + + + - + - + + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/README.md b/README.md index afd6ecb..2b401fe 100644 --- a/README.md +++ b/README.md @@ -48,3 +48,8 @@ | 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | +## Others + +| | Title | Solution | +|:--|:------|:---------| +| 1 | Tree | df | diff --git a/Tree.js b/Tree.js new file mode 100644 index 0000000..19e6982 --- /dev/null +++ b/Tree.js @@ -0,0 +1,13 @@ +class Node { + constructor(val) { + this.data = val; + this.children = []; + } + + add(data) { + const newNode = new Node(data); + this.children.push(newNode); + } +} + +class Tree {} diff --git a/two_sum_IV.js b/two_sum_IV.js new file mode 100644 index 0000000..fcdc511 --- /dev/null +++ b/two_sum_IV.js @@ -0,0 +1,70 @@ +// Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. +// +// Example 1: +// +// Input: +// 5 +// / \ +// 3 6 +// / \ \ +// 2 4 7 +// +// Target = 9 +// +// Output: True +// +// +// Example 2: +// +// Input: +// 5 +// / \ +// 3 6 +// / \ \ +// 2 4 7 +// +// Target = 28 +// +// Output: False + +// function TreeNode(val) { +// // this.val = val; +// // this.left = this.right = null; +// // } + +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {number} k + * @return {boolean} + */ +var findTarget = function(root, k) { + const values = new Set(); + let found = false; + function inorder(node) { + if (!node) { + return; + } + inorder(node.left); + if (values.has(k - node.val)) { + found = true; + return; + } + values.add(node.val); + inorder(node.right); + } + inorder(root); + return found; +}; + +let root = new TreeNode(1); +// root.left = new TreeNode(1); +// root.right = new TreeNode(3); + +findTarget(root, 2); //? From f13b19f93c777374a252f47be41c8c6948922039 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Tue, 18 Jun 2019 20:31:44 -0700 Subject: [PATCH 08/91] 589. N-ary Tree Preorder Traversal --- .idea/workspace.xml | 69 +++++++++++++++++++++++++------------------ README.md | 7 +++-- Tree.js | 13 -------- preorder_traversal.js | 29 ++++++++++++++++++ tree.js | 55 ++++++++++++++++++++++++++++++++++ 5 files changed, 128 insertions(+), 45 deletions(-) delete mode 100644 Tree.js create mode 100644 preorder_traversal.js create mode 100644 tree.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d128d1e..5668fd4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,10 +1,11 @@ - - + + + @@ -22,8 +23,8 @@ - - + + @@ -33,10 +34,10 @@ - + - - + + @@ -113,8 +114,8 @@ @@ -234,14 +235,7 @@ - - - - 1556768240387 - 1556770244995 @@ -579,11 +573,18 @@ - - @@ -604,7 +605,7 @@ - + @@ -621,9 +622,18 @@ + + + @@ -963,25 +974,25 @@ - + - - + + - + - - + + - - + + diff --git a/README.md b/README.md index 2b401fe..a9ac1af 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ | 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | +| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | @@ -50,6 +51,6 @@ ## Others -| | Title | Solution | -|:--|:------|:---------| -| 1 | Tree | df | +| | Title | Solution | +|:--|:------|:-------------------| +| 1 | Tree | [tree.js](tree.js) | diff --git a/Tree.js b/Tree.js deleted file mode 100644 index 19e6982..0000000 --- a/Tree.js +++ /dev/null @@ -1,13 +0,0 @@ -class Node { - constructor(val) { - this.data = val; - this.children = []; - } - - add(data) { - const newNode = new Node(data); - this.children.push(newNode); - } -} - -class Tree {} diff --git a/preorder_traversal.js b/preorder_traversal.js new file mode 100644 index 0000000..56aa9ca --- /dev/null +++ b/preorder_traversal.js @@ -0,0 +1,29 @@ +/** + * // Definition for a Node. + * function Node(val,children) { + * this.val = val; + * this.children = children; + * }; + */ +/** + * @param {Node} root + * @return {number[]} + */ + +var preorder = function(root) { + let out = []; + let stack = [root]; + while (stack.length > 0) { + let current = stack.shift(); + if (current) { + out.push(current.val); + if (current.children.length > 0) { + current.children.reverse().forEach(function(value, index, array) { + stack.unshift(value); + }); + } + } + } + + return out; +}; diff --git a/tree.js b/tree.js new file mode 100644 index 0000000..7fe252c --- /dev/null +++ b/tree.js @@ -0,0 +1,55 @@ +class Node { + constructor(val) { + this.data = val; + this.children = []; + } + + add(data) { + const newNode = new Node(data); + this.children.push(newNode); + } + + remove(data) { + this.children = this.children.filter(node => node.data !== data); + } +} + +class Tree { + constructor(node) { + this.root = node; + } + + traverseBFS() { + let queue = [this.root]; + + while (queue.length > 0) { + let current = queue.shift(); + console.log(current.data); + if (current.children.length > 0) { + current.children.forEach(function(value, index, array) { + queue.push(value); + }); + } + } + } + + traverseDFS() { + let stack = [this.root]; + while (stack.length > 0) { + let current = stack.shift(); + if (current.children.length > 0) { + current.children.reverse().forEach(function(value, index, array) { + stack.unshift(value); + }); + } + } + } +} + +let root = new Node(1); +let firstNode = new Node(2); +firstNode.children = [new Node(90)]; +root.children = [firstNode, new Node(45), new Node(47)]; + +const tree = new Tree(root); +tree.traverseDFS(); From e78e2e6850076bab1936e64bc735ec6bd751b580 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Tue, 18 Jun 2019 23:17:37 -0700 Subject: [PATCH 09/91] 215. Kth Largest Element in an Array --- README.md | 1 + largest_element _in_an_array.js | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 largest_element _in_an_array.js diff --git a/README.md b/README.md index a9ac1af..2568334 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ | 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | +| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | ## Others diff --git a/largest_element _in_an_array.js b/largest_element _in_an_array.js new file mode 100644 index 0000000..8c269c9 --- /dev/null +++ b/largest_element _in_an_array.js @@ -0,0 +1,11 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var findKthLargest = function(nums, k) { + let sortedArray = nums.sort((a, b) => b - a); + return sortedArray[k - 1]; +}; + +findKthLargest([3, 2, 1, 5, 6, 4], 2); //? From c7a74b2d022510cab0c31b40622f9938217952c3 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Tue, 18 Jun 2019 23:29:15 -0700 Subject: [PATCH 10/91] 74. Search a 2D Matrix --- .idea/workspace.xml | 97 ++++++++++++++++++++----------------------- README.md | 1 + search_a_2d_matrix.js | 44 ++++++++++++++++++++ 3 files changed, 90 insertions(+), 52 deletions(-) create mode 100644 search_a_2d_matrix.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 5668fd4..a6696af 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,11 +1,10 @@ - - + + - @@ -23,8 +22,8 @@ - - + + @@ -34,10 +33,10 @@ - + - - + + @@ -66,8 +65,6 @@ @@ -235,21 +234,7 @@ - - - - 1556770244995 - - - 1556776889897 - 1556859243955 @@ -580,11 +565,25 @@ - - @@ -634,8 +633,6 @@ @@ -693,26 +692,6 @@ - - - - - - - - - - - - - - - - - - - - @@ -988,11 +967,25 @@ + + + + + + + + + + + + + + - - + + diff --git a/README.md b/README.md index 2568334..6282a26 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ | 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | | 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | +| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | ## Others diff --git a/search_a_2d_matrix.js b/search_a_2d_matrix.js new file mode 100644 index 0000000..48ad3a9 --- /dev/null +++ b/search_a_2d_matrix.js @@ -0,0 +1,44 @@ +// https://leetcode.com/problems/search-a-2d-matrix/ + +// +// Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: +// +// Integers in each row are sorted from left to right. +// The first integer of each row is greater than the last integer of the previous row. +// Example 1: +// +// Input: +// matrix = [ +// [1, 3, 5, 7], +// [10, 11, 16, 20], +// [23, 30, 34, 50] +// ] +// target = 3 +// Output: true +// Example 2: +// +// Input: +// matrix = [ +// [1, 3, 5, 7], +// [10, 11, 16, 20], +// [23, 30, 34, 50] +// ] +// target = 13 +// Output: false + +/** + * @param {number[][]} matrix + * @param {number} target + * @return {boolean} + */ +var searchMatrix = function(matrix, target) { + for (let val of matrix) { + const includes = val.includes(target); + if (includes) { + return true; + } + } + return false; +}; + +searchMatrix([[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]], 3); //? From 2b82209fc9474cee0e549fe3278c40317157e799 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Wed, 19 Jun 2019 20:07:01 -0700 Subject: [PATCH 11/91] 590. N-ary Tree Postorder Traversal --- .idea/workspace.xml | 153 +++++++++++++++++++---------------------- README.md | 1 + postorder_traversal.js | 55 +++++++++++++++ 3 files changed, 125 insertions(+), 84 deletions(-) create mode 100644 postorder_traversal.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a6696af..0ff2af9 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + @@ -22,8 +22,8 @@ - - + + @@ -33,10 +33,28 @@ - + - - + + + + + + + + + + + + + + + + + + + + @@ -54,6 +72,7 @@ toLo depth = counter + .children $PROJECT_DIR$ @@ -65,7 +84,6 @@ @@ -234,14 +253,8 @@ - - - - 1556859243955 - 1557080701986 @@ -579,17 +592,24 @@ - - - + - + @@ -600,7 +620,7 @@ - + @@ -633,7 +653,6 @@ @@ -692,16 +712,6 @@ - - - - - - - - - - @@ -867,20 +877,8 @@ - - - - - - - - - - - - - - + + @@ -895,20 +893,8 @@ - - - - - - - - - - - - - - + + @@ -916,16 +902,8 @@ - - - - - - - - - - + + @@ -953,39 +931,46 @@ - + + + + + + + + - - + + - + - - + + - + - - + + - + - - + + - - + + diff --git a/README.md b/README.md index 6282a26..316d26a 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ | 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | +| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | diff --git a/postorder_traversal.js b/postorder_traversal.js new file mode 100644 index 0000000..8cffa1b --- /dev/null +++ b/postorder_traversal.js @@ -0,0 +1,55 @@ +// https://leetcode.com/problems/n-ary-tree-postorder-traversal/submissions/ + +// function Node(val, children) { +// this.val = val; +// this.children = []; +// } + +// +// Given an n-ary tree, return the postorder traversal of its nodes' values. +// +// For example, given a 3-ary tree: +// +// +// Return its postorder traversal as: [5,6,3,2,4,1]. +// +// +// Note: +// +// Recursive solution is trivial, could you do it iteratively? + +/** + * // Definition for a Node. + * function Node(val,children) { + * this.val = val; + * this.children = children; + * }; + */ +/** + * @param {Node} root + * @return {number[]} + */ +var postorder = function(root) { + let out = []; + let stack = [root]; //? + while (stack.length > 0) { + let current = stack.shift(); + if (current) { + out.unshift(current.val); + if (current.children.length > 0) { + current.children.forEach(function(value, index, array) { + stack.unshift(value); + }); + } + } + } + + return out; +}; + +let root = new Node(1); +let firstNode = new Node(3); +firstNode.children = [new Node(5), new Node(6)]; +root.children = [firstNode, new Node(2), new Node(4)]; + +postorder(root); //? From 44566a6931b1c219e8172cefc72d6b82b62095f7 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Wed, 19 Jun 2019 20:24:22 -0700 Subject: [PATCH 12/91] 965. Univalued Binary Tree --- README.md | 1 + univalued_binary_tree.js | 44 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 univalued_binary_tree.js diff --git a/README.md b/README.md index 316d26a..dcdc167 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ | 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | +| 965 | [ Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | | 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | diff --git a/univalued_binary_tree.js b/univalued_binary_tree.js new file mode 100644 index 0000000..e637bc2 --- /dev/null +++ b/univalued_binary_tree.js @@ -0,0 +1,44 @@ +function TreeNode(val) { + this.val = val; + this.left = this.right = null; +} +// https://leetcode.com/problems/univalued-binary-tree/ +// +// binary tree is univalued if every node in the tree has the same value. +// +// Return true if and only if the given tree is univalued. + +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ +var isUnivalTree = function(root) { + let set = new Set(); + let innerFunction = function(root) { + if (root) { + set.add(root.val); + } + if (root.left) { + innerFunction(root.left); + } + if (root.right) { + innerFunction(root.right); + } + }; + innerFunction(root); + + return set.size === 1; +}; + +let root = new TreeNode(1); +root.left = new TreeNode(1); +root.right = new TreeNode(1); + +isUnivalTree(root); //? From 557d01e8ea7d87c9e2aef809dc9ce797dee5812b Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Wed, 19 Jun 2019 23:05:37 -0700 Subject: [PATCH 13/91] 922. Sort Array By Parity II --- .idea/workspace.xml | 116 +++++++++++++++++-------------------- README.md | 3 +- sort_array_by_parity_II.js | 48 +++++++++++++++ 3 files changed, 104 insertions(+), 63 deletions(-) create mode 100644 sort_array_by_parity_II.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0ff2af9..bff9bd3 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + @@ -22,8 +22,8 @@ - - + + @@ -33,28 +33,13 @@ - + - - - - - - - - - - - - - - - - - - - - + + + + + @@ -84,8 +69,6 @@ @@ -147,7 +132,7 @@ -1 - + @@ -240,14 +237,8 @@ - - - - 1557120511261 - 1557120652863 @@ -585,17 +576,24 @@ - - - + @@ -606,7 +604,7 @@ - + @@ -639,7 +637,6 @@ @@ -698,18 +696,6 @@ - - - - - - - - - - - - @@ -958,11 +944,32 @@ + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/README.md b/README.md index a06b670..392f26b 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ | 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | +| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | | 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | diff --git a/search_insert_position.js b/search_insert_position.js new file mode 100644 index 0000000..c200c31 --- /dev/null +++ b/search_insert_position.js @@ -0,0 +1,16 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +var searchInsert = function(nums, target) { + for (let i = 0; i < nums.length; i++) { + const value = nums[i]; + if (value >= target) { + return i; + } + } + return nums.length; +}; + +searchInsert([1, 3, 5, 7], 9); //? From 3fb5b825edf49831b64f17c558e538ac30c121f8 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sat, 22 Jun 2019 00:10:34 -0700 Subject: [PATCH 17/91] 35. Search Insert Position --- search_insert_position.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/search_insert_position.js b/search_insert_position.js index c200c31..5cb2036 100644 --- a/search_insert_position.js +++ b/search_insert_position.js @@ -1,3 +1,26 @@ +// https://leetcode.com/problems/search-insert-position/ + +// Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. +// +// You may assume no duplicates in the array. +// +// Example 1: +// +// Input: [1,3,5,6], 5 +// Output: 2 +// Example 2: +// +// Input: [1,3,5,6], 2 +// Output: 1 +// Example 3: +// +// Input: [1,3,5,6], 7 +// Output: 4 +// Example 4: +// +// Input: [1,3,5,6], 0 +// Output: 0 + /** * @param {number[]} nums * @param {number} target From 74329411ff0053f4694d56df4c3e57d93df5ace3 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sat, 22 Jun 2019 00:11:36 -0700 Subject: [PATCH 18/91] Set theme jekyll-theme-architect --- _config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_config.yml b/_config.yml index c419263..3397c9a 100644 --- a/_config.yml +++ b/_config.yml @@ -1 +1 @@ -theme: jekyll-theme-cayman \ No newline at end of file +theme: jekyll-theme-architect \ No newline at end of file From ba881a99f09e63b9491de1e22f843d8598088b0e Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sat, 22 Jun 2019 00:13:13 -0700 Subject: [PATCH 19/91] Set theme jekyll-theme-minimal --- _config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_config.yml b/_config.yml index 3397c9a..2f7efbe 100644 --- a/_config.yml +++ b/_config.yml @@ -1 +1 @@ -theme: jekyll-theme-architect \ No newline at end of file +theme: jekyll-theme-minimal \ No newline at end of file From 1aaba16518055d1b9ad8c2b57104e748498b41e8 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sat, 22 Jun 2019 13:31:30 -0700 Subject: [PATCH 20/91] 540. Single Element in a Sorted Array --- .idea/workspace.xml | 96 +++++++++++++++-------------- README.md | 1 + single_element_in_a_sorted_array.js | 31 ++++++++++ 3 files changed, 81 insertions(+), 47 deletions(-) create mode 100644 single_element_in_a_sorted_array.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index e0f8c36..ccd5611 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,8 @@ - - + + @@ -19,10 +19,10 @@ - + - - + + @@ -31,8 +31,8 @@ - - + + @@ -66,8 +66,6 @@ @@ -238,21 +238,7 @@ - - - - 1557120652863 - - - 1557121613747 - 1558241744067 @@ -583,11 +569,25 @@ - - @@ -637,7 +637,6 @@ @@ -696,21 +696,6 @@ - - - - - - - - - - - - - - - @@ -958,18 +943,35 @@ - + - + + + + + + + + + + + + + + + + + + - - + + diff --git a/README.md b/README.md index 392f26b..193ed31 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ | 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | | 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | +| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | ## Others diff --git a/single_element_in_a_sorted_array.js b/single_element_in_a_sorted_array.js new file mode 100644 index 0000000..6c99a42 --- /dev/null +++ b/single_element_in_a_sorted_array.js @@ -0,0 +1,31 @@ +// 540. Single Element in a Sorted Array + +// Given a sorted array consisting of only integers where every element appears exactly twice except for one element which appears exactly once. Find this single element that appears only once. +// +// +// +// Example 1: +// +// Input: [1,1,2,3,3,4,4,8,8] +// Output: 2 +// Example 2: +// +// Input: [3,3,7,7,10,11,11] +// Output: 10 +// +// +// Note: Your solution should run in O(log n) time and O(1) space. + +/** + * @param {number[]} nums + * @return {number} + */ +var singleNonDuplicate = function(nums) { + for (let i = 0; i < nums.length; i += 2) { + if (nums[i] !== nums[i + 1]) { + return nums[i]; + } + } +}; + +singleNonDuplicate([3, 3, 7, 7, 10, 11, 11]); //? From 67679932b7ae39c1adef7a49045c375e2ae01790 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sat, 22 Jun 2019 21:55:09 -0700 Subject: [PATCH 21/91] 686. Repeated String Match --- .idea/workspace.xml | 88 ++++++++++++++++++++++------------------ README.md | 1 + repeated_string_match.js | 19 +++++++++ 3 files changed, 68 insertions(+), 40 deletions(-) create mode 100644 repeated_string_match.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index ccd5611..6ea7b24 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,10 @@ - - + + + + @@ -19,10 +21,10 @@ - + - - + + @@ -31,8 +33,8 @@ - - + + @@ -66,9 +68,6 @@ @@ -238,14 +240,7 @@ - - - - 1558241744067 - 1558245619380 @@ -583,11 +578,18 @@ - - @@ -611,7 +613,7 @@ - + @@ -637,7 +639,6 @@ @@ -696,21 +698,6 @@ - - - - - - - - - - - - - - - @@ -967,11 +954,32 @@ + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/README.md b/README.md index 193ed31..aeab88e 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ | 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | | 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | +| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | | 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | diff --git a/repeated_string_match.js b/repeated_string_match.js new file mode 100644 index 0000000..8b0d22f --- /dev/null +++ b/repeated_string_match.js @@ -0,0 +1,19 @@ +/** + * @param {string} A + * @param {string} B + * @return {number} + */ +var repeatedStringMatch = function(A, B) { + let str = ""; + let count = 0; + + while (str.length <= A.length + B.length) { + str += A; + count++; + if (str.includes(B)) return count; + } + + return -1; +}; + +repeatedStringMatch("abcd", "cdabcdab"); //? From ae0a65d2b71181aa63dbb4db31ba2420dec13e33 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sat, 22 Jun 2019 22:38:51 -0700 Subject: [PATCH 22/91] 1013. Partition Array Into Three Parts With Equal Sum --- README.md | 101 ++++++++++++++-------------- partition_array_into_three_parts.js | 20 ++++++ 2 files changed, 71 insertions(+), 50 deletions(-) create mode 100644 partition_array_into_three_parts.js diff --git a/README.md b/README.md index aeab88e..afa059f 100644 --- a/README.md +++ b/README.md @@ -5,56 +5,57 @@ #### This contain solution of leetcode in javascript -| # | Title | Solution | Runtime | Memory | Difficulty | -|:-----|:---------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------| -| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | -| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | -| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | -| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | -| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | -| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | -| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | -| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](find_common_characters.js) | 108 ms | 38.1 MB | Easy | -| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution 1](middle_of_the_linked_list.js)
[Solution 2](middle_of_the_linked_list_2.js) | 48 ms
44ms | 33.9 MB
33.8 MB | Easy | -| 126 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](single_number.js) | 60 ms | 37.1 MB | Easy | -| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution 1](max_consecutive_ones.js)
[Solution 2](max_consecutive_ones_2.js) | 56 ms | 37.1 MB | Easy | -| 258 | [ Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](add_digits.js) | 76 ms | 36.3 MB | Easy | -| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](delete_node_linked_list.js) | 60 ms | 35.6 MB | Easy | -| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](valid_anagram.js) | 56 ms | 37.8 MB | Easy | -| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](find_all_numbers_disappeared_in_an_array.js) | 6576 ms | 45.7 MB | Easy | -| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](contains_duplicate.js) | 96 ms | 42.7 MB | Easy | -| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](contains_duplicate_II.js) | 72 ms | 42.4 MB | Easy | -| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution 1](intersection_of_two_arrays.js)
[Solution 2](intersection_of_two_arrays_solution_2.js) | 56 ms | 35.8 MB | Easy | -| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](intersection_of_two_arrays_II.js)
[Solution 2](intersection_of_two_arrays_II_solution2.js) | 84 ms | 38.3 MB | Easy | -| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](squares_of_a_sorted_array.js) | 152 ms | 43.5 MB | Easy | -| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](minimum_index_sum_of_two_lists.js) | 108 ms | 43 MB | Easy | -| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](remove_duplicates_from_sorted_array.js) | 76 ms | 37.2 MB | Easy | -| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](same_tree.js) | 52 ms | 33.9 MB | Easy | -| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Iterative](binary_search.js)
[Recursion](binary_search_recursion.js) | | | Easy | -| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](search_in_a_binary_search_tree.js) | 100ms | 41.7 | Easy | -| 821 | [ Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](shortest_distance_to_a_character.js) | 192 ms | 45.5 MB | Easy | -| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](array_partition_I.js) | 116 ms | 39.1MB | Easy | -| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](number_complement.js) | 52ms | 33.9 MB | Easy | -| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](merge_two_binary_trees.js) | 92 ms | 40.4 MB | Easy | -| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](missing_number.js) | 388 ms | 37.1 MB | Easy | -| 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | -| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | -| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | -| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | -| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | -| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | -| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | -| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | -| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | -| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | -| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | -| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | -| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | -| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | -| 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | -| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | -| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | -| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | +| # | Title | Solution | Runtime | Memory | Difficulty | +|:-----|:----------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------| +| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | +| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | +| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | +| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | +| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | +| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | +| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | +| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](find_common_characters.js) | 108 ms | 38.1 MB | Easy | +| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution 1](middle_of_the_linked_list.js)
[Solution 2](middle_of_the_linked_list_2.js) | 48 ms
44ms | 33.9 MB
33.8 MB | Easy | +| 126 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](single_number.js) | 60 ms | 37.1 MB | Easy | +| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution 1](max_consecutive_ones.js)
[Solution 2](max_consecutive_ones_2.js) | 56 ms | 37.1 MB | Easy | +| 258 | [ Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](add_digits.js) | 76 ms | 36.3 MB | Easy | +| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](delete_node_linked_list.js) | 60 ms | 35.6 MB | Easy | +| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](valid_anagram.js) | 56 ms | 37.8 MB | Easy | +| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](find_all_numbers_disappeared_in_an_array.js) | 6576 ms | 45.7 MB | Easy | +| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](contains_duplicate.js) | 96 ms | 42.7 MB | Easy | +| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](contains_duplicate_II.js) | 72 ms | 42.4 MB | Easy | +| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution 1](intersection_of_two_arrays.js)
[Solution 2](intersection_of_two_arrays_solution_2.js) | 56 ms | 35.8 MB | Easy | +| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](intersection_of_two_arrays_II.js)
[Solution 2](intersection_of_two_arrays_II_solution2.js) | 84 ms | 38.3 MB | Easy | +| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](squares_of_a_sorted_array.js) | 152 ms | 43.5 MB | Easy | +| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](minimum_index_sum_of_two_lists.js) | 108 ms | 43 MB | Easy | +| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](remove_duplicates_from_sorted_array.js) | 76 ms | 37.2 MB | Easy | +| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](same_tree.js) | 52 ms | 33.9 MB | Easy | +| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Iterative](binary_search.js)
[Recursion](binary_search_recursion.js) | | | Easy | +| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](search_in_a_binary_search_tree.js) | 100ms | 41.7 | Easy | +| 821 | [ Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](shortest_distance_to_a_character.js) | 192 ms | 45.5 MB | Easy | +| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](array_partition_I.js) | 116 ms | 39.1MB | Easy | +| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](number_complement.js) | 52ms | 33.9 MB | Easy | +| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](merge_two_binary_trees.js) | 92 ms | 40.4 MB | Easy | +| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](missing_number.js) | 388 ms | 37.1 MB | Easy | +| 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | +| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | +| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | +| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | +| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | +| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | +| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | +| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | +| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | +| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | +| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | +| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | +| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | +| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | +| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | +| 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | +| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | +| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | +| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | ## Others diff --git a/partition_array_into_three_parts.js b/partition_array_into_three_parts.js new file mode 100644 index 0000000..7957914 --- /dev/null +++ b/partition_array_into_three_parts.js @@ -0,0 +1,20 @@ +/** + * @param {number[]} A + * @return {boolean} + */ +var canThreePartsEqualSum = function(A) { + let sum = A.reduce((prev, curr) => prev + curr); + + let counter = 0; + let temp = 0; + + A.forEach(function(value, index, array) { + temp += value; + if (temp === sum / 3) { + temp = 0; + counter++; + } + }); + + return counter === 3; +}; From 4b3b1dc1a494255cb65f0d84f98f5197b99c99b8 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sat, 22 Jun 2019 22:44:12 -0700 Subject: [PATCH 23/91] Set theme jekyll-theme-slate --- _config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_config.yml b/_config.yml index 2f7efbe..c741881 100644 --- a/_config.yml +++ b/_config.yml @@ -1 +1 @@ -theme: jekyll-theme-minimal \ No newline at end of file +theme: jekyll-theme-slate \ No newline at end of file From d58c00b0f3c194aea60d61ec33522bc731eafe0c Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sun, 23 Jun 2019 02:01:12 -0700 Subject: [PATCH 24/91] 429. N-ary Tree Level Order Traversal --- .idea/workspace.xml | 90 ++++++++++++++++++----------- README.md | 1 + n-ary_tree_level_order_traversal.js | 50 ++++++++++++++++ 3 files changed, 106 insertions(+), 35 deletions(-) create mode 100644 n-ary_tree_level_order_traversal.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6ea7b24..d1da1d1 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,7 +3,8 @@ - + + @@ -21,10 +22,10 @@ - + - - + + @@ -33,8 +34,8 @@ - - + + @@ -57,6 +58,7 @@ toLo depth = counter .children + .sort( $PROJECT_DIR$ @@ -68,9 +70,6 @@ @@ -240,21 +242,7 @@ - - - - 1558245619380 - - - 1558245972740 - 1558321720972 @@ -585,11 +573,25 @@ - - @@ -606,7 +608,7 @@ - + @@ -639,8 +641,6 @@ @@ -698,9 +700,6 @@ - - - @@ -968,6 +967,13 @@ + + + + + + + @@ -975,11 +981,25 @@ + + + + + + + + + + + + + + - - + + diff --git a/README.md b/README.md index afa059f..64eee46 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ | 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | +| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [n-ary_tree_level_order_traversal.js](n-ary_tree_level_order_traversal.js) | 644 ms | 80.9 MB | Easy | | 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | diff --git a/n-ary_tree_level_order_traversal.js b/n-ary_tree_level_order_traversal.js new file mode 100644 index 0000000..419e566 --- /dev/null +++ b/n-ary_tree_level_order_traversal.js @@ -0,0 +1,50 @@ +function Node(val, children) { + this.val = val; + this.children = children; +} +// +// https://leetcode.com/problems/n-ary-tree-level-order-traversal/ + +/** + * // Definition for a Node. + * function Node(val,children) { + * this.val = val; + * this.children = children; + * }; + */ +/** + * @param {Node} root + * @return {number[][]} + */ +var levelOrder = function(root) { + if (!root) return []; + + let queue = [root]; + let result = []; + + while (queue.length > 0) { + let size = queue.length; //? + let levelArray = []; + for (let i = 0; i < size; i++) { + let current = queue.shift(); + if (current) { + levelArray.push(current.val); + if (current.children.length > 0) { + queue.push(...current.children); + } + } + } + + result.push(levelArray); + } + + return result; +}; + +let root = new Node(1, [ + new Node(3, [new Node(5, []), new Node(6, [])]), + new Node(2, []), + new Node(4, []) +]); + +levelOrder(root); //? From 24ed52867246da6d058fcdeb85115876034c1f3e Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sun, 23 Jun 2019 17:19:04 -0700 Subject: [PATCH 25/91] 783. Minimum Distance Between BST Nodes --- README.md | 1 + minimum_distance_between_bst_nodes.js | 46 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 minimum_distance_between_bst_nodes.js diff --git a/README.md b/README.md index 64eee46..74f84e8 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ | 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | | 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [n-ary_tree_level_order_traversal.js](n-ary_tree_level_order_traversal.js) | 644 ms | 80.9 MB | Easy | | 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | +| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy | | 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | diff --git a/minimum_distance_between_bst_nodes.js b/minimum_distance_between_bst_nodes.js new file mode 100644 index 0000000..9670a42 --- /dev/null +++ b/minimum_distance_between_bst_nodes.js @@ -0,0 +1,46 @@ +function TreeNode(val) { + this.val = val; + this.left = this.right = null; +} + +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var minDiffInBST = function(root) { + let queue = [root]; + let min = Infinity; + + while (queue.length > 0) { + let current = queue.shift(); + + if (current) { + if (current.left) { + queue.push(current.left); + let diff = Math.abs(current.left.val - current.val); + min = Math.min(diff, min); + } + if (current.right) { + queue.push(current.right); + let diff = Math.abs(current.right.val - current.val); + min = Math.min(diff, min); + } + } + } + return min; +}; + +const tree = new TreeNode(90); +tree.left = new TreeNode(69); +tree.left.left = new TreeNode(49); +tree.left.right = new TreeNode(89); +tree.left.left.right = new TreeNode(52); + +minDiffInBST(tree); //? From 342e02d18b2f8b26c718fc8e86d823a346187448 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sun, 23 Jun 2019 21:53:06 -0700 Subject: [PATCH 26/91] 414. Third Maximum Number --- .idea/workspace.xml | 137 ++++++++++++++++++++++------------------ README.md | 1 + third_maximum_number.js | 15 +++++ 3 files changed, 90 insertions(+), 63 deletions(-) create mode 100644 third_maximum_number.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d1da1d1..d58846f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,8 +4,8 @@ - + @@ -22,10 +22,10 @@ - + - - + + @@ -34,8 +34,8 @@ - - + + @@ -44,6 +44,15 @@ + + + + + + + + + @@ -59,6 +68,7 @@ depth = counter .children .sort( + TreeNode $PROJECT_DIR$ @@ -70,8 +80,6 @@ @@ -242,21 +252,8 @@ - - - - 1558321720972 - - - 1558322944285 - 1558323582086 @@ -587,17 +584,31 @@ - - - + @@ -608,7 +619,7 @@ - + @@ -641,8 +652,6 @@ @@ -700,9 +711,6 @@ - - - @@ -856,13 +864,6 @@ - - - - - - - @@ -915,27 +916,9 @@ - - - - - - - - - - - - - - - - - - - - - + + + @@ -995,11 +978,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/README.md b/README.md index 74f84e8..d3fae1f 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ | 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy | | 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | +| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [third_maximum_number.js](third_maximum_number.js) | 76 ms | 38 MB | Easy | | 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | diff --git a/third_maximum_number.js b/third_maximum_number.js new file mode 100644 index 0000000..b2cf95f --- /dev/null +++ b/third_maximum_number.js @@ -0,0 +1,15 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var thirdMax = function(nums) { + nums.sort((a, b) => b - a); + nums = [...new Set(nums)]; + + if (nums.length < 3) { + return nums[0]; + } + return nums[2]; +}; + +thirdMax([1, 2, 3]); //? From 6ce8e39c7ebb0bdb9ac10a5bd97eec8b26032ad6 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sun, 23 Jun 2019 22:13:36 -0700 Subject: [PATCH 27/91] 347. Top K Frequent Elements --- README.md | 1 + top_k_frequent_elements.js | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 top_k_frequent_elements.js diff --git a/README.md b/README.md index d3fae1f..5b11781 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ | 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | | 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | +| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | ## Others diff --git a/top_k_frequent_elements.js b/top_k_frequent_elements.js new file mode 100644 index 0000000..afaf683 --- /dev/null +++ b/top_k_frequent_elements.js @@ -0,0 +1,41 @@ +// 347. Top K Frequent Elements + +// https://leetcode.com/problems/top-k-frequent-elements/ + +// +// Given a non-empty array of integers, return the k most frequent elements. +// +// Example 1: +// +// Input: nums = [1,1,1,2,2,3], k = 2 +// Output: [1,2] +// Example 2: +// +// Input: nums = [1], k = 1 +// Output: [1] +// Note: +// +// You may assume k is always valid, 1 ≤ k ≤ number of unique elements. +// Your algorithm's time complexity must be better than O(n log n), where n is the array's size. + +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var topKFrequent = function(nums, k) { + let store = {}; + + for (let val of nums) { + if (store[val] === undefined) store[val] = 0; + store[val]++; + } + + let res = Object.entries(store) + .sort((a, b) => b[1] - a[1]) + .map(a => a[0]); //? + + return res.slice(0, k); //? +}; + +topKFrequent([1, 1, 1, 2, 2, 3], 2); //? From 83263a63ba681587fbbcb829e143e85af19a8063 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Mon, 24 Jun 2019 23:10:24 -0700 Subject: [PATCH 28/91] 167. Two Sum II - Input array is sorted --- .idea/workspace.xml | 119 ++++++++++++++++++++------------------------ README.md | 1 + two_sum_II.js | 23 +++++++++ 3 files changed, 79 insertions(+), 64 deletions(-) create mode 100644 two_sum_II.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d58846f..4eccc89 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,7 +5,7 @@ - + @@ -22,10 +22,10 @@ - + - + @@ -34,8 +34,8 @@ - - + + @@ -44,15 +44,6 @@ - - - - - - - - - @@ -80,9 +71,6 @@ @@ -143,10 +134,9 @@ -1 - -
- + - - - - - + + + + + + + + + + + @@ -71,8 +78,6 @@ @@ -245,14 +252,7 @@ - - - - 1558330842387 - 1558413693875 @@ -590,16 +590,52 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -627,7 +663,7 @@ - + @@ -703,14 +739,6 @@ - - - - - - - - @@ -964,21 +992,35 @@ + + + + + + + - - + + + + + + + + + - - + + diff --git a/README.md b/README.md index cad47b9..1087017 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ | 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | +| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | | 84 ms | 37.6 MB | Medium | ## Others diff --git a/combination_sum_II.js b/combination_sum_II.js new file mode 100644 index 0000000..02eb93b --- /dev/null +++ b/combination_sum_II.js @@ -0,0 +1,46 @@ +/** + * @param {number[]} candidates + * @param {number} target + * @return {number[][]} + */ +var combinationSum2 = function( + candidates, + target, + index = 0, + curr = [], + combinations = [], + store = {} +) { + if (target <= 0) { + if (target === 0) { + let val = curr.slice(0); + let sortedVal = val + .slice(0) + .sort() + .join(""); + if (!store.hasOwnProperty(sortedVal)) { + store[sortedVal] = true; + combinations.push(val); + } + } + return; + } + if (index < candidates.length) { + let current = candidates[index]; + curr.push(current); + combinationSum2( + candidates, + target - current, + index + 1, + curr, + combinations, + store + ); + curr.pop(); + combinationSum2(candidates, target, index + 1, curr, combinations, store); + } + + return combinations; +}; + +combinationSum2([10, 1, 2, 7, 6, 1, 5], 8); //? From c9b0a490a6ca57d0e598e5352a52dab6edb00da4 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Wed, 26 Jun 2019 20:55:16 -0700 Subject: [PATCH 32/91] 40. Combination Sum II --- README.md | 2 +- combination_sum_II.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1087017..b7799d4 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ | 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | -| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | | 84 ms | 37.6 MB | Medium | +| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [combination_sum_II.js](combination_sum_II.js) | 84 ms | 37.6 MB | Medium | ## Others diff --git a/combination_sum_II.js b/combination_sum_II.js index 02eb93b..87d8f6b 100644 --- a/combination_sum_II.js +++ b/combination_sum_II.js @@ -43,4 +43,4 @@ var combinationSum2 = function( return combinations; }; -combinationSum2([10, 1, 2, 7, 6, 1, 5], 8); //? +combinationSum2([10, 1, 2, 7, 6, 1, 5].sort(), 8); //? From 467e0b241b67539748fb3b40555635ce8a4c5104 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Wed, 26 Jun 2019 23:17:58 -0700 Subject: [PATCH 33/91] 189. Rotate Array --- .idea/workspace.xml | 156 ++++++++++++++++++++++---------------------- README.md | 1 + rotate_array.js | 15 +++++ 3 files changed, 95 insertions(+), 77 deletions(-) create mode 100644 rotate_array.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 96dd8f8..d301c9d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,9 +1,12 @@ - - + + + + + @@ -18,13 +21,13 @@ - + - - + + @@ -34,19 +37,10 @@ - + - - - - - - - - - - - + + @@ -78,10 +72,6 @@ @@ -150,6 +144,7 @@ + @@ -161,7 +156,6 @@ + @@ -250,14 +251,8 @@ - - - - 1559453588505 - 1559455717786 @@ -595,11 +590,18 @@ - - @@ -678,7 +680,6 @@ @@ -737,7 +739,6 @@ - @@ -913,27 +914,9 @@ - - - - - - - - - - - - - - - - - - - - - + + + @@ -941,13 +924,7 @@ - - - - - - - + @@ -955,27 +932,9 @@ - - - - - - - - - - - - - - - - - - - - - + + + @@ -997,13 +956,7 @@ - - - - - - - + @@ -1031,15 +984,22 @@ - + + + + + + + + - - + + diff --git a/README.md b/README.md index 769b011..0372121 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ | 46 | [Permutations](https://leetcode.com/problems/permutations/) | [permutations.js](permutations.js) | 88 ms | 37.6 MB | Medium | | 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [remove_duplicates_from_sorted_array_II.js](remove_duplicates_from_sorted_array_II.js) | 64 ms | 35.8 MB | Medium | | 78 | [Subsets](https://leetcode.com/problems/subsets/) | [subset.js](subset.js) | 60 ms | 35 MB | Medium | +| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [reverse_words_in_string.js](reverse_words_in_string.js) | 56 ms | 34.6 MB | Medium | ## Others diff --git a/reverse_words_in_string.js b/reverse_words_in_string.js new file mode 100644 index 0000000..b7e89cf --- /dev/null +++ b/reverse_words_in_string.js @@ -0,0 +1,49 @@ +// Given an input string, reverse the string word by word. +// +// +// +// Example 1: +// +// Input: "the sky is blue" +// Output: "blue is sky the" +// Example 2: +// +// Input: " hello world! " +// Output: "world! hello" +// Explanation: Your reversed string should not contain leading or trailing spaces. +// Example 3: +// +// Input: "a good example" +// Output: "example good a" +// Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string. +// +// +// Note: +// +// A word is defined as a sequence of non-space characters. +// Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces. +// You need to reduce multiple spaces between two words to a single space in the reversed string. +// +// +// Follow up: +// +// For C programmers, try to solve it in-place in O(1) extra space. + +/** + * @param {string} s + * @return {string} + */ +var reverseWords = function(s) { + let stringArray = s + .trim() + .split(" ") + .filter(a => a.length > 0); + let out = ""; + for (let i = stringArray.length - 1; i >= 0; i--) { + out += stringArray[i] + (i >= 1 ? " " : ""); + } + + return out; +}; + +reverseWords("a good example"); //? From a9b4fb494ef54c803b78f3a889571a369ba0c832 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Tue, 2 Jul 2019 10:42:53 -0700 Subject: [PATCH 42/91] 647. Palindromic Substrings --- .idea/workspace.xml | 100 ++++++++++++++++++++++++-------------- README.md | 1 + palindromic_substrings.js | 63 ++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 37 deletions(-) create mode 100644 palindromic_substrings.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 49310b4..dadc33f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,8 @@ - + + @@ -19,10 +20,10 @@ - + - - + + @@ -31,8 +32,8 @@ - - + + @@ -41,6 +42,15 @@ + + + + + + + + + @@ -58,6 +68,7 @@ .sort( TreeNode openStack + WORD LA $PROJECT_DIR$ @@ -69,7 +80,6 @@ @@ -133,9 +144,9 @@ - @@ -252,14 +263,7 @@ - - - - 1559455717786 - 1559522403524 @@ -597,17 +601,24 @@ - - - + - + @@ -618,7 +629,7 @@ - + @@ -680,7 +691,6 @@ @@ -739,7 +750,6 @@ - @@ -934,7 +944,6 @@ - @@ -971,10 +980,27 @@ + + + + + + + + + + + + + + + + + - - + + @@ -983,23 +1009,23 @@ - - + + - + - - + + - - + + diff --git a/README.md b/README.md index 0372121..5a1891a 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ | 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [remove_duplicates_from_sorted_array_II.js](remove_duplicates_from_sorted_array_II.js) | 64 ms | 35.8 MB | Medium | | 78 | [Subsets](https://leetcode.com/problems/subsets/) | [subset.js](subset.js) | 60 ms | 35 MB | Medium | | 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [reverse_words_in_string.js](reverse_words_in_string.js) | 56 ms | 34.6 MB | Medium | +| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [palindromic_substrings.js](palindromic_substrings.js) | 704 ms | 37.2 MB | Medium | ## Others diff --git a/palindromic_substrings.js b/palindromic_substrings.js new file mode 100644 index 0000000..90481a5 --- /dev/null +++ b/palindromic_substrings.js @@ -0,0 +1,63 @@ +// https://leetcode.com/problems/palindromic-substrings/submissions/ + +// +// 647. Palindromic Substrings +// Medium +// +// 1420 +// +// 74 +// +// Favorite +// +// Share +// Given a string, your task is to count how many palindromic substrings in this string. +// +// The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters. +// +// Example 1: +// +// Input: "abc" +// Output: 3 +// Explanation: Three palindromic strings: "a", "b", "c". +// +// +// Example 2: +// +// Input: "aaa" +// Output: 6 +// Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa". + +let isPalindromic = function(array) { + if (array.length === 1) { + return true; + } + + for (let i = 0; i < array.length; i++) { + if (array[i] !== array[array.length - i - 1]) { + return false; + } + } + + return true; +}; + +/** + * @param {string} s + * @return {number} + */ +var countSubstrings = function(s) { + let stringToArray = s.split(""); + let result = 0; + let index = 0; + while (index < s.length) { + for (let i = index; i < s.length; i++) { + if (isPalindromic(stringToArray.slice(index, i + 1))) { + result++; + } + } + index++; + } + return result; +}; +countSubstrings("abc"); //? From a29d4dfc50bb044ff71bbcb1eb4c54463f64b0d9 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Tue, 2 Jul 2019 20:03:02 -0700 Subject: [PATCH 43/91] 102. Binary Tree Level Order Traversal --- .idea/workspace.xml | 100 +++++++++----------- README.md | 135 ++++++++++++++------------- binary_tree_level_order_traversal.js | 54 +++++++++++ 3 files changed, 168 insertions(+), 121 deletions(-) create mode 100644 binary_tree_level_order_traversal.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index dadc33f..8ff896f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + @@ -20,10 +20,10 @@ - + - - + + @@ -32,8 +32,8 @@ - - + + @@ -42,15 +42,6 @@ - - - - - - - - - @@ -66,9 +57,9 @@ depth = counter .children .sort( - TreeNode openStack WORD LA + TreeNode $PROJECT_DIR$ @@ -80,7 +71,6 @@ @@ -153,6 +144,7 @@ + @@ -164,7 +156,6 @@ + @@ -174,7 +176,7 @@ - + @@ -255,28 +257,9 @@ - - - - 1559625729734 - - - 1559626807013 - - - 1559627039050 - 1559709516403 @@ -600,17 +583,38 @@ - - - + - + @@ -621,7 +625,7 @@ - + @@ -683,9 +687,6 @@ @@ -742,56 +746,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -922,13 +876,6 @@ - - - - - - - @@ -985,13 +932,6 @@ - - - - - - - @@ -1006,6 +946,9 @@ + + + @@ -1013,11 +956,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/README.md b/README.md index 8547eec..5a5a86a 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ | 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [rotate_array.js](rotate_array.js) | 92 ms | 35.3 MB | Easy | | | | 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | | | 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [first_unique_character_in_a_string.js](first_unique_character_in_a_string.js) | 124 ms | 39.3 MB | Easy | | | +| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [add_string.js](add_string.js) | 60 ms | 36.5 MB | Easy | 0(N) | O(1) | | 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [two_sum_II.js](two_sum_II.js) | 64 ms | 35.5 MB | Easy | | | | 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [valid_parentheses.js](valid_parentheses.js) | 52 ms | 33.9 MB | Easy | | | | 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [valid_palindrome_II.js](valid_palindrome_II.js) | 72 ms | 43.2 MB | Easy | O(N) | O(1) | @@ -74,7 +75,6 @@ | 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [palindromic_substrings.js](palindromic_substrings.js) | 704 ms | 37.2 MB | Medium | | | | 102. | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [binary_tree_level_order_traversal.js](binary_tree_level_order_traversal.js) | 60 ms | 34.9 MB | Medium | O(N) | | | 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | | -| | | | | | | | | ## Others diff --git a/add_string.js b/add_string.js new file mode 100644 index 0000000..00ef589 --- /dev/null +++ b/add_string.js @@ -0,0 +1,39 @@ +// +// Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. +// +// Note: +// +// The length of both num1 and num2 is < 5100. +// Both num1 and num2 contains only digits 0-9. +// Both num1 and num2 does not contain any leading zero. +// You must not use any built-in BigInteger library or convert the inputs to integer directly. + +/** + * @param {string} num1 + * @param {string} num2 + * @return {string} + */ +var addStrings = function(num1, num2) { + let count = Math.max(num1.length, num2.length); + num1 = num1.padStart(count, "0"); + num2 = num2.padStart(count, "0"); + let carry = 0; + let out = ""; + for (let i = count - 1; i >= 0; i--) { + let val = parseInt(num1.charAt(i)) + parseInt(num2.charAt(i)) + carry; + if (val > 9) { + out = (val % 10) + out; + carry = 1; + } else { + out = val + out; + carry = 0; + } + } + if (carry == 1) { + out = 1 + out; + } + + return out; +}; + +addStrings("999", "999"); //? From 1ec8f271a318c6e069ad6de23ffe855e87eaf9d6 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sat, 6 Jul 2019 13:28:36 -0700 Subject: [PATCH 47/91] 121. Best Time to Buy and Sell Stock --- .idea/workspace.xml | 87 ++++++++++++++---------------- README.md | 3 +- best_time_to_buy_and_sell_stock.js | 36 +++++++++++++ 3 files changed, 79 insertions(+), 47 deletions(-) create mode 100644 best_time_to_buy_and_sell_stock.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6a6c28e..226fcf8 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + @@ -22,10 +22,10 @@ - + - - + + @@ -34,8 +34,8 @@ - - + + @@ -73,8 +73,6 @@ @@ -136,7 +136,7 @@ -1 - + + + + @@ -263,49 +276,7 @@ - - - - 1560045352752 - - - 1560117477497 - - - 1560119043612 - - - 1560133301894 - - - 1560230019977 - - - 1560321149950 - 1560405039334 @@ -608,11 +579,53 @@ - - @@ -691,11 +704,6 @@ @@ -750,24 +763,6 @@ - - - - - - - - - - - - - - - - - - @@ -1021,6 +1016,27 @@ + + + + + + + + + + + + + + + + + + + + + @@ -1030,11 +1046,18 @@ + + + + + + + - - + + diff --git a/README.md b/README.md index e38bcc1..b8549c2 100644 --- a/README.md +++ b/README.md @@ -5,79 +5,80 @@ #### This contain solution of leetcode in javascript -| # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | -|:-----|:----------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:--------------------------------------------| -| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | -| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | -| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | | -| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | | -| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | | | | -| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | | | | -| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | | | | -| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](find_common_characters.js) | 108 ms | 38.1 MB | Easy | | | | -| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution 1](middle_of_the_linked_list.js)
[Solution 2](middle_of_the_linked_list_2.js) | 48 ms
44ms | 33.9 MB
33.8 MB | Easy | | | | -| 126 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](single_number.js) | 60 ms | 37.1 MB | Easy | | | | -| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution 1](max_consecutive_ones.js)
[Solution 2](max_consecutive_ones_2.js) | 56 ms | 37.1 MB | Easy | | | | -| 258 | [ Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](add_digits.js) | 76 ms | 36.3 MB | Easy | | | | -| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](delete_node_linked_list.js) | 60 ms | 35.6 MB | Easy | | | | -| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](valid_anagram.js) | 56 ms | 37.8 MB | Easy | | | | -| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](find_all_numbers_disappeared_in_an_array.js) | 6576 ms | 45.7 MB | Easy | | | | -| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](contains_duplicate.js) | 96 ms | 42.7 MB | Easy | | | | -| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](contains_duplicate_II.js) | 72 ms | 42.4 MB | Easy | | | | -| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution 1](intersection_of_two_arrays.js)
[Solution 2](intersection_of_two_arrays_solution_2.js) | 56 ms | 35.8 MB | Easy | | | | -| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](intersection_of_two_arrays_II.js)
[Solution 2](intersection_of_two_arrays_II_solution2.js) | 84 ms | 38.3 MB | Easy | | | | -| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](squares_of_a_sorted_array.js) | 152 ms | 43.5 MB | Easy | | | | -| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](minimum_index_sum_of_two_lists.js) | 108 ms | 43 MB | Easy | | | | -| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](remove_duplicates_from_sorted_array.js) | 76 ms | 37.2 MB | Easy | | | | -| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](same_tree.js) | 52 ms | 33.9 MB | Easy | | | | -| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Iterative](binary_search.js)
[Recursion](binary_search_recursion.js) | | | Easy | | | | -| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](search_in_a_binary_search_tree.js) | 100ms | 41.7 | Easy | | | | -| 821 | [ Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](shortest_distance_to_a_character.js) | 192 ms | 45.5 MB | Easy | | | | -| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](array_partition_I.js) | 116 ms | 39.1MB | Easy | | | | -| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](number_complement.js) | 52ms | 33.9 MB | Easy | | | | -| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](merge_two_binary_trees.js) | 92 ms | 40.4 MB | Easy | | | | -| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](missing_number.js) | 388 ms | 37.1 MB | Easy | | | | -| 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | | | | -| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | | | -| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | | | -| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | | | -| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | | | -| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | | | | -| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | | | | -| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | | | -| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | | | -| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | | | | -| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [n-ary_tree_level_order_traversal.js](n-ary_tree_level_order_traversal.js) | 644 ms | 80.9 MB | Easy | | | | -| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | | | -| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [duplicate_zeros.js](duplicate_zeros.js) | 64 ms | 35.7 MB | Easy | | | | -| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy | | | | -| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [rotate_array.js](rotate_array.js) | 92 ms | 35.3 MB | Easy | | | | -| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | | | -| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [first_unique_character_in_a_string.js](first_unique_character_in_a_string.js) | 124 ms | 39.3 MB | Easy | | | | -| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [add_string.js](add_string.js) | 60 ms | 36.5 MB | Easy | 0(N) | O(1) | | -| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [two_sum_II.js](two_sum_II.js) | 64 ms | 35.5 MB | Easy | | | | -| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [valid_parentheses.js](valid_parentheses.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(1) | | -| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [best_time_to_buy_and_sell_stock.js](best_time_to_buy_and_sell_stock.js) | 56 ms | 36.5 MB | Easy | 0(N) | O(1) | | -| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [valid_palindrome_II.js](valid_palindrome_II.js) | 72 ms | 43.2 MB | Easy | O(N) | O(1) | | -| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [third_maximum_number.js](third_maximum_number.js) | 76 ms | 38 MB | Easy | | | | -| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | | | -| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | | | -| 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | | | | -| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | | | -| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | | | | -| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | | | -| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | | | -| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | | | | -| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [combination_sum_II.js](combination_sum_II.js) | 84 ms | 37.6 MB | Medium | | | | -| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [permutations.js](permutations.js) | 88 ms | 37.6 MB | Medium | | | | -| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [remove_duplicates_from_sorted_array_II.js](remove_duplicates_from_sorted_array_II.js) | 64 ms | 35.8 MB | Medium | | | | -| 78 | [Subsets](https://leetcode.com/problems/subsets/) | [subset.js](subset.js) | 60 ms | 35 MB | Medium | | | | -| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [reverse_words_in_string.js](reverse_words_in_string.js) | 56 ms | 34.6 MB | Medium | | | | -| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [palindromic_substrings.js](palindromic_substrings.js) | 704 ms | 37.2 MB | Medium | | | | -| 102. | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [binary_tree_level_order_traversal.js](binary_tree_level_order_traversal.js) | 60 ms | 34.9 MB | Medium | O(N) | | | -| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | | | -| 287 | [ Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [find_the_duplicate_number.js](find_the_duplicate_number.js) | 84 ms | 36 MB | Medium | O(N) | O(1) | | -| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [minimum_path_sum.js](minimum_path_sum.js) | 52 ms | 35.5 MB | Medium | 0(N2) | O(1) | https://www.youtube.com/watch?v=lBRtnuxg-gU | +| # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | +|:-----|:----------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:-------------------------------------------------------| +| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | +| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | +| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | | +| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | | +| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | | | | +| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | | | | +| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | | | | +| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](find_common_characters.js) | 108 ms | 38.1 MB | Easy | | | | +| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution 1](middle_of_the_linked_list.js)
[Solution 2](middle_of_the_linked_list_2.js) | 48 ms
44ms | 33.9 MB
33.8 MB | Easy | | | | +| 126 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](single_number.js) | 60 ms | 37.1 MB | Easy | | | | +| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution 1](max_consecutive_ones.js)
[Solution 2](max_consecutive_ones_2.js) | 56 ms | 37.1 MB | Easy | | | | +| 258 | [ Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](add_digits.js) | 76 ms | 36.3 MB | Easy | | | | +| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](delete_node_linked_list.js) | 60 ms | 35.6 MB | Easy | | | | +| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](valid_anagram.js) | 56 ms | 37.8 MB | Easy | | | | +| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](find_all_numbers_disappeared_in_an_array.js) | 6576 ms | 45.7 MB | Easy | | | | +| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](contains_duplicate.js) | 96 ms | 42.7 MB | Easy | | | | +| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](contains_duplicate_II.js) | 72 ms | 42.4 MB | Easy | | | | +| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution 1](intersection_of_two_arrays.js)
[Solution 2](intersection_of_two_arrays_solution_2.js) | 56 ms | 35.8 MB | Easy | | | | +| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](intersection_of_two_arrays_II.js)
[Solution 2](intersection_of_two_arrays_II_solution2.js) | 84 ms | 38.3 MB | Easy | | | | +| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](squares_of_a_sorted_array.js) | 152 ms | 43.5 MB | Easy | | | | +| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](minimum_index_sum_of_two_lists.js) | 108 ms | 43 MB | Easy | | | | +| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](remove_duplicates_from_sorted_array.js) | 76 ms | 37.2 MB | Easy | | | | +| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](same_tree.js) | 52 ms | 33.9 MB | Easy | | | | +| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Iterative](binary_search.js)
[Recursion](binary_search_recursion.js) | | | Easy | | | | +| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](search_in_a_binary_search_tree.js) | 100ms | 41.7 | Easy | | | | +| 821 | [ Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](shortest_distance_to_a_character.js) | 192 ms | 45.5 MB | Easy | | | | +| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](array_partition_I.js) | 116 ms | 39.1MB | Easy | | | | +| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](number_complement.js) | 52ms | 33.9 MB | Easy | | | | +| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](merge_two_binary_trees.js) | 92 ms | 40.4 MB | Easy | | | | +| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](missing_number.js) | 388 ms | 37.1 MB | Easy | | | | +| 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | | | | +| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | | | +| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | | | +| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | | | +| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | | | +| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | | | | +| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | | | | +| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | | | +| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | | | +| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | | | | +| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [n-ary_tree_level_order_traversal.js](n-ary_tree_level_order_traversal.js) | 644 ms | 80.9 MB | Easy | | | | +| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | | | +| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [duplicate_zeros.js](duplicate_zeros.js) | 64 ms | 35.7 MB | Easy | | | | +| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy | | | | +| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [rotate_array.js](rotate_array.js) | 92 ms | 35.3 MB | Easy | | | | +| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | | | +| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [first_unique_character_in_a_string.js](first_unique_character_in_a_string.js) | 124 ms | 39.3 MB | Easy | | | | +| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [add_string.js](add_string.js) | 60 ms | 36.5 MB | Easy | 0(N) | O(1) | | +| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [two_sum_II.js](two_sum_II.js) | 64 ms | 35.5 MB | Easy | | | | +| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [valid_parentheses.js](valid_parentheses.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(1) | | +| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [best_time_to_buy_and_sell_stock.js](best_time_to_buy_and_sell_stock.js) | 56 ms | 36.5 MB | Easy | 0(N) | O(1) | | +| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [valid_palindrome_II.js](valid_palindrome_II.js) | 72 ms | 43.2 MB | Easy | O(N) | O(1) | | +| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [third_maximum_number.js](third_maximum_number.js) | 76 ms | 38 MB | Easy | | | | +| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | | | +| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | | | +| 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | | | | +| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | | | +| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | | | | +| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | | | +| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | | | +| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | | | | +| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [combination_sum_II.js](combination_sum_II.js) | 84 ms | 37.6 MB | Medium | | | | +| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [permutations.js](permutations.js) | 88 ms | 37.6 MB | Medium | | | | +| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [remove_duplicates_from_sorted_array_II.js](remove_duplicates_from_sorted_array_II.js) | 64 ms | 35.8 MB | Medium | | | | +| 78 | [Subsets](https://leetcode.com/problems/subsets/) | [subset.js](subset.js) | 60 ms | 35 MB | Medium | | | | +| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [reverse_words_in_string.js](reverse_words_in_string.js) | 56 ms | 34.6 MB | Medium | | | | +| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [palindromic_substrings.js](palindromic_substrings.js) | 704 ms | 37.2 MB | Medium | | | | +| 102. | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [binary_tree_level_order_traversal.js](binary_tree_level_order_traversal.js) | 60 ms | 34.9 MB | Medium | O(N) | | | +| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | | | +| 287 | [ Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [find_the_duplicate_number.js](find_the_duplicate_number.js) | 84 ms | 36 MB | Medium | O(N) | O(1) | | +| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [minimum_path_sum.js](minimum_path_sum.js) | 52 ms | 35.5 MB | Medium | o(N2) | O(1) | https://www.youtube.com/watch?v=lBRtnuxg-gU | +| 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [group_anagrams.js](group_anagrams.js) | 152 ms | 47.3 MB | Medium | NlogN | | https://leetcode.com/problems/group-anagrams/solution/ | ## Others diff --git a/group_anagrams.js b/group_anagrams.js new file mode 100644 index 0000000..01c9b40 --- /dev/null +++ b/group_anagrams.js @@ -0,0 +1,29 @@ +/** + * @param {string[]} strs + * @return {string[][]} + */ +var groupAnagrams = function(strs) { + let hashMap = {}; + let out = []; + + for (let val of strs) { + let sortedVal = val + .split("") + .sort() + .join(""); + + if (!hashMap.hasOwnProperty(sortedVal)) hashMap[sortedVal] = []; + + if (hashMap.hasOwnProperty(sortedVal)) { + hashMap[sortedVal].push(val); + } + } + + for (let val in hashMap) { + out.push(hashMap[val]); + } + + return out; +}; + +groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]); //? From 5901b7bc6574a0c4b5f4e0c4c5ba728387eff7d0 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sun, 7 Jul 2019 15:01:26 -0700 Subject: [PATCH 56/91] Set theme jekyll-theme-dinky --- _config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_config.yml b/_config.yml index c741881..9da9a02 100644 --- a/_config.yml +++ b/_config.yml @@ -1 +1 @@ -theme: jekyll-theme-slate \ No newline at end of file +theme: jekyll-theme-dinky \ No newline at end of file From 7f070411571c73d98c537cba576fced8ab82b4ed Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sun, 7 Jul 2019 15:03:59 -0700 Subject: [PATCH 57/91] Code cleaned --- README.md | 148 ++++++++++++++++++++++++------------------------ css/custom.scss | 3 - 2 files changed, 74 insertions(+), 77 deletions(-) delete mode 100644 css/custom.scss diff --git a/README.md b/README.md index b8549c2..2e04936 100644 --- a/README.md +++ b/README.md @@ -5,80 +5,80 @@ #### This contain solution of leetcode in javascript -| # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | -|:-----|:----------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:-------------------------------------------------------| -| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | -| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | -| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | | -| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | | -| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | | | | -| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | | | | -| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | | | | -| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](find_common_characters.js) | 108 ms | 38.1 MB | Easy | | | | -| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution 1](middle_of_the_linked_list.js)
[Solution 2](middle_of_the_linked_list_2.js) | 48 ms
44ms | 33.9 MB
33.8 MB | Easy | | | | -| 126 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](single_number.js) | 60 ms | 37.1 MB | Easy | | | | -| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution 1](max_consecutive_ones.js)
[Solution 2](max_consecutive_ones_2.js) | 56 ms | 37.1 MB | Easy | | | | -| 258 | [ Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](add_digits.js) | 76 ms | 36.3 MB | Easy | | | | -| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](delete_node_linked_list.js) | 60 ms | 35.6 MB | Easy | | | | -| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](valid_anagram.js) | 56 ms | 37.8 MB | Easy | | | | -| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](find_all_numbers_disappeared_in_an_array.js) | 6576 ms | 45.7 MB | Easy | | | | -| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](contains_duplicate.js) | 96 ms | 42.7 MB | Easy | | | | -| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](contains_duplicate_II.js) | 72 ms | 42.4 MB | Easy | | | | -| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution 1](intersection_of_two_arrays.js)
[Solution 2](intersection_of_two_arrays_solution_2.js) | 56 ms | 35.8 MB | Easy | | | | -| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](intersection_of_two_arrays_II.js)
[Solution 2](intersection_of_two_arrays_II_solution2.js) | 84 ms | 38.3 MB | Easy | | | | -| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](squares_of_a_sorted_array.js) | 152 ms | 43.5 MB | Easy | | | | -| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](minimum_index_sum_of_two_lists.js) | 108 ms | 43 MB | Easy | | | | -| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](remove_duplicates_from_sorted_array.js) | 76 ms | 37.2 MB | Easy | | | | -| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](same_tree.js) | 52 ms | 33.9 MB | Easy | | | | -| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Iterative](binary_search.js)
[Recursion](binary_search_recursion.js) | | | Easy | | | | -| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](search_in_a_binary_search_tree.js) | 100ms | 41.7 | Easy | | | | -| 821 | [ Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](shortest_distance_to_a_character.js) | 192 ms | 45.5 MB | Easy | | | | -| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](array_partition_I.js) | 116 ms | 39.1MB | Easy | | | | -| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](number_complement.js) | 52ms | 33.9 MB | Easy | | | | -| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](merge_two_binary_trees.js) | 92 ms | 40.4 MB | Easy | | | | -| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](missing_number.js) | 388 ms | 37.1 MB | Easy | | | | -| 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | | | | -| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | | | -| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | | | -| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | | | -| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | | | -| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | | | | -| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | | | | -| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | | | -| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | | | -| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | | | | -| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [n-ary_tree_level_order_traversal.js](n-ary_tree_level_order_traversal.js) | 644 ms | 80.9 MB | Easy | | | | -| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | | | -| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [duplicate_zeros.js](duplicate_zeros.js) | 64 ms | 35.7 MB | Easy | | | | -| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy | | | | -| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [rotate_array.js](rotate_array.js) | 92 ms | 35.3 MB | Easy | | | | -| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | | | -| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [first_unique_character_in_a_string.js](first_unique_character_in_a_string.js) | 124 ms | 39.3 MB | Easy | | | | -| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [add_string.js](add_string.js) | 60 ms | 36.5 MB | Easy | 0(N) | O(1) | | -| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [two_sum_II.js](two_sum_II.js) | 64 ms | 35.5 MB | Easy | | | | -| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [valid_parentheses.js](valid_parentheses.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(1) | | -| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [best_time_to_buy_and_sell_stock.js](best_time_to_buy_and_sell_stock.js) | 56 ms | 36.5 MB | Easy | 0(N) | O(1) | | -| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [valid_palindrome_II.js](valid_palindrome_II.js) | 72 ms | 43.2 MB | Easy | O(N) | O(1) | | -| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [third_maximum_number.js](third_maximum_number.js) | 76 ms | 38 MB | Easy | | | | -| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | | | -| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | | | -| 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | | | | -| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | | | -| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | | | | -| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | | | -| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | | | -| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | | | | -| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [combination_sum_II.js](combination_sum_II.js) | 84 ms | 37.6 MB | Medium | | | | -| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [permutations.js](permutations.js) | 88 ms | 37.6 MB | Medium | | | | -| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [remove_duplicates_from_sorted_array_II.js](remove_duplicates_from_sorted_array_II.js) | 64 ms | 35.8 MB | Medium | | | | -| 78 | [Subsets](https://leetcode.com/problems/subsets/) | [subset.js](subset.js) | 60 ms | 35 MB | Medium | | | | -| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [reverse_words_in_string.js](reverse_words_in_string.js) | 56 ms | 34.6 MB | Medium | | | | -| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [palindromic_substrings.js](palindromic_substrings.js) | 704 ms | 37.2 MB | Medium | | | | -| 102. | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [binary_tree_level_order_traversal.js](binary_tree_level_order_traversal.js) | 60 ms | 34.9 MB | Medium | O(N) | | | -| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | | | -| 287 | [ Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [find_the_duplicate_number.js](find_the_duplicate_number.js) | 84 ms | 36 MB | Medium | O(N) | O(1) | | -| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [minimum_path_sum.js](minimum_path_sum.js) | 52 ms | 35.5 MB | Medium | o(N2) | O(1) | https://www.youtube.com/watch?v=lBRtnuxg-gU | -| 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [group_anagrams.js](group_anagrams.js) | 152 ms | 47.3 MB | Medium | NlogN | | https://leetcode.com/problems/group-anagrams/solution/ | +| # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | +|:-----|:----------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:------------------------------------------------------------------------------------------------------------------| +| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | +| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | +| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | | +| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | | +| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | | | | +| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | | | | +| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | | | | +| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](find_common_characters.js) | 108 ms | 38.1 MB | Easy | | | | +| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution 1](middle_of_the_linked_list.js)
[Solution 2](middle_of_the_linked_list_2.js) | 48 ms
44ms | 33.9 MB
33.8 MB | Easy | | | | +| 126 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](single_number.js) | 60 ms | 37.1 MB | Easy | | | | +| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution 1](max_consecutive_ones.js)
[Solution 2](max_consecutive_ones_2.js) | 56 ms | 37.1 MB | Easy | | | | +| 258 | [ Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](add_digits.js) | 76 ms | 36.3 MB | Easy | | | | +| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](delete_node_linked_list.js) | 60 ms | 35.6 MB | Easy | | | | +| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](valid_anagram.js) | 56 ms | 37.8 MB | Easy | | | | +| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](find_all_numbers_disappeared_in_an_array.js) | 6576 ms | 45.7 MB | Easy | | | | +| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](contains_duplicate.js) | 96 ms | 42.7 MB | Easy | | | | +| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](contains_duplicate_II.js) | 72 ms | 42.4 MB | Easy | | | | +| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution 1](intersection_of_two_arrays.js)
[Solution 2](intersection_of_two_arrays_solution_2.js) | 56 ms | 35.8 MB | Easy | | | | +| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](intersection_of_two_arrays_II.js)
[Solution 2](intersection_of_two_arrays_II_solution2.js) | 84 ms | 38.3 MB | Easy | | | | +| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](squares_of_a_sorted_array.js) | 152 ms | 43.5 MB | Easy | | | | +| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](minimum_index_sum_of_two_lists.js) | 108 ms | 43 MB | Easy | | | | +| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](remove_duplicates_from_sorted_array.js) | 76 ms | 37.2 MB | Easy | | | | +| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](same_tree.js) | 52 ms | 33.9 MB | Easy | | | | +| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Iterative](binary_search.js)
[Recursion](binary_search_recursion.js) | | | Easy | | | | +| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](search_in_a_binary_search_tree.js) | 100ms | 41.7 | Easy | | | | +| 821 | [ Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](shortest_distance_to_a_character.js) | 192 ms | 45.5 MB | Easy | | | | +| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](array_partition_I.js) | 116 ms | 39.1MB | Easy | | | | +| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](number_complement.js) | 52ms | 33.9 MB | Easy | | | | +| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](merge_two_binary_trees.js) | 92 ms | 40.4 MB | Easy | | | | +| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](missing_number.js) | 388 ms | 37.1 MB | Easy | | | | +| 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | | | | +| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | | | +| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | | | +| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | | | +| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | | | +| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | | | | +| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | | | | +| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | | | +| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | | | +| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | | | | +| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [n-ary_tree_level_order_traversal.js](n-ary_tree_level_order_traversal.js) | 644 ms | 80.9 MB | Easy | | | | +| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | | | +| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [duplicate_zeros.js](duplicate_zeros.js) | 64 ms | 35.7 MB | Easy | | | | +| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy | | | | +| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [rotate_array.js](rotate_array.js) | 92 ms | 35.3 MB | Easy | | | | +| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | | | +| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [first_unique_character_in_a_string.js](first_unique_character_in_a_string.js) | 124 ms | 39.3 MB | Easy | | | | +| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [add_string.js](add_string.js) | 60 ms | 36.5 MB | Easy | 0(N) | O(1) | | +| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [two_sum_II.js](two_sum_II.js) | 64 ms | 35.5 MB | Easy | | | | +| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [valid_parentheses.js](valid_parentheses.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(1) | | +| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [best_time_to_buy_and_sell_stock.js](best_time_to_buy_and_sell_stock.js) | 56 ms | 36.5 MB | Easy | 0(N) | O(1) | | +| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [valid_palindrome_II.js](valid_palindrome_II.js) | 72 ms | 43.2 MB | Easy | O(N) | O(1) | | +| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [third_maximum_number.js](third_maximum_number.js) | 76 ms | 38 MB | Easy | | | | +| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | | | +| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | | | +| 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | | | | +| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | | | +| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | | | | +| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | | | +| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | | | +| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | | | | +| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [combination_sum_II.js](combination_sum_II.js) | 84 ms | 37.6 MB | Medium | | | | +| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [permutations.js](permutations.js) | 88 ms | 37.6 MB | Medium | | | | +| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [remove_duplicates_from_sorted_array_II.js](remove_duplicates_from_sorted_array_II.js) | 64 ms | 35.8 MB | Medium | | | | +| 78 | [Subsets](https://leetcode.com/problems/subsets/) | [subset.js](subset.js) | 60 ms | 35 MB | Medium | | | | +| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [reverse_words_in_string.js](reverse_words_in_string.js) | 56 ms | 34.6 MB | Medium | | | | +| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [palindromic_substrings.js](palindromic_substrings.js) | 704 ms | 37.2 MB | Medium | | | | +| 102. | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [binary_tree_level_order_traversal.js](binary_tree_level_order_traversal.js) | 60 ms | 34.9 MB | Medium | O(N) | | | +| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | | | +| 287 | [ Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [find_the_duplicate_number.js](find_the_duplicate_number.js) | 84 ms | 36 MB | Medium | O(N) | O(1) | | +| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [minimum_path_sum.js](minimum_path_sum.js) | 52 ms | 35.5 MB | Medium | o(N2) | O(1) | [https://www.youtube.com/watch?v=lBRtnuxg-gU](https://www.youtube.com/watch?v=lBRtnuxg-gU) | +| 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [group_anagrams.js](group_anagrams.js) | 152 ms | 47.3 MB | Medium | NlogN | | [https://leetcode.com/problems/group-anagrams/solution/](https://leetcode.com/problems/group-anagrams/solution/ ) | ## Others diff --git a/css/custom.scss b/css/custom.scss deleted file mode 100644 index 4b6b5bd..0000000 --- a/css/custom.scss +++ /dev/null @@ -1,3 +0,0 @@ -.inner { - max-width: 1200px; -} From dcd883ef370e002e9207be598fe225c7796cf5af Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sun, 7 Jul 2019 18:37:48 -0700 Subject: [PATCH 58/91] 3. Longest Substring Without Repeating Characters --- .idea/workspace.xml | 81 ++++++++++++++++++-------------------------- README.md | 8 +++++ longest_substring.js | 45 ++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 48 deletions(-) create mode 100644 longest_substring.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 7ee8f7f..7951296 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,8 @@ - - + + @@ -19,10 +19,10 @@ - + - - + + @@ -31,8 +31,8 @@ - - + + @@ -70,7 +70,6 @@ @@ -143,7 +143,6 @@ - @@ -151,26 +150,11 @@ - - - - - - - - - - - - - - - - - @@ -261,14 +268,8 @@ - - - - 1560628463341 - 1560657304862 @@ -606,17 +607,24 @@ - - - + @@ -627,7 +635,7 @@ - + @@ -635,6 +643,7 @@ + @@ -689,7 +698,6 @@ @@ -748,7 +757,6 @@ - @@ -1001,13 +1009,7 @@ - - - - - - - + @@ -1041,8 +1043,8 @@ - - + + @@ -1050,6 +1052,16 @@ + + + + + + + + + + diff --git a/README.md b/README.md index eb44f97..2f87e7a 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ | 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [minimum_path_sum.js](minimum_path_sum.js) | 52 ms | 35.5 MB | Medium | o(N2) | O(1) | [https://www.youtube.com/watch?v=lBRtnuxg-gU](https://www.youtube.com/watch?v=lBRtnuxg-gU) | | 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [group_anagrams.js](group_anagrams.js) | 152 ms | 47.3 MB | Medium | NlogN | | [https://leetcode.com/problems/group-anagrams/solution/](https://leetcode.com/problems/group-anagrams/solution/ ) | | 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Sliding Window Approach](longest_substring.js) | 84 ms | 38.4 MB | Medium | O(N) | | [https://www.youtube.com/watch?v=MK-NZ4hN7rs](https://www.youtube.com/watch?v=MK-NZ4hN7rs) | +| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Iterative](single_number_II.js) | 64 ms | 37.6 MB | Medium | NlogN | | [https://www.youtube.com/watch?v=KXjgQWDvQ24](https://www.youtube.com/watch?v=KXjgQWDvQ24) | ## Others diff --git a/single_number_II.js b/single_number_II.js new file mode 100644 index 0000000..fb231d6 --- /dev/null +++ b/single_number_II.js @@ -0,0 +1,36 @@ +// 137. Single Number II +// Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one. +// +// Note: +// +// Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? +// +// Example 1: +// +// Input: [2,2,3,2] +// Output: 3 +// Example 2: +// +// Input: [0,1,0,1,0,1,99] +// Output: 99 + +/** + * @param {number[]} nums + * @return {number} + */ +var singleNumber = function(nums) { + let map = {}; + + for (let val of nums) { + if (!map.hasOwnProperty(val)) map[val] = 0; + map[val]++; + } + + for (let val in map) { + if (map[val] === 1) { + return val; + } + } +}; + +singleNumber([2, 2, 3, 2]); //? From 284474bf46ee9d77008d8b500fbaaeecb3744459 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Mon, 8 Jul 2019 20:38:46 -0700 Subject: [PATCH 60/91] Given a string, find the length of the longest substring T that contains at most k distinct characters. Example 1: Input: s = "eceba", k = 2 Output: 3 Explanation: T is "ece" which its length is 3. Example 2: Input: s = "aa", k = 1 Output: 2 Explanation: T is "aa" which its length is 2. --- .idea/workspace.xml | 111 ++++++++++++++-------------- README.md | 153 ++++++++++++++++++++------------------- k_distinct_characters.js | 28 +++++++ 3 files changed, 159 insertions(+), 133 deletions(-) create mode 100644 k_distinct_characters.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index cad10a9..bf83dc6 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,8 @@ - - + + @@ -18,12 +18,12 @@ - + - - + + @@ -32,14 +32,11 @@ - - + + - - - - - + + @@ -73,7 +70,6 @@ @@ -150,7 +147,6 @@ - @@ -162,6 +158,7 @@ - @@ -172,7 +183,7 @@ - + @@ -265,14 +276,8 @@ - - - - 1560915104744 - 1560925057962 @@ -610,14 +615,21 @@ - - - + @@ -632,7 +644,7 @@ - + @@ -695,7 +707,6 @@ @@ -754,27 +766,6 @@ - - - - - - - - - - - - - - - - - - - - - @@ -896,13 +887,6 @@
- - - - - - - @@ -1002,21 +986,7 @@ - - - - - - - - - - - - - - @@ -1037,11 +1007,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/README.md b/README.md index 14a1737..a522c28 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ | 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | | | 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | | | 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [most_common_word.js](most_common_word.js) | 76 ms | 37.8 MB | Easy | | | | +| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Recursion](maximum_depth_of_binary_tree.js) | 72 ms | 36.9 MB | Easy | O(N) | 0(1) | https://leetcode.com/explore/featured/card/recursion-i/256/complexity-analysis/2376/#approach-1-recursion | | 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | | | | | 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | | | | | 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | | | | diff --git a/maximum_depth_of_binary_tree.js b/maximum_depth_of_binary_tree.js new file mode 100644 index 0000000..484a08a --- /dev/null +++ b/maximum_depth_of_binary_tree.js @@ -0,0 +1,24 @@ +function TreeNode(val) { + this.val = val; + this.left = this.right = null; +} + +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var maxDepth = function(root) { + if (!root) return 0; + + let left_height = maxDepth(root.left); + let right_height = maxDepth(root.right); + + return Math.max(left_height, right_height) + 1; +}; From d47f589eeaea813f0489447c29307083743018c9 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Tue, 16 Jul 2019 20:06:32 -0700 Subject: [PATCH 66/91] Readme reformatted --- .idea/workspace.xml | 98 ++++++++++++++++----------- README.md | 158 ++++++++++++++++++++++---------------------- 2 files changed, 139 insertions(+), 117 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index c27665a..992a9e8 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,7 +4,7 @@ - + @@ -25,8 +25,8 @@ - - + + @@ -36,10 +36,22 @@
- + - - + + + + + + + + + + + + + + @@ -76,8 +88,6 @@ @@ -143,7 +155,7 @@ -1 - + - - - + + + - - + - + @@ -644,7 +657,7 @@ - + @@ -707,7 +720,6 @@ @@ -766,8 +779,6 @@ - - @@ -1007,13 +1018,7 @@
- - - - - - - + @@ -1049,11 +1054,28 @@ + + + + + + + + + + + + + + + + + - - + + diff --git a/README.md b/README.md index a522c28..92ed74b 100644 --- a/README.md +++ b/README.md @@ -5,85 +5,85 @@ #### This contain solution of leetcode in javascript -| # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | -|:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:------------------------------------------------------------------------------------------------------------------| -| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | -| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | -| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | | -| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | | -| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [most_common_word.js](most_common_word.js) | 76 ms | 37.8 MB | Easy | | | | -| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Recursion](maximum_depth_of_binary_tree.js) | 72 ms | 36.9 MB | Easy | O(N) | 0(1) | https://leetcode.com/explore/featured/card/recursion-i/256/complexity-analysis/2376/#approach-1-recursion | -| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | | | | -| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | | | | -| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | | | | -| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](find_common_characters.js) | 108 ms | 38.1 MB | Easy | | | | -| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution 1](middle_of_the_linked_list.js)
[Solution 2](middle_of_the_linked_list_2.js) | 48 ms
44ms | 33.9 MB
33.8 MB | Easy | | | | -| 126 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](single_number.js) | 60 ms | 37.1 MB | Easy | | | | -| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution 1](max_consecutive_ones.js)
[Solution 2](max_consecutive_ones_2.js) | 56 ms | 37.1 MB | Easy | | | | -| 258 | [ Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](add_digits.js) | 76 ms | 36.3 MB | Easy | | | | -| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](delete_node_linked_list.js) | 60 ms | 35.6 MB | Easy | | | | -| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](valid_anagram.js) | 56 ms | 37.8 MB | Easy | | | | -| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](find_all_numbers_disappeared_in_an_array.js) | 6576 ms | 45.7 MB | Easy | | | | -| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](contains_duplicate.js) | 96 ms | 42.7 MB | Easy | | | | -| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](contains_duplicate_II.js) | 72 ms | 42.4 MB | Easy | | | | -| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution 1](intersection_of_two_arrays.js)
[Solution 2](intersection_of_two_arrays_solution_2.js) | 56 ms | 35.8 MB | Easy | | | | -| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](intersection_of_two_arrays_II.js)
[Solution 2](intersection_of_two_arrays_II_solution2.js) | 84 ms | 38.3 MB | Easy | | | | -| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](squares_of_a_sorted_array.js) | 152 ms | 43.5 MB | Easy | | | | -| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](minimum_index_sum_of_two_lists.js) | 108 ms | 43 MB | Easy | | | | -| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](remove_duplicates_from_sorted_array.js) | 76 ms | 37.2 MB | Easy | | | | -| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](same_tree.js) | 52 ms | 33.9 MB | Easy | | | | -| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Iterative](binary_search.js)
[Recursion](binary_search_recursion.js) | | | Easy | | | | -| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](search_in_a_binary_search_tree.js) | 100ms | 41.7 | Easy | | | | -| 821 | [ Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](shortest_distance_to_a_character.js) | 192 ms | 45.5 MB | Easy | | | | -| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](array_partition_I.js) | 116 ms | 39.1MB | Easy | | | | -| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](number_complement.js) | 52ms | 33.9 MB | Easy | | | | -| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](merge_two_binary_trees.js) | 92 ms | 40.4 MB | Easy | | | | -| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](missing_number.js) | 388 ms | 37.1 MB | Easy | | | | -| 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | | | | -| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | | | -| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | | | -| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | | | -| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | | | -| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | | | | -| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | | | | -| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | | | -| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | | | -| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | | | | -| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [n-ary_tree_level_order_traversal.js](n-ary_tree_level_order_traversal.js) | 644 ms | 80.9 MB | Easy | | | | -| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | | | -| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [duplicate_zeros.js](duplicate_zeros.js) | 64 ms | 35.7 MB | Easy | | | | -| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy | | | | -| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [rotate_array.js](rotate_array.js) | 92 ms | 35.3 MB | Easy | | | | -| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | | | -| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [first_unique_character_in_a_string.js](first_unique_character_in_a_string.js) | 124 ms | 39.3 MB | Easy | | | | -| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [add_string.js](add_string.js) | 60 ms | 36.5 MB | Easy | 0(N) | O(1) | | -| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [two_sum_II.js](two_sum_II.js) | 64 ms | 35.5 MB | Easy | | | | -| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [valid_parentheses.js](valid_parentheses.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(1) | | -| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [best_time_to_buy_and_sell_stock.js](best_time_to_buy_and_sell_stock.js) | 56 ms | 36.5 MB | Easy | 0(N) | O(1) | | -| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [valid_palindrome_II.js](valid_palindrome_II.js) | 72 ms | 43.2 MB | Easy | O(N) | O(1) | | -| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [third_maximum_number.js](third_maximum_number.js) | 76 ms | 38 MB | Easy | | | | -| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | | | -| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | | | -| 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | | | | -| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | | | -| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | | | | -| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | | | -| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | | | -| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | | | | -| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [combination_sum_II.js](combination_sum_II.js) | 84 ms | 37.6 MB | Medium | | | | -| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [permutations.js](permutations.js) | 88 ms | 37.6 MB | Medium | | | | -| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [remove_duplicates_from_sorted_array_II.js](remove_duplicates_from_sorted_array_II.js) | 64 ms | 35.8 MB | Medium | | | | -| 78 | [Subsets](https://leetcode.com/problems/subsets/) | [subset.js](subset.js) | 60 ms | 35 MB | Medium | | | | -| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [reverse_words_in_string.js](reverse_words_in_string.js) | 56 ms | 34.6 MB | Medium | | | | -| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [palindromic_substrings.js](palindromic_substrings.js) | 704 ms | 37.2 MB | Medium | | | | -| 102. | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [binary_tree_level_order_traversal.js](binary_tree_level_order_traversal.js) | 60 ms | 34.9 MB | Medium | O(N) | | | -| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | | | -| 287 | [ Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [find_the_duplicate_number.js](find_the_duplicate_number.js) | 84 ms | 36 MB | Medium | O(N) | O(1) | | -| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [minimum_path_sum.js](minimum_path_sum.js) | 52 ms | 35.5 MB | Medium | o(N2) | O(1) | [https://www.youtube.com/watch?v=lBRtnuxg-gU](https://www.youtube.com/watch?v=lBRtnuxg-gU) | -| 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [group_anagrams.js](group_anagrams.js) | 152 ms | 47.3 MB | Medium | NlogN | | [https://leetcode.com/problems/group-anagrams/solution/](https://leetcode.com/problems/group-anagrams/solution/ ) | -| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Sliding Window Approach](longest_substring.js) | 84 ms | 38.4 MB | Medium | O(N) | | [https://www.youtube.com/watch?v=MK-NZ4hN7rs](https://www.youtube.com/watch?v=MK-NZ4hN7rs) | -| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Iterative](single_number_II.js) | 64 ms | 37.6 MB | Medium | NlogN | | [https://www.youtube.com/watch?v=KXjgQWDvQ24](https://www.youtube.com/watch?v=KXjgQWDvQ24) | -| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) | +| # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | +|:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:--------------------------------------------------------------------------------------------------------------------| +| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | +| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | +| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | | +| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | | +| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [most_common_word.js](most_common_word.js) | 76 ms | 37.8 MB | Easy | | | | +| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Recursion](maximum_depth_of_binary_tree.js) | 72 ms | 36.9 MB | Easy | O(N) | 0(1) | [:link:](https://leetcode.com/explore/featured/card/recursion-i/256/complexity-analysis/2376/#approach-1-recursion) | +| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | | | | +| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | | | | +| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | | | | +| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](find_common_characters.js) | 108 ms | 38.1 MB | Easy | | | | +| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution 1](middle_of_the_linked_list.js)
[Solution 2](middle_of_the_linked_list_2.js) | 48 ms
44ms | 33.9 MB
33.8 MB | Easy | | | | +| 126 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](single_number.js) | 60 ms | 37.1 MB | Easy | | | | +| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution 1](max_consecutive_ones.js)
[Solution 2](max_consecutive_ones_2.js) | 56 ms | 37.1 MB | Easy | | | | +| 258 | [ Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](add_digits.js) | 76 ms | 36.3 MB | Easy | | | | +| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](delete_node_linked_list.js) | 60 ms | 35.6 MB | Easy | | | | +| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](valid_anagram.js) | 56 ms | 37.8 MB | Easy | | | | +| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](find_all_numbers_disappeared_in_an_array.js) | 6576 ms | 45.7 MB | Easy | | | | +| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](contains_duplicate.js) | 96 ms | 42.7 MB | Easy | | | | +| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](contains_duplicate_II.js) | 72 ms | 42.4 MB | Easy | | | | +| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution 1](intersection_of_two_arrays.js)
[Solution 2](intersection_of_two_arrays_solution_2.js) | 56 ms | 35.8 MB | Easy | | | | +| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](intersection_of_two_arrays_II.js)
[Solution 2](intersection_of_two_arrays_II_solution2.js) | 84 ms | 38.3 MB | Easy | | | | +| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](squares_of_a_sorted_array.js) | 152 ms | 43.5 MB | Easy | | | | +| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](minimum_index_sum_of_two_lists.js) | 108 ms | 43 MB | Easy | | | | +| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](remove_duplicates_from_sorted_array.js) | 76 ms | 37.2 MB | Easy | | | | +| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](same_tree.js) | 52 ms | 33.9 MB | Easy | | | | +| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Iterative](binary_search.js)
[Recursion](binary_search_recursion.js) | | | Easy | | | | +| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](search_in_a_binary_search_tree.js) | 100ms | 41.7 | Easy | | | | +| 821 | [ Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](shortest_distance_to_a_character.js) | 192 ms | 45.5 MB | Easy | | | | +| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](array_partition_I.js) | 116 ms | 39.1MB | Easy | | | | +| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](number_complement.js) | 52ms | 33.9 MB | Easy | | | | +| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](merge_two_binary_trees.js) | 92 ms | 40.4 MB | Easy | | | | +| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](missing_number.js) | 388 ms | 37.1 MB | Easy | | | | +| 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | | | | +| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | | | +| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | | | +| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | | | +| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | | | +| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | | | | +| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | | | | +| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | | | +| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | | | +| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | | | | +| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [n-ary_tree_level_order_traversal.js](n-ary_tree_level_order_traversal.js) | 644 ms | 80.9 MB | Easy | | | | +| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | | | +| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [duplicate_zeros.js](duplicate_zeros.js) | 64 ms | 35.7 MB | Easy | | | | +| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy | | | | +| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [rotate_array.js](rotate_array.js) | 92 ms | 35.3 MB | Easy | | | | +| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | | | +| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [first_unique_character_in_a_string.js](first_unique_character_in_a_string.js) | 124 ms | 39.3 MB | Easy | | | | +| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [add_string.js](add_string.js) | 60 ms | 36.5 MB | Easy | 0(N) | O(1) | | +| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [two_sum_II.js](two_sum_II.js) | 64 ms | 35.5 MB | Easy | | | | +| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [valid_parentheses.js](valid_parentheses.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(1) | | +| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [best_time_to_buy_and_sell_stock.js](best_time_to_buy_and_sell_stock.js) | 56 ms | 36.5 MB | Easy | 0(N) | O(1) | | +| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [valid_palindrome_II.js](valid_palindrome_II.js) | 72 ms | 43.2 MB | Easy | O(N) | O(1) | | +| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [third_maximum_number.js](third_maximum_number.js) | 76 ms | 38 MB | Easy | | | | +| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | | | +| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | | | +| 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | | | | +| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | | | +| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | | | | +| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | | | +| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | | | +| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | | | | +| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [combination_sum_II.js](combination_sum_II.js) | 84 ms | 37.6 MB | Medium | | | | +| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [permutations.js](permutations.js) | 88 ms | 37.6 MB | Medium | | | | +| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [remove_duplicates_from_sorted_array_II.js](remove_duplicates_from_sorted_array_II.js) | 64 ms | 35.8 MB | Medium | | | | +| 78 | [Subsets](https://leetcode.com/problems/subsets/) | [subset.js](subset.js) | 60 ms | 35 MB | Medium | | | | +| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [reverse_words_in_string.js](reverse_words_in_string.js) | 56 ms | 34.6 MB | Medium | | | | +| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [palindromic_substrings.js](palindromic_substrings.js) | 704 ms | 37.2 MB | Medium | | | | +| 102. | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [binary_tree_level_order_traversal.js](binary_tree_level_order_traversal.js) | 60 ms | 34.9 MB | Medium | O(N) | | | +| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | | | +| 287 | [ Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [find_the_duplicate_number.js](find_the_duplicate_number.js) | 84 ms | 36 MB | Medium | O(N) | O(1) | | +| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [minimum_path_sum.js](minimum_path_sum.js) | 52 ms | 35.5 MB | Medium | o(N2) | O(1) | [:link:](https://www.youtube.com/watch?v=lBRtnuxg-gU) | +| 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [group_anagrams.js](group_anagrams.js) | 152 ms | 47.3 MB | Medium | NlogN | | [:link:](https://leetcode.com/problems/group-anagrams/solution/ ) | +| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Sliding Window Approach](longest_substring.js) | 84 ms | 38.4 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs) | +| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Iterative](single_number_II.js) | 64 ms | 37.6 MB | Medium | NlogN | | [:link:](https://www.youtube.com/watch?v=KXjgQWDvQ24) | +| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) | ## Others From cff22065a5f70645fd89a71c940b41d0033c4119 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Thu, 18 Jul 2019 19:04:22 -0700 Subject: [PATCH 67/91] Container with most water added --- .idea/workspace.xml | 175 ++++++++++++++++------------------- README.md | 170 +++++++++++++++++----------------- container_with_most_water.js | 29 ++++++ 3 files changed, 196 insertions(+), 178 deletions(-) create mode 100644 container_with_most_water.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 992a9e8..9a64973 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,10 +2,11 @@ - - + + - + + @@ -20,13 +21,22 @@ - + + + + + + + + + + - - + + @@ -35,27 +45,6 @@ - - - - - - - - - - - - - - - - - - - - - @@ -88,11 +77,6 @@ @@ -155,7 +144,7 @@ -1 - + - - + - + @@ -657,7 +647,7 @@ - + @@ -720,7 +710,6 @@ @@ -779,23 +769,6 @@ - - - - - - - - - - - - - - - - - @@ -887,16 +860,6 @@
- - - - - - - - - - @@ -990,13 +953,7 @@
- - - - - - - + @@ -1019,13 +976,7 @@ - - - - - - - + @@ -1056,7 +1007,7 @@ - + @@ -1064,18 +1015,54 @@ + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/README.md b/README.md index 92ed74b..4b1c2c6 100644 --- a/README.md +++ b/README.md @@ -5,93 +5,95 @@ #### This contain solution of leetcode in javascript -| # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | -|:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:--------------------------------------------------------------------------------------------------------------------| -| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | -| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | -| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | | -| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | | -| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [most_common_word.js](most_common_word.js) | 76 ms | 37.8 MB | Easy | | | | -| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Recursion](maximum_depth_of_binary_tree.js) | 72 ms | 36.9 MB | Easy | O(N) | 0(1) | [:link:](https://leetcode.com/explore/featured/card/recursion-i/256/complexity-analysis/2376/#approach-1-recursion) | -| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | | | | -| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | | | | -| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | | | | -| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](find_common_characters.js) | 108 ms | 38.1 MB | Easy | | | | -| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution 1](middle_of_the_linked_list.js)
[Solution 2](middle_of_the_linked_list_2.js) | 48 ms
44ms | 33.9 MB
33.8 MB | Easy | | | | -| 126 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](single_number.js) | 60 ms | 37.1 MB | Easy | | | | -| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution 1](max_consecutive_ones.js)
[Solution 2](max_consecutive_ones_2.js) | 56 ms | 37.1 MB | Easy | | | | -| 258 | [ Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](add_digits.js) | 76 ms | 36.3 MB | Easy | | | | -| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](delete_node_linked_list.js) | 60 ms | 35.6 MB | Easy | | | | -| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](valid_anagram.js) | 56 ms | 37.8 MB | Easy | | | | -| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](find_all_numbers_disappeared_in_an_array.js) | 6576 ms | 45.7 MB | Easy | | | | -| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](contains_duplicate.js) | 96 ms | 42.7 MB | Easy | | | | -| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](contains_duplicate_II.js) | 72 ms | 42.4 MB | Easy | | | | -| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution 1](intersection_of_two_arrays.js)
[Solution 2](intersection_of_two_arrays_solution_2.js) | 56 ms | 35.8 MB | Easy | | | | -| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](intersection_of_two_arrays_II.js)
[Solution 2](intersection_of_two_arrays_II_solution2.js) | 84 ms | 38.3 MB | Easy | | | | -| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](squares_of_a_sorted_array.js) | 152 ms | 43.5 MB | Easy | | | | -| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](minimum_index_sum_of_two_lists.js) | 108 ms | 43 MB | Easy | | | | -| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](remove_duplicates_from_sorted_array.js) | 76 ms | 37.2 MB | Easy | | | | -| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](same_tree.js) | 52 ms | 33.9 MB | Easy | | | | -| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Iterative](binary_search.js)
[Recursion](binary_search_recursion.js) | | | Easy | | | | -| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](search_in_a_binary_search_tree.js) | 100ms | 41.7 | Easy | | | | -| 821 | [ Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](shortest_distance_to_a_character.js) | 192 ms | 45.5 MB | Easy | | | | -| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](array_partition_I.js) | 116 ms | 39.1MB | Easy | | | | -| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](number_complement.js) | 52ms | 33.9 MB | Easy | | | | -| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](merge_two_binary_trees.js) | 92 ms | 40.4 MB | Easy | | | | -| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](missing_number.js) | 388 ms | 37.1 MB | Easy | | | | -| 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | | | | -| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | | | -| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | | | -| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | | | -| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | | | -| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | | | | -| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | | | | -| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | | | -| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | | | -| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | | | | -| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [n-ary_tree_level_order_traversal.js](n-ary_tree_level_order_traversal.js) | 644 ms | 80.9 MB | Easy | | | | -| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | | | -| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [duplicate_zeros.js](duplicate_zeros.js) | 64 ms | 35.7 MB | Easy | | | | -| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy | | | | -| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [rotate_array.js](rotate_array.js) | 92 ms | 35.3 MB | Easy | | | | -| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | | | -| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [first_unique_character_in_a_string.js](first_unique_character_in_a_string.js) | 124 ms | 39.3 MB | Easy | | | | -| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [add_string.js](add_string.js) | 60 ms | 36.5 MB | Easy | 0(N) | O(1) | | -| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [two_sum_II.js](two_sum_II.js) | 64 ms | 35.5 MB | Easy | | | | -| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [valid_parentheses.js](valid_parentheses.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(1) | | -| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [best_time_to_buy_and_sell_stock.js](best_time_to_buy_and_sell_stock.js) | 56 ms | 36.5 MB | Easy | 0(N) | O(1) | | -| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [valid_palindrome_II.js](valid_palindrome_II.js) | 72 ms | 43.2 MB | Easy | O(N) | O(1) | | -| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [third_maximum_number.js](third_maximum_number.js) | 76 ms | 38 MB | Easy | | | | -| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | | | -| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | | | -| 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | | | | -| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | | | -| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | | | | -| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | | | -| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | | | -| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | | | | -| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [combination_sum_II.js](combination_sum_II.js) | 84 ms | 37.6 MB | Medium | | | | -| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [permutations.js](permutations.js) | 88 ms | 37.6 MB | Medium | | | | -| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [remove_duplicates_from_sorted_array_II.js](remove_duplicates_from_sorted_array_II.js) | 64 ms | 35.8 MB | Medium | | | | -| 78 | [Subsets](https://leetcode.com/problems/subsets/) | [subset.js](subset.js) | 60 ms | 35 MB | Medium | | | | -| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [reverse_words_in_string.js](reverse_words_in_string.js) | 56 ms | 34.6 MB | Medium | | | | -| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [palindromic_substrings.js](palindromic_substrings.js) | 704 ms | 37.2 MB | Medium | | | | -| 102. | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [binary_tree_level_order_traversal.js](binary_tree_level_order_traversal.js) | 60 ms | 34.9 MB | Medium | O(N) | | | -| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | | | -| 287 | [ Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [find_the_duplicate_number.js](find_the_duplicate_number.js) | 84 ms | 36 MB | Medium | O(N) | O(1) | | -| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [minimum_path_sum.js](minimum_path_sum.js) | 52 ms | 35.5 MB | Medium | o(N2) | O(1) | [:link:](https://www.youtube.com/watch?v=lBRtnuxg-gU) | -| 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [group_anagrams.js](group_anagrams.js) | 152 ms | 47.3 MB | Medium | NlogN | | [:link:](https://leetcode.com/problems/group-anagrams/solution/ ) | -| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Sliding Window Approach](longest_substring.js) | 84 ms | 38.4 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs) | -| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Iterative](single_number_II.js) | 64 ms | 37.6 MB | Medium | NlogN | | [:link:](https://www.youtube.com/watch?v=KXjgQWDvQ24) | -| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) | +| # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | +|:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:------------------------------------------------------------------| +| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | +| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | +| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | | +| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | | +| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [most_common_word.js](most_common_word.js) | 76 ms | 37.8 MB | Easy | | | | +| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Recursion](maximum_depth_of_binary_tree.js) | 72 ms | 36.9 MB | Easy | O(N) | 0(1) | [:link:](http://bit.ly/2XQnNJI) | +| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | | | | +| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | | | | +| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | | | | +| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](find_common_characters.js) | 108 ms | 38.1 MB | Easy | | | | +| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution 1](middle_of_the_linked_list.js)
[Solution 2](middle_of_the_linked_list_2.js) | 48 ms
44ms | 33.9 MB
33.8 MB | Easy | | | | +| 126 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](single_number.js) | 60 ms | 37.1 MB | Easy | | | | +| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution 1](max_consecutive_ones.js)
[Solution 2](max_consecutive_ones_2.js) | 56 ms | 37.1 MB | Easy | | | | +| 258 | [ Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](add_digits.js) | 76 ms | 36.3 MB | Easy | | | | +| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](delete_node_linked_list.js) | 60 ms | 35.6 MB | Easy | | | | +| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](valid_anagram.js) | 56 ms | 37.8 MB | Easy | | | | +| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](find_all_numbers_disappeared_in_an_array.js) | 6576 ms | 45.7 MB | Easy | | | | +| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](contains_duplicate.js) | 96 ms | 42.7 MB | Easy | | | | +| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](contains_duplicate_II.js) | 72 ms | 42.4 MB | Easy | | | | +| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution 1](intersection_of_two_arrays.js)
[Solution 2](intersection_of_two_arrays_solution_2.js) | 56 ms | 35.8 MB | Easy | | | | +| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](intersection_of_two_arrays_II.js)
[Solution 2](intersection_of_two_arrays_II_solution2.js) | 84 ms | 38.3 MB | Easy | | | | +| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](squares_of_a_sorted_array.js) | 152 ms | 43.5 MB | Easy | | | | +| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](minimum_index_sum_of_two_lists.js) | 108 ms | 43 MB | Easy | | | | +| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](remove_duplicates_from_sorted_array.js) | 76 ms | 37.2 MB | Easy | | | | +| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](same_tree.js) | 52 ms | 33.9 MB | Easy | | | | +| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Iterative](binary_search.js)
[Recursion](binary_search_recursion.js) | | | Easy | | | | +| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](search_in_a_binary_search_tree.js) | 100ms | 41.7 | Easy | | | | +| 821 | [ Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](shortest_distance_to_a_character.js) | 192 ms | 45.5 MB | Easy | | | | +| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](array_partition_I.js) | 116 ms | 39.1MB | Easy | | | | +| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](number_complement.js) | 52ms | 33.9 MB | Easy | | | | +| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](merge_two_binary_trees.js) | 92 ms | 40.4 MB | Easy | | | | +| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](missing_number.js) | 388 ms | 37.1 MB | Easy | | | | +| 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | | | | +| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | | | +| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | | | +| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | | | +| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | | | +| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | | | | +| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | | | | +| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | | | +| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | | | +| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | | | | +| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [n-ary_tree_level_order_traversal.js](n-ary_tree_level_order_traversal.js) | 644 ms | 80.9 MB | Easy | | | | +| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | | | +| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [duplicate_zeros.js](duplicate_zeros.js) | 64 ms | 35.7 MB | Easy | | | | +| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy | | | | +| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [rotate_array.js](rotate_array.js) | 92 ms | 35.3 MB | Easy | | | | +| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | | | +| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [first_unique_character_in_a_string.js](first_unique_character_in_a_string.js) | 124 ms | 39.3 MB | Easy | | | | +| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [add_string.js](add_string.js) | 60 ms | 36.5 MB | Easy | 0(N) | O(1) | | +| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [two_sum_II.js](two_sum_II.js) | 64 ms | 35.5 MB | Easy | | | | +| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [valid_parentheses.js](valid_parentheses.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(1) | | +| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [best_time_to_buy_and_sell_stock.js](best_time_to_buy_and_sell_stock.js) | 56 ms | 36.5 MB | Easy | 0(N) | O(1) | | +| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [valid_palindrome_II.js](valid_palindrome_II.js) | 72 ms | 43.2 MB | Easy | O(N) | O(1) | | +| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [third_maximum_number.js](third_maximum_number.js) | 76 ms | 38 MB | Easy | | | | +| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | | | +| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | | | +| 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | | | | +| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | | | +| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | | | | +| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | | | +| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | | | +| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | | | | +| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [combination_sum_II.js](combination_sum_II.js) | 84 ms | 37.6 MB | Medium | | | | +| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [permutations.js](permutations.js) | 88 ms | 37.6 MB | Medium | | | | +| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [remove_duplicates_from_sorted_array_II.js](remove_duplicates_from_sorted_array_II.js) | 64 ms | 35.8 MB | Medium | | | | +| 78 | [Subsets](https://leetcode.com/problems/subsets/) | [subset.js](subset.js) | 60 ms | 35 MB | Medium | | | | +| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [reverse_words_in_string.js](reverse_words_in_string.js) | 56 ms | 34.6 MB | Medium | | | | +| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [palindromic_substrings.js](palindromic_substrings.js) | 704 ms | 37.2 MB | Medium | | | | +| 102. | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [binary_tree_level_order_traversal.js](binary_tree_level_order_traversal.js) | 60 ms | 34.9 MB | Medium | O(N) | | | +| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | | | +| 287 | [ Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [find_the_duplicate_number.js](find_the_duplicate_number.js) | 84 ms | 36 MB | Medium | O(N) | O(1) | | +| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [minimum_path_sum.js](minimum_path_sum.js) | 52 ms | 35.5 MB | Medium | o(N2) | O(1) | [:link:](https://www.youtube.com/watch?v=lBRtnuxg-gU) | +| 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [group_anagrams.js](group_anagrams.js) | 152 ms | 47.3 MB | Medium | NlogN | | [:link:](https://leetcode.com/problems/group-anagrams/solution/ ) | +| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Sliding Window Approach](longest_substring.js) | 84 ms | 38.4 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs) | +| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Iterative](single_number_II.js) | 64 ms | 37.6 MB | Medium | NlogN | | [:link:](https://www.youtube.com/watch?v=KXjgQWDvQ24) | +| 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [Ratcheting](container_with_most_water.js) | 52 ms | 35.4 MB | Medium | O(N) | O(1) | [:link:](https://www.youtube.com/watch?v=k5fbSqb9sCI&t=138s) | +| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) | ## Others -| | Title | Solution | -|:--|:----------|:-----------------------------| -| 1 | Tree | [tree.js](tree.js) | -| 2 | Factorial | [factorial.js](factorial.js) | -| 3 | Graph | [graph.js](graph.js) | +| | Title | Solution | Links | +|:--|:---------------|:-----------------------------|:-------------------------------------------------------------------------| +| 1 | Tree | [tree.js](tree.js) | | +| 2 | Factorial | [factorial.js](factorial.js) | | +| 3 | Graph | [graph.js](graph.js) | [:link:](https://www.geeksforgeeks.org/implementation-graph-javascript/) | +| 4 | Priority Queue | | | ## Approach diff --git a/container_with_most_water.js b/container_with_most_water.js new file mode 100644 index 0000000..242c589 --- /dev/null +++ b/container_with_most_water.js @@ -0,0 +1,29 @@ +//https://leetcode.com/problems/container-with-most-water/submissions/ + +// 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 contains the most water. +// +// Note: You may not slant the container and n is at least 2. + +/** + * @param {number[]} height + * @return {number} + */ +var maxArea = function(height) { + let start = 0, + end = height.length - 1, + area = -Infinity; + + while (start < end) { + const currentArea = Math.min(height[start], height[end]) * (end - start); + area = Math.max(area, currentArea); + if (height[start] < height[end]) { + start++; + } else { + end--; + } + } + + return area; +}; + +maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7]); //? From 175e7eba4e0398047b5489e08ecfe5ef09b3a497 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sun, 21 Jul 2019 09:52:38 -0700 Subject: [PATCH 68/91] 941. Valid Mountain Array --- .idea/workspace.xml | 57 +++++++++++++++++++++++------------------ README.md | 1 + valid_mountain_array.js | 36 ++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 valid_mountain_array.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9a64973..7db9cd1 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,11 +2,11 @@ - + @@ -21,12 +21,12 @@ - + - + - - + + @@ -35,8 +35,8 @@ - - + + @@ -77,7 +77,6 @@ @@ -154,7 +154,6 @@ - @@ -166,6 +165,7 @@ - - - - - + @@ -278,14 +168,8 @@ - - - - 1561187342444 - 1561187434995 @@ -623,76 +507,16 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -706,9 +530,19 @@
+ + + @@ -766,324 +601,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/README.md b/README.md index 49bd12d..a2e9f4b 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ | 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | | 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Recursive](reverse_string.js) | 112 ms | 48.6 MB | Easy | 0(N) | 0(1) | | | 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | +| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Recursive](climbing_stairs.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(N) | [:link:](https://www.youtube.com/watch?v=NFJ3m9a1oJQ) | | 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | | | 941 | [Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/) | [Ittrative](valid_mountain_array.js) | 56 ms | 36.8 MB | Easy | 0(N) | O(1) | [:link:](https://www.youtube.com/watch?v=WWysBX-N2Ak) | | 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | | diff --git a/climbing_stairs.js b/climbing_stairs.js new file mode 100644 index 0000000..90bf80c --- /dev/null +++ b/climbing_stairs.js @@ -0,0 +1,33 @@ +/** + * @param {number} n + * @return {number} + */ +var climbStairs = function(n, memo = []) { + /* + 0 distinct ways to climb negative steps if we + can only take 1 or 2 steps + */ + if (n === 0) { + return 1; + } + + /* + 1 distinct way to climb 1 if we can only take 1 + or 2 steps. + We take 1 step. + */ + if (n < 0) { + return 0; + } + + if (memo[n] > 0) { + return memo[n]; + } + + const left = climbStairs(n - 1, memo); + const right = climbStairs(n - 2, memo); + memo[n] = left + right; + return memo[n]; +}; + +climbStairs(38); From 0cfe68749e042b571c57640f18d69117ac68d8cd Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Tue, 23 Jul 2019 22:48:01 -0700 Subject: [PATCH 72/91] 122. Best Time to Buy and Sell Stock II --- .idea/workspace.xml | 32 +++++++++++++++------------ README.md | 1 + best_time_to_buy_and_sell_stock_II.js | 17 ++++++++++++++ climbing_stairs.js | 4 ++-- 4 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 best_time_to_buy_and_sell_stock_II.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2d64119..31aa9e8 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,10 +2,10 @@ - - + + @@ -535,7 +537,9 @@ - + + @@ -543,7 +547,6 @@ diff --git a/README.md b/README.md index a2e9f4b..f33aa3f 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ | # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | |:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:------------------------------------------------------------------| | 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | +| 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [iterative](best_time_to_buy_and_sell_stock_II.js) | 56 ms | 35.4 MB | Easy | O(N) | O(1) | | | 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Recursive](reverse_string.js) | 112 ms | 48.6 MB | Easy | 0(N) | 0(1) | | | 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | | 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Recursive](climbing_stairs.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(N) | [:link:](https://www.youtube.com/watch?v=NFJ3m9a1oJQ) | diff --git a/best_time_to_buy_and_sell_stock_II.js b/best_time_to_buy_and_sell_stock_II.js new file mode 100644 index 0000000..836d683 --- /dev/null +++ b/best_time_to_buy_and_sell_stock_II.js @@ -0,0 +1,17 @@ +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function(prices) { + let maxProfit = 0; + + for (let i = 1; i < prices.length; i++) { + if (prices[i] > prices[i - 1]) { + maxProfit += prices[i] - prices[i - 1]; + } + } + + return maxProfit; +}; + +maxProfit([7, 1, 5, 3, 6, 4]); //? diff --git a/climbing_stairs.js b/climbing_stairs.js index 90bf80c..cce9b9f 100644 --- a/climbing_stairs.js +++ b/climbing_stairs.js @@ -24,10 +24,10 @@ var climbStairs = function(n, memo = []) { return memo[n]; } - const left = climbStairs(n - 1, memo); + const left = climbStairs(n - 1, memo); //? const right = climbStairs(n - 2, memo); memo[n] = left + right; return memo[n]; }; -climbStairs(38); +climbStairs(6); //? From 096c39b7856db702595d40c6193fb82526765316 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sat, 27 Jul 2019 18:00:59 -0700 Subject: [PATCH 73/91] 122. Best Time to Buy and Sell Stock II --- .idea/workspace.xml | 31 ++++++++++++++++-------------- README.md | 1 + valid_sudoku.js | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 valid_sudoku.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 31aa9e8..b3b14f1 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,11 +1,10 @@ - - + + - @@ -547,7 +550,6 @@ diff --git a/README.md b/README.md index f33aa3f..c261b8e 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ | 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Iterative](single_number_II.js) | 64 ms | 37.6 MB | Medium | NlogN | | [:link:](https://www.youtube.com/watch?v=KXjgQWDvQ24) | | 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [Ratcheting](container_with_most_water.js) | 52 ms | 35.4 MB | Medium | O(N) | O(1) | [:link:](https://www.youtube.com/watch?v=k5fbSqb9sCI&t=138s) | | 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [Recursive](swap_nodes_in_pairs.js) | 68 ms | 34 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=zOovxGmION4) | +| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Itterative](valid_sudoku.js) | 68 ms | 37.7 MB | Medium | O(1) | O(1) | [:link:](https://leetcode.com/problems/valid-sudoku/solution/) | | 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) | ## Others diff --git a/valid_sudoku.js b/valid_sudoku.js new file mode 100644 index 0000000..9036df0 --- /dev/null +++ b/valid_sudoku.js @@ -0,0 +1,47 @@ +// 36. Valid Sudoku +// https://leetcode.com/problems/valid-sudoku/ + +/** + * @param {character[][]} board + * @return {boolean} + */ +let isValidSudoku = function(board) { + let rows = {}; + let colums = {}; + let boxex = {}; + + for (let i = 0; i < 9; i++) { + for (let j = 0; j < 9; j++) { + const current = board[i][j]; + if (current !== ".") { + const boxIndex = Math.floor(i / 3 ) * 3 + Math.floor(j / 3); + if (!rows.hasOwnProperty(i)) rows[i] = new Set(); + if (!colums.hasOwnProperty(j)) colums[j] = new Set(); + if (!boxex.hasOwnProperty(boxIndex)) boxex[boxIndex] = new Set(); + + if(rows[i].has(current) || colums[j].has(current) || boxex[boxIndex].has(current)){ + return false + } + + rows[i].add(current) + colums[j].add(current) + boxex[boxIndex].add(current) + + } + } + } + + return true +}; + +isValidSudoku([ + ["5","3",".",".","7",".",".",".","."], + ["6",".",".","1","9","5",".",".","."], + [".","9","8",".",".",".",".","6","."], + ["8",".",".",".","6",".",".",".","3"], + ["4",".",".","8",".","3",".",".","1"], + ["7",".",".",".","2",".",".",".","6"], + [".","6",".",".",".",".","2","8","."], + [".",".",".","4","1","9",".",".","5"], + [".",".",".",".","8",".",".","7","9"] +]); From c8294799ebd1cb5d4148facbfce6440e36665b21 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Wed, 31 Jul 2019 23:00:19 -0700 Subject: [PATCH 74/91] 48. Rotate Image --- .idea/workspace.xml | 42 +++++++++++++----------------------------- README.md | 1 + rotate_image.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 29 deletions(-) create mode 100644 rotate_image.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index b3b14f1..5e5734c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,8 @@ - - + + @@ -19,24 +19,6 @@ - - - toLo - depth = counter - .children - .sort( - openStack - WORD LA - TreeNode - start++ - left++ - level - 344 - - - $PROJECT_DIR$ - - @@ -174,14 +156,9 @@ - - - - 1561265709874 - 1561268331519 @@ -519,7 +496,14 @@ - diff --git a/README.md b/README.md index c261b8e..9213626 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ | 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Iterative](single_number_II.js) | 64 ms | 37.6 MB | Medium | NlogN | | [:link:](https://www.youtube.com/watch?v=KXjgQWDvQ24) | | 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [Ratcheting](container_with_most_water.js) | 52 ms | 35.4 MB | Medium | O(N) | O(1) | [:link:](https://www.youtube.com/watch?v=k5fbSqb9sCI&t=138s) | | 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [Recursive](swap_nodes_in_pairs.js) | 68 ms | 34 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=zOovxGmION4) | +| 48 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [Iterative](rotate_image.js) | 56 ms | 33.7 MB | Medium | O(N) | O(1) | [:link:](https://leetcode.com/problems/rotate-image/solution/) | | 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Itterative](valid_sudoku.js) | 68 ms | 37.7 MB | Medium | O(1) | O(1) | [:link:](https://leetcode.com/problems/valid-sudoku/solution/) | | 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) | diff --git a/rotate_image.js b/rotate_image.js new file mode 100644 index 0000000..5d15857 --- /dev/null +++ b/rotate_image.js @@ -0,0 +1,28 @@ +// https://leetcode.com/problems/rotate-image/ + +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var rotate = function(matrix) { + const n = matrix.length; + + //tranpose the matric + for (let i = 0; i < n; i++) { + for (let j = i; j < n; j++) { + const temp = matrix[j][i]; + matrix[j][i] = matrix[i][j]; + matrix[i][j] = temp; + } + } + // reverse row + for (let i = 0; i < n; i++) { + for (let j = 0; j < Math.floor(n / 2); j++) { + let tmp = matrix[i][j]; + matrix[i][j] = matrix[i][n - j - 1]; + matrix[i][n - j - 1] = tmp; + } + } +}; + +rotate([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); //? From 34417719d8e98f41d42cb1a2ef782494fd8c3ee8 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sun, 11 Aug 2019 18:43:12 -0700 Subject: [PATCH 75/91] 1122. Relative Sort Array --- .idea/workspace.xml | 29 +++++++++++--------- README.md | 1 + relative_sort_array.js | 61 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 relative_sort_array.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 5e5734c..0901563 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,8 @@ - - + + @@ -158,14 +158,10 @@ - - - - 1561268331519 - 1561280472289 @@ -503,7 +499,14 @@ - @@ -534,7 +537,6 @@ diff --git a/README.md b/README.md index 9213626..c19fa6e 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ |:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:------------------------------------------------------------------| | 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | | 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [iterative](best_time_to_buy_and_sell_stock_II.js) | 56 ms | 35.4 MB | Easy | O(N) | O(1) | | +| 1122 | [Relative Sort Array](https://leetcode.com/problems/relative-sort-array/) | [Ittrative](relative_sort_array.js) | 52 ms | 34.9 MB | Easy | | | | | 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Recursive](reverse_string.js) | 112 ms | 48.6 MB | Easy | 0(N) | 0(1) | | | 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | | 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Recursive](climbing_stairs.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(N) | [:link:](https://www.youtube.com/watch?v=NFJ3m9a1oJQ) | diff --git a/relative_sort_array.js b/relative_sort_array.js new file mode 100644 index 0000000..294b44d --- /dev/null +++ b/relative_sort_array.js @@ -0,0 +1,61 @@ +// Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1. +// +// Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order. +// +// +// +// Example 1: +// +// Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6] +// Output: [2,2,2,1,4,3,3,9,6,7,19] +// +// +// Constraints: +// +// arr1.length, arr2.length <= 1000 +// 0 <= arr1[i], arr2[i] <= 1000 +// Each arr2[i] is distinct. +// Each arr2[i] is in arr1. + +let arrayToSet = function(arr) { + let arrayMap = new Map(); + for (let val of arr) { + if (!arrayMap.has(val)) { + arrayMap.set(val, 0); + } + arrayMap.set(val, arrayMap.get(val) + 1); + } + + return arrayMap; +}; + +/** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number[]} + */ +var relativeSortArray = function(arr1, arr2) { + let out = []; + let remainingArray = []; + + let arrayMap = arrayToSet(arr1); + + for (let val of arr2) { + out.push(...Array.from({ length: arrayMap.get(val) }, i => val)); + arrayMap.delete(val); + } + + arrayMap.forEach(function(value, key) { + remainingArray.push(...Array.from({ length: arrayMap.get(key) }, i => key)); + }); + + remainingArray.sort((a, b) => a - b); //? + + out = [...out, ...remainingArray]; + return out; +}; + +relativeSortArray( + [2, 21, 43, 38, 0, 42, 33, 7, 24, 13, 12, 27, 12, 24, 5, 23, 29, 48, 30, 31], + [2, 42, 38, 0, 43, 21] +); //? From 410438848dfd13b098e76a5c32cd426af5013b43 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Thu, 15 Aug 2019 15:55:29 -0700 Subject: [PATCH 76/91] 67. Add Binary --- .idea/workspace.xml | 29 ++++++++++++++++------------- README.md | 3 ++- addBinary.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 addBinary.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0901563..d40cded 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,8 @@ - - + + @@ -161,14 +161,10 @@ - - - - 1561280472289 - 1561335544480 @@ -506,7 +502,14 @@ - @@ -537,7 +540,6 @@ diff --git a/README.md b/README.md index c19fa6e..313de0b 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,8 @@ |:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:------------------------------------------------------------------| | 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | | 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [iterative](best_time_to_buy_and_sell_stock_II.js) | 56 ms | 35.4 MB | Easy | O(N) | O(1) | | -| 1122 | [Relative Sort Array](https://leetcode.com/problems/relative-sort-array/) | [Ittrative](relative_sort_array.js) | 52 ms | 34.9 MB | Easy | | | | +| 1122 | [Relative Sort Array](https://leetcode.com/problems/relative-sort-array/) | [Ittrative](relative_sort_array.js) | 52 ms | 34.9 MB | Easy | | | | +| 67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [addBinary.js](addBinary.js) | 72 ms | 35.7 MB | Easy | | | [:link:](https://www.youtube.com/watch?v=6axItxXHouA) | | 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Recursive](reverse_string.js) | 112 ms | 48.6 MB | Easy | 0(N) | 0(1) | | | 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | | 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Recursive](climbing_stairs.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(N) | [:link:](https://www.youtube.com/watch?v=NFJ3m9a1oJQ) | diff --git a/addBinary.js b/addBinary.js new file mode 100644 index 0000000..aea2716 --- /dev/null +++ b/addBinary.js @@ -0,0 +1,32 @@ +/** + * @param {string} a + * @param {string} b + * @return {string} + */ +var addBinary = function(a, b) { + let carry = 0; + let out = ""; + if (a.length !== b.length) { + if (a.length < b.length) { + a = "0".repeat(b.length - a.length) + a; + } else { + b = "0".repeat(a.length - b.length) + b; + } + } + let i = a.length - 1; + let j = b.length - 1; + + while (i >= 0 || j >= 0) { + let aval = a[i]; + let bval = b[j]; + out = String(aval ^ bval ^ String(carry)) + out; + if (aval === bval && aval !== String(carry)) { + carry = Number(!carry); + } + i--; + j--; + } + + return carry ? String(carry) + out : out; +}; +addBinary("101111", "10"); //? From 0720d925183ea40a67417aafcfd3589266f54b86 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Fri, 16 Aug 2019 14:36:08 -0700 Subject: [PATCH 77/91] 146. LRU Cache --- .idea/workspace.xml | 26 ++++++++++++------------ README.md | 1 + lru_cache.js | 48 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 lru_cache.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d40cded..db8ef4f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,8 @@ - - + + @@ -164,14 +164,7 @@ - - - - 1561335544480 - 1561351986824 @@ -509,7 +502,14 @@ - @@ -540,7 +540,6 @@ diff --git a/README.md b/README.md index 313de0b..c6413f2 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ | 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [Recursive](swap_nodes_in_pairs.js) | 68 ms | 34 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=zOovxGmION4) | | 48 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [Iterative](rotate_image.js) | 56 ms | 33.7 MB | Medium | O(N) | O(1) | [:link:](https://leetcode.com/problems/rotate-image/solution/) | | 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Itterative](valid_sudoku.js) | 68 ms | 37.7 MB | Medium | O(1) | O(1) | [:link:](https://leetcode.com/problems/valid-sudoku/solution/) | +| 146 | [ LRU Cache](https://leetcode.com/problems/lru-cache/) | [Map](lru_cache.js) | 216 ms | 58.8 MB | Medium | O(1) | O(1) | | | 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) | ## Others diff --git a/lru_cache.js b/lru_cache.js new file mode 100644 index 0000000..3332e7d --- /dev/null +++ b/lru_cache.js @@ -0,0 +1,48 @@ +// https://leetcode.com/problems/lru-cache/ + +/** + * @param {number} capacity + */ +var LRUCache = function(capacity) { + this.cache = new Map(); + this.capacity = capacity; +}; + +/** + * @param {number} key + * @return {number} + */ +LRUCache.prototype.get = function(key) { + let cache = this.cache; + let temp = cache.get(key); + if (temp) { + // to maintain order + cache.delete(key); + cache.set(key, temp); + return temp; + } else { + return -1; + } +}; + +/** + * @param {number} key + * @param {number} value + * @return {void} + */ +LRUCache.prototype.put = function(key, value) { + let cache = this.cache; + if (cache.has(key)) { + cache.delete(key); + } else if (cache.size >= this.capacity) { + cache.delete(cache.keys().next().value); + } + cache.set(key, value); +}; + +/** + * Your LRUCache object will be instantiated and called as such: + * var obj = new LRUCache(capacity) + * var param_1 = obj.get(key) + * obj.put(key,value) + */ From e76e57781c2dcefe40196d0b0a78331b09d2d352 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Tue, 20 Aug 2019 23:10:16 -0700 Subject: [PATCH 78/91] 297. Serialize and Deserialize Binary Tree --- .idea/workspace.xml | 27 ++++++------ README.md | 1 + serialize_and_deserialize_binary_tree.js | 54 ++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 serialize_and_deserialize_binary_tree.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index db8ef4f..9acaacc 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + @@ -164,14 +164,10 @@ - - - - 1561351986824 - 1561353216930 @@ -509,7 +505,14 @@ - @@ -540,7 +543,6 @@ diff --git a/README.md b/README.md index c6413f2..b71057e 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ | 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Itterative](valid_sudoku.js) | 68 ms | 37.7 MB | Medium | O(1) | O(1) | [:link:](https://leetcode.com/problems/valid-sudoku/solution/) | | 146 | [ LRU Cache](https://leetcode.com/problems/lru-cache/) | [Map](lru_cache.js) | 216 ms | 58.8 MB | Medium | O(1) | O(1) | | | 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) | +| 297 | [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [DFS Recursion](serialize_and_deserialize_binary_tree.js) | 80 ms | 43.6 MB | Hard | | | [:link:](https://www.youtube.com/watch?v=suj1ro8TIVY) | ## Others diff --git a/serialize_and_deserialize_binary_tree.js b/serialize_and_deserialize_binary_tree.js new file mode 100644 index 0000000..465bf6f --- /dev/null +++ b/serialize_and_deserialize_binary_tree.js @@ -0,0 +1,54 @@ +// https://www.youtube.com/watch?v=YGZuDSs8fQo + +function TreeNode(val) { + this.val = val; + this.left = this.right = null; +} + +/** + * Encodes a tree to a single string. + * + * @param {TreeNode} root + * @return {string} + */ +var serialize = function(root) { + if (root === null) { + return "null,"; + } + let leftString = serialize(root.left); + let rightString = serialize(root.right); + return root.val + "," + leftString + rightString; + serialize(root); +}; + +/** + * Decodes your encoded data to tree. + * + * @param {string} data + * @return {TreeNode} + */ +var deserialize = function(data) { + const list = data.split(","); + let recur = function(list) { + let val = list.shift(); + if (val === "null") return null; + let newNode = new TreeNode(parseInt(val)); + newNode.left = recur(list); + newNode.right = recur(list); + return newNode; + }; + return recur(list); +}; + +/** + * Your functions will be called as such: + * deserialize(serialize(root)); + */ + +let root = new TreeNode(1); +root.left = new TreeNode(2); +root.right = new TreeNode(3); +root.right.left = new TreeNode(4); +root.right.right = new TreeNode(5); + +deserialize(serialize(root)); //? From 1418465c8975c57f16c48a2c28137f840a3da35a Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Thu, 22 Aug 2019 22:42:14 -0700 Subject: [PATCH 79/91] 443. String Compression --- .idea/workspace.xml | 28 +++++++++++++++------------- README.md | 1 + string_compression.js | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 string_compression.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9acaacc..0b7f1b7 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,8 @@ - - + + @@ -167,14 +167,9 @@ - - - - 1561353216930 - 1561443024643 @@ -512,7 +507,14 @@ - @@ -543,7 +545,6 @@ diff --git a/README.md b/README.md index b71057e..c329ead 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ | # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | |:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:------------------------------------------------------------------| | 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | +| 443 | [String Compression](https://leetcode.com/problems/string-compression/) | [string_compression.js](string_compression.js) | | | Easy | | | | | 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [iterative](best_time_to_buy_and_sell_stock_II.js) | 56 ms | 35.4 MB | Easy | O(N) | O(1) | | | 1122 | [Relative Sort Array](https://leetcode.com/problems/relative-sort-array/) | [Ittrative](relative_sort_array.js) | 52 ms | 34.9 MB | Easy | | | | | 67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [addBinary.js](addBinary.js) | 72 ms | 35.7 MB | Easy | | | [:link:](https://www.youtube.com/watch?v=6axItxXHouA) | diff --git a/string_compression.js b/string_compression.js new file mode 100644 index 0000000..6a35cea --- /dev/null +++ b/string_compression.js @@ -0,0 +1,23 @@ +/** + * @param {character[]} chars + * @return {number} + */ +var compress = function(chars) { + chars.push("end"); + let count = 1; + for (var i = 0; i < chars.indexOf("end"); i++) { + if (chars[i] === chars[i + 1]) { + count++; + continue; + } + + chars.push(chars[i]); + if (count !== 1) { + chars.push(...count.toString().split("")); + } + + count = 1; + } + + chars.splice(0, chars.indexOf("end") + 1); +}; From 1e62d120b12c88e0343f925417285f26b611b595 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Thu, 22 Aug 2019 22:46:00 -0700 Subject: [PATCH 80/91] Code imporved --- .idea/workspace.xml | 27 +++++++++++++-------------- string_compression.js | 27 +++++++++++++-------------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0b7f1b7..df57a6b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,10 +1,9 @@ - - + - + @@ -545,7 +544,6 @@ diff --git a/string_compression.js b/string_compression.js index 6a35cea..c3da2a8 100644 --- a/string_compression.js +++ b/string_compression.js @@ -3,21 +3,20 @@ * @return {number} */ var compress = function(chars) { - chars.push("end"); - let count = 1; - for (var i = 0; i < chars.indexOf("end"); i++) { - if (chars[i] === chars[i + 1]) { - count++; - continue; - } + let index = 0, + count = 1; - chars.push(chars[i]); - if (count !== 1) { - chars.push(...count.toString().split("")); + for (let i = 0; i < chars.length; i++) { + if (chars[i] !== chars[i + 1]) { + const splitCount = count > 1 ? String(count).split("") : []; + chars.splice(index, count, chars[i], ...splitCount); + index = index + splitCount.length + 1; + count = 1; + i = index - 1; + } else { + count++; } - - count = 1; } - - chars.splice(0, chars.indexOf("end") + 1); }; + +compress(["a", "b", "c"]); From 984e518c40567daf6c647333767769d24021d3f4 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sun, 25 Aug 2019 15:12:11 -0700 Subject: [PATCH 81/91] 1. Two Sum --- .idea/workspace.xml | 28 +++++++++++++++------------- README.md | 1 + two_sum.js | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 two_sum.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index df57a6b..311475d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,9 +1,10 @@ - + + - + @@ -544,7 +546,6 @@ diff --git a/README.md b/README.md index c329ead..091b676 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ | # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | |:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:------------------------------------------------------------------| | 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | +| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [two_sum.js](two_sum.js) | 52 ms | 35 MB | Easy | | | | | 443 | [String Compression](https://leetcode.com/problems/string-compression/) | [string_compression.js](string_compression.js) | | | Easy | | | | | 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [iterative](best_time_to_buy_and_sell_stock_II.js) | 56 ms | 35.4 MB | Easy | O(N) | O(1) | | | 1122 | [Relative Sort Array](https://leetcode.com/problems/relative-sort-array/) | [Ittrative](relative_sort_array.js) | 52 ms | 34.9 MB | Easy | | | | diff --git a/two_sum.js b/two_sum.js new file mode 100644 index 0000000..4b929fe --- /dev/null +++ b/two_sum.js @@ -0,0 +1,18 @@ +/** + * @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 targetSum = target - nums[i]; //? + if (map.has(targetSum)) { + return [i, map.get(targetSum)]; + } + map.set(nums[i], i); + } +}; + +twoSum([2, 7, 11, 15], 9); //? From 5256949f25d4bfa7d4dddd97570e1d80fcabdbd6 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sun, 25 Aug 2019 22:00:58 -0700 Subject: [PATCH 82/91] 238. Product of Array Except Self --- .idea/workspace.xml | 24 ++-- README.md | 187 ++++++++++++++++---------------- product_of_array_except_self.js | 24 ++++ 3 files changed, 130 insertions(+), 105 deletions(-) create mode 100644 product_of_array_except_self.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 311475d..3ad8934 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + @@ -170,14 +170,7 @@ - - - - 1561516057413 - 1561523843517 @@ -515,7 +508,14 @@ - @@ -546,7 +546,6 @@ diff --git a/README.md b/README.md index 091b676..e033b37 100644 --- a/README.md +++ b/README.md @@ -5,99 +5,100 @@ #### This contain solution of leetcode in javascript -| # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | -|:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:------------------------------------------------------------------| -| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | -| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [two_sum.js](two_sum.js) | 52 ms | 35 MB | Easy | | | | -| 443 | [String Compression](https://leetcode.com/problems/string-compression/) | [string_compression.js](string_compression.js) | | | Easy | | | | -| 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [iterative](best_time_to_buy_and_sell_stock_II.js) | 56 ms | 35.4 MB | Easy | O(N) | O(1) | | -| 1122 | [Relative Sort Array](https://leetcode.com/problems/relative-sort-array/) | [Ittrative](relative_sort_array.js) | 52 ms | 34.9 MB | Easy | | | | -| 67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [addBinary.js](addBinary.js) | 72 ms | 35.7 MB | Easy | | | [:link:](https://www.youtube.com/watch?v=6axItxXHouA) | -| 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Recursive](reverse_string.js) | 112 ms | 48.6 MB | Easy | 0(N) | 0(1) | | -| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | -| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Recursive](climbing_stairs.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(N) | [:link:](https://www.youtube.com/watch?v=NFJ3m9a1oJQ) | -| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | | -| 941 | [Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/) | [Ittrative](valid_mountain_array.js) | 56 ms | 36.8 MB | Easy | 0(N) | O(1) | [:link:](https://www.youtube.com/watch?v=WWysBX-N2Ak) | -| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | | -| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [most_common_word.js](most_common_word.js) | 76 ms | 37.8 MB | Easy | | | | -| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Recursion](maximum_depth_of_binary_tree.js) | 72 ms | 36.9 MB | Easy | O(N) | 0(1) | [:link:](http://bit.ly/2XQnNJI) | -| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | | | | -| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | | | | -| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | | | | -| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](find_common_characters.js) | 108 ms | 38.1 MB | Easy | | | | -| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution 1](middle_of_the_linked_list.js)
[Solution 2](middle_of_the_linked_list_2.js) | 48 ms
44ms | 33.9 MB
33.8 MB | Easy | | | | -| 126 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](single_number.js) | 60 ms | 37.1 MB | Easy | | | | -| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution 1](max_consecutive_ones.js)
[Solution 2](max_consecutive_ones_2.js) | 56 ms | 37.1 MB | Easy | | | | -| 258 | [ Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](add_digits.js) | 76 ms | 36.3 MB | Easy | | | | -| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](delete_node_linked_list.js) | 60 ms | 35.6 MB | Easy | | | | -| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](valid_anagram.js) | 56 ms | 37.8 MB | Easy | | | | -| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](find_all_numbers_disappeared_in_an_array.js) | 6576 ms | 45.7 MB | Easy | | | | -| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](contains_duplicate.js) | 96 ms | 42.7 MB | Easy | | | | -| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](contains_duplicate_II.js) | 72 ms | 42.4 MB | Easy | | | | -| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution 1](intersection_of_two_arrays.js)
[Solution 2](intersection_of_two_arrays_solution_2.js) | 56 ms | 35.8 MB | Easy | | | | -| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](intersection_of_two_arrays_II.js)
[Solution 2](intersection_of_two_arrays_II_solution2.js) | 84 ms | 38.3 MB | Easy | | | | -| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](squares_of_a_sorted_array.js) | 152 ms | 43.5 MB | Easy | | | | -| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](minimum_index_sum_of_two_lists.js) | 108 ms | 43 MB | Easy | | | | -| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](remove_duplicates_from_sorted_array.js) | 76 ms | 37.2 MB | Easy | | | | -| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](same_tree.js) | 52 ms | 33.9 MB | Easy | | | | -| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Iterative](binary_search.js)
[Recursion](binary_search_recursion.js) | | | Easy | | | | -| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](search_in_a_binary_search_tree.js) | 100ms | 41.7 | Easy | | | | -| 821 | [ Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](shortest_distance_to_a_character.js) | 192 ms | 45.5 MB | Easy | | | | -| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](array_partition_I.js) | 116 ms | 39.1MB | Easy | | | | -| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](number_complement.js) | 52ms | 33.9 MB | Easy | | | | -| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](merge_two_binary_trees.js) | 92 ms | 40.4 MB | Easy | | | | -| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](missing_number.js) | 388 ms | 37.1 MB | Easy | | | | -| 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | | | | -| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | | | -| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | | | -| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | | | -| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | | | -| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | | | | -| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | | | | -| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | | | -| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | | | -| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | | | | -| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [n-ary_tree_level_order_traversal.js](n-ary_tree_level_order_traversal.js) | 644 ms | 80.9 MB | Easy | | | | -| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | | | -| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [duplicate_zeros.js](duplicate_zeros.js) | 64 ms | 35.7 MB | Easy | | | | -| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy | | | | -| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [rotate_array.js](rotate_array.js) | 92 ms | 35.3 MB | Easy | | | | -| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | | | -| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [first_unique_character_in_a_string.js](first_unique_character_in_a_string.js) | 124 ms | 39.3 MB | Easy | | | | -| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [add_string.js](add_string.js) | 60 ms | 36.5 MB | Easy | 0(N) | O(1) | | -| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [two_sum_II.js](two_sum_II.js) | 64 ms | 35.5 MB | Easy | | | | -| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [valid_parentheses.js](valid_parentheses.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(1) | | -| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [best_time_to_buy_and_sell_stock.js](best_time_to_buy_and_sell_stock.js) | 56 ms | 36.5 MB | Easy | 0(N) | O(1) | | -| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [valid_palindrome_II.js](valid_palindrome_II.js) | 72 ms | 43.2 MB | Easy | O(N) | O(1) | | -| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [third_maximum_number.js](third_maximum_number.js) | 76 ms | 38 MB | Easy | | | | -| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | | | -| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | | | -| 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | | | | -| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | | | -| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | | | | -| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | | | -| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | | | -| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | | | | -| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [combination_sum_II.js](combination_sum_II.js) | 84 ms | 37.6 MB | Medium | | | | -| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [permutations.js](permutations.js) | 88 ms | 37.6 MB | Medium | | | | -| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [remove_duplicates_from_sorted_array_II.js](remove_duplicates_from_sorted_array_II.js) | 64 ms | 35.8 MB | Medium | | | | -| 78 | [Subsets](https://leetcode.com/problems/subsets/) | [subset.js](subset.js) | 60 ms | 35 MB | Medium | | | | -| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [reverse_words_in_string.js](reverse_words_in_string.js) | 56 ms | 34.6 MB | Medium | | | | -| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [palindromic_substrings.js](palindromic_substrings.js) | 704 ms | 37.2 MB | Medium | | | | -| 102. | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [binary_tree_level_order_traversal.js](binary_tree_level_order_traversal.js) | 60 ms | 34.9 MB | Medium | O(N) | | | -| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | | | -| 287 | [ Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [find_the_duplicate_number.js](find_the_duplicate_number.js) | 84 ms | 36 MB | Medium | O(N) | O(1) | | -| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [minimum_path_sum.js](minimum_path_sum.js) | 52 ms | 35.5 MB | Medium | o(N2) | O(1) | [:link:](https://www.youtube.com/watch?v=lBRtnuxg-gU) | -| 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [group_anagrams.js](group_anagrams.js) | 152 ms | 47.3 MB | Medium | NlogN | | [:link:](https://leetcode.com/problems/group-anagrams/solution/ ) | -| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Sliding Window Approach](longest_substring.js) | 84 ms | 38.4 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs) | -| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Iterative](single_number_II.js) | 64 ms | 37.6 MB | Medium | NlogN | | [:link:](https://www.youtube.com/watch?v=KXjgQWDvQ24) | -| 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [Ratcheting](container_with_most_water.js) | 52 ms | 35.4 MB | Medium | O(N) | O(1) | [:link:](https://www.youtube.com/watch?v=k5fbSqb9sCI&t=138s) | -| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [Recursive](swap_nodes_in_pairs.js) | 68 ms | 34 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=zOovxGmION4) | -| 48 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [Iterative](rotate_image.js) | 56 ms | 33.7 MB | Medium | O(N) | O(1) | [:link:](https://leetcode.com/problems/rotate-image/solution/) | -| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Itterative](valid_sudoku.js) | 68 ms | 37.7 MB | Medium | O(1) | O(1) | [:link:](https://leetcode.com/problems/valid-sudoku/solution/) | -| 146 | [ LRU Cache](https://leetcode.com/problems/lru-cache/) | [Map](lru_cache.js) | 216 ms | 58.8 MB | Medium | O(1) | O(1) | | -| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) | -| 297 | [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [DFS Recursion](serialize_and_deserialize_binary_tree.js) | 80 ms | 43.6 MB | Hard | | | [:link:](https://www.youtube.com/watch?v=suj1ro8TIVY) | +| # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | +|:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:------------------------------------------------------------------------------| +| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | +| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [two_sum.js](two_sum.js) | 52 ms | 35 MB | Easy | | | | +| 443 | [String Compression](https://leetcode.com/problems/string-compression/) | [string_compression.js](string_compression.js) | | | Easy | | | | +| 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [iterative](best_time_to_buy_and_sell_stock_II.js) | 56 ms | 35.4 MB | Easy | O(N) | O(1) | | +| 1122 | [Relative Sort Array](https://leetcode.com/problems/relative-sort-array/) | [Ittrative](relative_sort_array.js) | 52 ms | 34.9 MB | Easy | | | | +| 67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [addBinary.js](addBinary.js) | 72 ms | 35.7 MB | Easy | | | [:link:](https://www.youtube.com/watch?v=6axItxXHouA) | +| 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Recursive](reverse_string.js) | 112 ms | 48.6 MB | Easy | 0(N) | 0(1) | | +| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | +| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Recursive](climbing_stairs.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(N) | [:link:](https://www.youtube.com/watch?v=NFJ3m9a1oJQ) | +| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | | +| 941 | [Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/) | [Ittrative](valid_mountain_array.js) | 56 ms | 36.8 MB | Easy | 0(N) | O(1) | [:link:](https://www.youtube.com/watch?v=WWysBX-N2Ak) | +| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | | +| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [most_common_word.js](most_common_word.js) | 76 ms | 37.8 MB | Easy | | | | +| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Recursion](maximum_depth_of_binary_tree.js) | 72 ms | 36.9 MB | Easy | O(N) | 0(1) | [:link:](http://bit.ly/2XQnNJI) | +| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | | | | +| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | | | | +| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | | | | +| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](find_common_characters.js) | 108 ms | 38.1 MB | Easy | | | | +| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution 1](middle_of_the_linked_list.js)
[Solution 2](middle_of_the_linked_list_2.js) | 48 ms
44ms | 33.9 MB
33.8 MB | Easy | | | | +| 126 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](single_number.js) | 60 ms | 37.1 MB | Easy | | | | +| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution 1](max_consecutive_ones.js)
[Solution 2](max_consecutive_ones_2.js) | 56 ms | 37.1 MB | Easy | | | | +| 258 | [ Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](add_digits.js) | 76 ms | 36.3 MB | Easy | | | | +| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](delete_node_linked_list.js) | 60 ms | 35.6 MB | Easy | | | | +| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](valid_anagram.js) | 56 ms | 37.8 MB | Easy | | | | +| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](find_all_numbers_disappeared_in_an_array.js) | 6576 ms | 45.7 MB | Easy | | | | +| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](contains_duplicate.js) | 96 ms | 42.7 MB | Easy | | | | +| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](contains_duplicate_II.js) | 72 ms | 42.4 MB | Easy | | | | +| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution 1](intersection_of_two_arrays.js)
[Solution 2](intersection_of_two_arrays_solution_2.js) | 56 ms | 35.8 MB | Easy | | | | +| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](intersection_of_two_arrays_II.js)
[Solution 2](intersection_of_two_arrays_II_solution2.js) | 84 ms | 38.3 MB | Easy | | | | +| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](squares_of_a_sorted_array.js) | 152 ms | 43.5 MB | Easy | | | | +| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](minimum_index_sum_of_two_lists.js) | 108 ms | 43 MB | Easy | | | | +| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](remove_duplicates_from_sorted_array.js) | 76 ms | 37.2 MB | Easy | | | | +| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](same_tree.js) | 52 ms | 33.9 MB | Easy | | | | +| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Iterative](binary_search.js)
[Recursion](binary_search_recursion.js) | | | Easy | | | | +| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](search_in_a_binary_search_tree.js) | 100ms | 41.7 | Easy | | | | +| 821 | [ Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](shortest_distance_to_a_character.js) | 192 ms | 45.5 MB | Easy | | | | +| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](array_partition_I.js) | 116 ms | 39.1MB | Easy | | | | +| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](number_complement.js) | 52ms | 33.9 MB | Easy | | | | +| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](merge_two_binary_trees.js) | 92 ms | 40.4 MB | Easy | | | | +| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](missing_number.js) | 388 ms | 37.1 MB | Easy | | | | +| 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | | | | +| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | | | +| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | | | +| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | | | +| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | | | +| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | | | | +| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | | | | +| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | | | +| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | | | +| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | | | | +| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [n-ary_tree_level_order_traversal.js](n-ary_tree_level_order_traversal.js) | 644 ms | 80.9 MB | Easy | | | | +| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | | | +| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [duplicate_zeros.js](duplicate_zeros.js) | 64 ms | 35.7 MB | Easy | | | | +| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy | | | | +| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [rotate_array.js](rotate_array.js) | 92 ms | 35.3 MB | Easy | | | | +| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | | | +| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [first_unique_character_in_a_string.js](first_unique_character_in_a_string.js) | 124 ms | 39.3 MB | Easy | | | | +| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [add_string.js](add_string.js) | 60 ms | 36.5 MB | Easy | 0(N) | O(1) | | +| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [two_sum_II.js](two_sum_II.js) | 64 ms | 35.5 MB | Easy | | | | +| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [valid_parentheses.js](valid_parentheses.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(1) | | +| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [best_time_to_buy_and_sell_stock.js](best_time_to_buy_and_sell_stock.js) | 56 ms | 36.5 MB | Easy | 0(N) | O(1) | | +| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [valid_palindrome_II.js](valid_palindrome_II.js) | 72 ms | 43.2 MB | Easy | O(N) | O(1) | | +| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [third_maximum_number.js](third_maximum_number.js) | 76 ms | 38 MB | Easy | | | | +| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | | | +| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | | | +| 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | | | | +| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | | | +| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | | | | +| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | | | +| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | | | +| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | | | | +| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [combination_sum_II.js](combination_sum_II.js) | 84 ms | 37.6 MB | Medium | | | | +| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [permutations.js](permutations.js) | 88 ms | 37.6 MB | Medium | | | | +| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [remove_duplicates_from_sorted_array_II.js](remove_duplicates_from_sorted_array_II.js) | 64 ms | 35.8 MB | Medium | | | | +| 78 | [Subsets](https://leetcode.com/problems/subsets/) | [subset.js](subset.js) | 60 ms | 35 MB | Medium | | | | +| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [reverse_words_in_string.js](reverse_words_in_string.js) | 56 ms | 34.6 MB | Medium | | | | +| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [palindromic_substrings.js](palindromic_substrings.js) | 704 ms | 37.2 MB | Medium | | | | +| 102. | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [binary_tree_level_order_traversal.js](binary_tree_level_order_traversal.js) | 60 ms | 34.9 MB | Medium | O(N) | | | +| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | | | +| 287 | [ Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [find_the_duplicate_number.js](find_the_duplicate_number.js) | 84 ms | 36 MB | Medium | O(N) | O(1) | | +| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [minimum_path_sum.js](minimum_path_sum.js) | 52 ms | 35.5 MB | Medium | o(N2) | O(1) | [:link:](https://www.youtube.com/watch?v=lBRtnuxg-gU) | +| 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [group_anagrams.js](group_anagrams.js) | 152 ms | 47.3 MB | Medium | NlogN | | [:link:](https://leetcode.com/problems/group-anagrams/solution/ ) | +| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Sliding Window Approach](longest_substring.js) | 84 ms | 38.4 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs) | +| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [product_of_array_except_self.js](product_of_array_except_self.js) | 76 ms | 42.3 MB | Medium | O(N) | O(1) | [:link:](https://leetcode.com/problems/product-of-array-except-self/solution/) | +| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Iterative](single_number_II.js) | 64 ms | 37.6 MB | Medium | NlogN | | [:link:](https://www.youtube.com/watch?v=KXjgQWDvQ24) | +| 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [Ratcheting](container_with_most_water.js) | 52 ms | 35.4 MB | Medium | O(N) | O(1) | [:link:](https://www.youtube.com/watch?v=k5fbSqb9sCI&t=138s) | +| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [Recursive](swap_nodes_in_pairs.js) | 68 ms | 34 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=zOovxGmION4) | +| 48 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [Iterative](rotate_image.js) | 56 ms | 33.7 MB | Medium | O(N) | O(1) | [:link:](https://leetcode.com/problems/rotate-image/solution/) | +| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Itterative](valid_sudoku.js) | 68 ms | 37.7 MB | Medium | O(1) | O(1) | [:link:](https://leetcode.com/problems/valid-sudoku/solution/) | +| 146 | [ LRU Cache](https://leetcode.com/problems/lru-cache/) | [Map](lru_cache.js) | 216 ms | 58.8 MB | Medium | O(1) | O(1) | | +| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) | +| 297 | [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [DFS Recursion](serialize_and_deserialize_binary_tree.js) | 80 ms | 43.6 MB | Hard | | | [:link:](https://www.youtube.com/watch?v=suj1ro8TIVY) | ## Others diff --git a/product_of_array_except_self.js b/product_of_array_except_self.js new file mode 100644 index 0000000..681d551 --- /dev/null +++ b/product_of_array_except_self.js @@ -0,0 +1,24 @@ +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function(nums) { + let anwser = []; + + anwser[0] = 1; + + for (let i = 1; i < nums.length; i++) { + anwser[i] = nums[i - 1] * anwser[i - 1]; + } + + let R = 1; + + for (let i = anwser.length - 1; i >= 0; i--) { + anwser[i] = anwser[i] * R; + R *= nums[i]; + } + + return anwser; +}; + +productExceptSelf([1, 2, 3, 4]); //? From c861441b99efc98ebce965cccd6a17a85b329b78 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Tue, 3 Sep 2019 22:34:09 -0700 Subject: [PATCH 83/91] 152. Maximum Product Subarray --- .idea/workspace.xml | 28 ++++--- README.md | 187 ++++++++++++++++++++++---------------------- max_product.js | 28 +++++++ 3 files changed, 138 insertions(+), 105 deletions(-) create mode 100644 max_product.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3ad8934..df02fcb 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + @@ -170,14 +170,11 @@ - - - - 1561523843517 - 1561607716498 @@ -515,7 +512,14 @@ - @@ -546,7 +550,6 @@ diff --git a/README.md b/README.md index e033b37..e387cb0 100644 --- a/README.md +++ b/README.md @@ -5,100 +5,101 @@ #### This contain solution of leetcode in javascript -| # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | -|:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:------------------------------------------------------------------------------| -| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | -| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [two_sum.js](two_sum.js) | 52 ms | 35 MB | Easy | | | | -| 443 | [String Compression](https://leetcode.com/problems/string-compression/) | [string_compression.js](string_compression.js) | | | Easy | | | | -| 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [iterative](best_time_to_buy_and_sell_stock_II.js) | 56 ms | 35.4 MB | Easy | O(N) | O(1) | | -| 1122 | [Relative Sort Array](https://leetcode.com/problems/relative-sort-array/) | [Ittrative](relative_sort_array.js) | 52 ms | 34.9 MB | Easy | | | | -| 67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [addBinary.js](addBinary.js) | 72 ms | 35.7 MB | Easy | | | [:link:](https://www.youtube.com/watch?v=6axItxXHouA) | -| 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Recursive](reverse_string.js) | 112 ms | 48.6 MB | Easy | 0(N) | 0(1) | | -| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | -| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Recursive](climbing_stairs.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(N) | [:link:](https://www.youtube.com/watch?v=NFJ3m9a1oJQ) | -| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | | -| 941 | [Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/) | [Ittrative](valid_mountain_array.js) | 56 ms | 36.8 MB | Easy | 0(N) | O(1) | [:link:](https://www.youtube.com/watch?v=WWysBX-N2Ak) | -| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | | -| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [most_common_word.js](most_common_word.js) | 76 ms | 37.8 MB | Easy | | | | -| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Recursion](maximum_depth_of_binary_tree.js) | 72 ms | 36.9 MB | Easy | O(N) | 0(1) | [:link:](http://bit.ly/2XQnNJI) | -| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | | | | -| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | | | | -| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | | | | -| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](find_common_characters.js) | 108 ms | 38.1 MB | Easy | | | | -| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution 1](middle_of_the_linked_list.js)
[Solution 2](middle_of_the_linked_list_2.js) | 48 ms
44ms | 33.9 MB
33.8 MB | Easy | | | | -| 126 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](single_number.js) | 60 ms | 37.1 MB | Easy | | | | -| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution 1](max_consecutive_ones.js)
[Solution 2](max_consecutive_ones_2.js) | 56 ms | 37.1 MB | Easy | | | | -| 258 | [ Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](add_digits.js) | 76 ms | 36.3 MB | Easy | | | | -| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](delete_node_linked_list.js) | 60 ms | 35.6 MB | Easy | | | | -| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](valid_anagram.js) | 56 ms | 37.8 MB | Easy | | | | -| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](find_all_numbers_disappeared_in_an_array.js) | 6576 ms | 45.7 MB | Easy | | | | -| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](contains_duplicate.js) | 96 ms | 42.7 MB | Easy | | | | -| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](contains_duplicate_II.js) | 72 ms | 42.4 MB | Easy | | | | -| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution 1](intersection_of_two_arrays.js)
[Solution 2](intersection_of_two_arrays_solution_2.js) | 56 ms | 35.8 MB | Easy | | | | -| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](intersection_of_two_arrays_II.js)
[Solution 2](intersection_of_two_arrays_II_solution2.js) | 84 ms | 38.3 MB | Easy | | | | -| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](squares_of_a_sorted_array.js) | 152 ms | 43.5 MB | Easy | | | | -| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](minimum_index_sum_of_two_lists.js) | 108 ms | 43 MB | Easy | | | | -| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](remove_duplicates_from_sorted_array.js) | 76 ms | 37.2 MB | Easy | | | | -| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](same_tree.js) | 52 ms | 33.9 MB | Easy | | | | -| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Iterative](binary_search.js)
[Recursion](binary_search_recursion.js) | | | Easy | | | | -| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](search_in_a_binary_search_tree.js) | 100ms | 41.7 | Easy | | | | -| 821 | [ Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](shortest_distance_to_a_character.js) | 192 ms | 45.5 MB | Easy | | | | -| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](array_partition_I.js) | 116 ms | 39.1MB | Easy | | | | -| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](number_complement.js) | 52ms | 33.9 MB | Easy | | | | -| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](merge_two_binary_trees.js) | 92 ms | 40.4 MB | Easy | | | | -| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](missing_number.js) | 388 ms | 37.1 MB | Easy | | | | -| 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | | | | -| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | | | -| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | | | -| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | | | -| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | | | -| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | | | | -| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | | | | -| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | | | -| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | | | -| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | | | | -| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [n-ary_tree_level_order_traversal.js](n-ary_tree_level_order_traversal.js) | 644 ms | 80.9 MB | Easy | | | | -| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | | | -| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [duplicate_zeros.js](duplicate_zeros.js) | 64 ms | 35.7 MB | Easy | | | | -| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy | | | | -| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [rotate_array.js](rotate_array.js) | 92 ms | 35.3 MB | Easy | | | | -| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | | | -| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [first_unique_character_in_a_string.js](first_unique_character_in_a_string.js) | 124 ms | 39.3 MB | Easy | | | | -| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [add_string.js](add_string.js) | 60 ms | 36.5 MB | Easy | 0(N) | O(1) | | -| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [two_sum_II.js](two_sum_II.js) | 64 ms | 35.5 MB | Easy | | | | -| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [valid_parentheses.js](valid_parentheses.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(1) | | -| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [best_time_to_buy_and_sell_stock.js](best_time_to_buy_and_sell_stock.js) | 56 ms | 36.5 MB | Easy | 0(N) | O(1) | | -| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [valid_palindrome_II.js](valid_palindrome_II.js) | 72 ms | 43.2 MB | Easy | O(N) | O(1) | | -| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [third_maximum_number.js](third_maximum_number.js) | 76 ms | 38 MB | Easy | | | | -| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | | | -| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | | | -| 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | | | | -| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | | | -| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | | | | -| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | | | -| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | | | -| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | | | | -| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [combination_sum_II.js](combination_sum_II.js) | 84 ms | 37.6 MB | Medium | | | | -| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [permutations.js](permutations.js) | 88 ms | 37.6 MB | Medium | | | | -| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [remove_duplicates_from_sorted_array_II.js](remove_duplicates_from_sorted_array_II.js) | 64 ms | 35.8 MB | Medium | | | | -| 78 | [Subsets](https://leetcode.com/problems/subsets/) | [subset.js](subset.js) | 60 ms | 35 MB | Medium | | | | -| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [reverse_words_in_string.js](reverse_words_in_string.js) | 56 ms | 34.6 MB | Medium | | | | -| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [palindromic_substrings.js](palindromic_substrings.js) | 704 ms | 37.2 MB | Medium | | | | -| 102. | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [binary_tree_level_order_traversal.js](binary_tree_level_order_traversal.js) | 60 ms | 34.9 MB | Medium | O(N) | | | -| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | | | -| 287 | [ Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [find_the_duplicate_number.js](find_the_duplicate_number.js) | 84 ms | 36 MB | Medium | O(N) | O(1) | | -| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [minimum_path_sum.js](minimum_path_sum.js) | 52 ms | 35.5 MB | Medium | o(N2) | O(1) | [:link:](https://www.youtube.com/watch?v=lBRtnuxg-gU) | -| 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [group_anagrams.js](group_anagrams.js) | 152 ms | 47.3 MB | Medium | NlogN | | [:link:](https://leetcode.com/problems/group-anagrams/solution/ ) | -| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Sliding Window Approach](longest_substring.js) | 84 ms | 38.4 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs) | +| # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation | +|:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:-------------------------------------------------------------------------------| +| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | +| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [two_sum.js](two_sum.js) | 52 ms | 35 MB | Easy | | | | +| 443 | [String Compression](https://leetcode.com/problems/string-compression/) | [string_compression.js](string_compression.js) | | | Easy | | | | +| 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [iterative](best_time_to_buy_and_sell_stock_II.js) | 56 ms | 35.4 MB | Easy | O(N) | O(1) | | +| 1122 | [Relative Sort Array](https://leetcode.com/problems/relative-sort-array/) | [Ittrative](relative_sort_array.js) | 52 ms | 34.9 MB | Easy | | | | +| 67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [addBinary.js](addBinary.js) | 72 ms | 35.7 MB | Easy | | | [:link:](https://www.youtube.com/watch?v=6axItxXHouA) | +| 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Recursive](reverse_string.js) | 112 ms | 48.6 MB | Easy | 0(N) | 0(1) | | +| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | | +| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Recursive](climbing_stairs.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(N) | [:link:](https://www.youtube.com/watch?v=NFJ3m9a1oJQ) | +| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | | +| 941 | [Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/) | [Ittrative](valid_mountain_array.js) | 56 ms | 36.8 MB | Easy | 0(N) | O(1) | [:link:](https://www.youtube.com/watch?v=WWysBX-N2Ak) | +| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | | +| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [most_common_word.js](most_common_word.js) | 76 ms | 37.8 MB | Easy | | | | +| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Recursion](maximum_depth_of_binary_tree.js) | 72 ms | 36.9 MB | Easy | O(N) | 0(1) | [:link:](http://bit.ly/2XQnNJI) | +| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | | | | +| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)
[Recursive](reverse_linked_list_recursive.js) | 64 ms
64 ms | 35 MB
35.4 MB | Easy | | | | +| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | | | | +| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](find_common_characters.js) | 108 ms | 38.1 MB | Easy | | | | +| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution 1](middle_of_the_linked_list.js)
[Solution 2](middle_of_the_linked_list_2.js) | 48 ms
44ms | 33.9 MB
33.8 MB | Easy | | | | +| 126 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](single_number.js) | 60 ms | 37.1 MB | Easy | | | | +| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution 1](max_consecutive_ones.js)
[Solution 2](max_consecutive_ones_2.js) | 56 ms | 37.1 MB | Easy | | | | +| 258 | [ Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](add_digits.js) | 76 ms | 36.3 MB | Easy | | | | +| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](delete_node_linked_list.js) | 60 ms | 35.6 MB | Easy | | | | +| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](valid_anagram.js) | 56 ms | 37.8 MB | Easy | | | | +| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](find_all_numbers_disappeared_in_an_array.js) | 6576 ms | 45.7 MB | Easy | | | | +| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](contains_duplicate.js) | 96 ms | 42.7 MB | Easy | | | | +| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](contains_duplicate_II.js) | 72 ms | 42.4 MB | Easy | | | | +| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution 1](intersection_of_two_arrays.js)
[Solution 2](intersection_of_two_arrays_solution_2.js) | 56 ms | 35.8 MB | Easy | | | | +| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](intersection_of_two_arrays_II.js)
[Solution 2](intersection_of_two_arrays_II_solution2.js) | 84 ms | 38.3 MB | Easy | | | | +| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](squares_of_a_sorted_array.js) | 152 ms | 43.5 MB | Easy | | | | +| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](minimum_index_sum_of_two_lists.js) | 108 ms | 43 MB | Easy | | | | +| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](remove_duplicates_from_sorted_array.js) | 76 ms | 37.2 MB | Easy | | | | +| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](same_tree.js) | 52 ms | 33.9 MB | Easy | | | | +| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Iterative](binary_search.js)
[Recursion](binary_search_recursion.js) | | | Easy | | | | +| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](search_in_a_binary_search_tree.js) | 100ms | 41.7 | Easy | | | | +| 821 | [ Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](shortest_distance_to_a_character.js) | 192 ms | 45.5 MB | Easy | | | | +| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](array_partition_I.js) | 116 ms | 39.1MB | Easy | | | | +| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](number_complement.js) | 52ms | 33.9 MB | Easy | | | | +| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](merge_two_binary_trees.js) | 92 ms | 40.4 MB | Easy | | | | +| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](missing_number.js) | 388 ms | 37.1 MB | Easy | | | | +| 1047 | [ Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](remove_all_adjacent_duplicates_in_string.js) | 480 ms | 39.7 MB | Easy | | | | +| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](valid_palindrome.js) | 76 ms | 41 MB | Easy | | | | +| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](monotonic_array.js) | 68 ms | 40.5 MB | Easy | | | | +| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](two_sum_IV.js) | 84 ms | 41.5 MB | Easy | | | | +| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](preorder_traversal.js) | 608 ms | 77.1 MB | Easy | | | | +| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [univalued_binary_tree.js](univalued_binary_tree.js) | 52 ms | 34 MB | Easy | | | | +| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [search_insert_position.js](search_insert_position.js) | 52 ms | 34.5 MB | Easy | | | | +| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [sort_array_by_parity_II.js](sort_array_by_parity_II.js) | 100 ms | 41.9 MB | Easy | | | | +| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [binary_tree_tilt.js](binary_tree_tilt.js) | 68 ms | 37.7 MB | Easy | | | | +| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [repeated_string_match.js](repeated_string_match.js) | 64 ms | 35.9 MB | Easy | | | | +| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [n-ary_tree_level_order_traversal.js](n-ary_tree_level_order_traversal.js) | 644 ms | 80.9 MB | Easy | | | | +| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy | | | | +| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [duplicate_zeros.js](duplicate_zeros.js) | 64 ms | 35.7 MB | Easy | | | | +| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy | | | | +| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [rotate_array.js](rotate_array.js) | 92 ms | 35.3 MB | Easy | | | | +| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy | | | | +| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [first_unique_character_in_a_string.js](first_unique_character_in_a_string.js) | 124 ms | 39.3 MB | Easy | | | | +| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [add_string.js](add_string.js) | 60 ms | 36.5 MB | Easy | 0(N) | O(1) | | +| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [two_sum_II.js](two_sum_II.js) | 64 ms | 35.5 MB | Easy | | | | +| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [valid_parentheses.js](valid_parentheses.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(1) | | +| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [best_time_to_buy_and_sell_stock.js](best_time_to_buy_and_sell_stock.js) | 56 ms | 36.5 MB | Easy | 0(N) | O(1) | | +| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [valid_palindrome_II.js](valid_palindrome_II.js) | 72 ms | 43.2 MB | Easy | O(N) | O(1) | | +| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [third_maximum_number.js](third_maximum_number.js) | 76 ms | 38 MB | Easy | | | | +| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium | | | | +| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](binary_tree_inorder_traversal.js) | 56 ms | 33.7 MB | Medium | | | | +| 1015 | [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) | [Solution](smallest_integer_divisible.js) | 52 ms | 34.3 MB | Medium | | | | +| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | | | +| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | | | | +| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | | | +| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | | | +| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | | | | +| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [combination_sum_II.js](combination_sum_II.js) | 84 ms | 37.6 MB | Medium | | | | +| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [permutations.js](permutations.js) | 88 ms | 37.6 MB | Medium | | | | +| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [remove_duplicates_from_sorted_array_II.js](remove_duplicates_from_sorted_array_II.js) | 64 ms | 35.8 MB | Medium | | | | +| 78 | [Subsets](https://leetcode.com/problems/subsets/) | [subset.js](subset.js) | 60 ms | 35 MB | Medium | | | | +| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [reverse_words_in_string.js](reverse_words_in_string.js) | 56 ms | 34.6 MB | Medium | | | | +| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [palindromic_substrings.js](palindromic_substrings.js) | 704 ms | 37.2 MB | Medium | | | | +| 102. | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [binary_tree_level_order_traversal.js](binary_tree_level_order_traversal.js) | 60 ms | 34.9 MB | Medium | O(N) | | | +| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium | | | | +| 287 | [ Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [find_the_duplicate_number.js](find_the_duplicate_number.js) | 84 ms | 36 MB | Medium | O(N) | O(1) | | +| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [minimum_path_sum.js](minimum_path_sum.js) | 52 ms | 35.5 MB | Medium | o(N2) | O(1) | [:link:](https://www.youtube.com/watch?v=lBRtnuxg-gU) | +| 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [group_anagrams.js](group_anagrams.js) | 152 ms | 47.3 MB | Medium | NlogN | | [:link:](https://leetcode.com/problems/group-anagrams/solution/ ) | +| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Sliding Window Approach](longest_substring.js) | 84 ms | 38.4 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs) | | 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [product_of_array_except_self.js](product_of_array_except_self.js) | 76 ms | 42.3 MB | Medium | O(N) | O(1) | [:link:](https://leetcode.com/problems/product-of-array-except-self/solution/) | -| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Iterative](single_number_II.js) | 64 ms | 37.6 MB | Medium | NlogN | | [:link:](https://www.youtube.com/watch?v=KXjgQWDvQ24) | -| 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [Ratcheting](container_with_most_water.js) | 52 ms | 35.4 MB | Medium | O(N) | O(1) | [:link:](https://www.youtube.com/watch?v=k5fbSqb9sCI&t=138s) | -| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [Recursive](swap_nodes_in_pairs.js) | 68 ms | 34 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=zOovxGmION4) | -| 48 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [Iterative](rotate_image.js) | 56 ms | 33.7 MB | Medium | O(N) | O(1) | [:link:](https://leetcode.com/problems/rotate-image/solution/) | -| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Itterative](valid_sudoku.js) | 68 ms | 37.7 MB | Medium | O(1) | O(1) | [:link:](https://leetcode.com/problems/valid-sudoku/solution/) | -| 146 | [ LRU Cache](https://leetcode.com/problems/lru-cache/) | [Map](lru_cache.js) | 216 ms | 58.8 MB | Medium | O(1) | O(1) | | -| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) | -| 297 | [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [DFS Recursion](serialize_and_deserialize_binary_tree.js) | 80 ms | 43.6 MB | Hard | | | [:link:](https://www.youtube.com/watch?v=suj1ro8TIVY) | +| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Iterative](single_number_II.js) | 64 ms | 37.6 MB | Medium | NlogN | | [:link:](https://www.youtube.com/watch?v=KXjgQWDvQ24) | +| 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [Ratcheting](container_with_most_water.js) | 52 ms | 35.4 MB | Medium | O(N) | O(1) | [:link:](https://www.youtube.com/watch?v=k5fbSqb9sCI&t=138s) | +| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [Recursive](swap_nodes_in_pairs.js) | 68 ms | 34 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=zOovxGmION4) | +| 48 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [Iterative](rotate_image.js) | 56 ms | 33.7 MB | Medium | O(N) | O(1) | [:link:](https://leetcode.com/problems/rotate-image/solution/) | +| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Itterative](valid_sudoku.js) | 68 ms | 37.7 MB | Medium | O(1) | O(1) | [:link:](https://leetcode.com/problems/valid-sudoku/solution/) | +| 146 | [ LRU Cache](https://leetcode.com/problems/lru-cache/) | [Map](lru_cache.js) | 216 ms | 58.8 MB | Medium | O(1) | O(1) | | +| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [Dynamic Programming](max_product.js) | 60 ms | 35 MB | Medium | O(N) | O(1) | [:link:](https://www.youtube.com/watch?v=-rUBh45rugs) | +| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) | +| 297 | [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [DFS Recursion](serialize_and_deserialize_binary_tree.js) | 80 ms | 43.6 MB | Hard | | | [:link:](https://www.youtube.com/watch?v=suj1ro8TIVY) | ## Others diff --git a/max_product.js b/max_product.js new file mode 100644 index 0000000..514a861 --- /dev/null +++ b/max_product.js @@ -0,0 +1,28 @@ +// https://leetcode.com/problems/maximum-product-subarray/ +// https://www.youtube.com/watch?v=-rUBh45rugs + +/** + * @param {number[]} nums + * @return {number} + */ +var maxProduct = function(nums) { + let maxSoFar = nums[0], + minSoFar = nums[0], + res = nums[0]; + + for (let i = 1; i < nums.length; i++) { + maxSoFar *= nums[i]; + minSoFar *= nums[i]; + + if (nums[i] < 0) { + let temp = maxSoFar; + maxSoFar = minSoFar; + minSoFar = temp; + } + + maxSoFar = Math.max(maxSoFar, nums[i]); + minSoFar = Math.min(minSoFar, nums[i]); + res = Math.max(maxSoFar, res); + } + return res; +}; From 259b94378a15907c3980e8154648751d3066cd5c Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Sun, 15 Sep 2019 12:32:47 -0700 Subject: [PATCH 84/91] 394. Decode String --- .idea/workspace.xml | 29 ++++++++++++++------------ README.md | 1 + decode_string.js | 51 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 decode_string.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index df02fcb..786ab00 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,8 @@ - - + + @@ -174,14 +174,10 @@ - - - - 1561607716498 - 1561616278761 @@ -519,7 +515,14 @@ - @@ -550,7 +553,6 @@ diff --git a/README.md b/README.md index e387cb0..356a1fb 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ | 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Itterative](valid_sudoku.js) | 68 ms | 37.7 MB | Medium | O(1) | O(1) | [:link:](https://leetcode.com/problems/valid-sudoku/solution/) | | 146 | [ LRU Cache](https://leetcode.com/problems/lru-cache/) | [Map](lru_cache.js) | 216 ms | 58.8 MB | Medium | O(1) | O(1) | | | 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [Dynamic Programming](max_product.js) | 60 ms | 35 MB | Medium | O(N) | O(1) | [:link:](https://www.youtube.com/watch?v=-rUBh45rugs) | +| 394 | [Decode String](https://leetcode.com/problems/decode-string/) | [Decode String](decode_string.js) | 68 ms | 33.7 MB | Medium | | | | | 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) | | 297 | [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [DFS Recursion](serialize_and_deserialize_binary_tree.js) | 80 ms | 43.6 MB | Hard | | | [:link:](https://www.youtube.com/watch?v=suj1ro8TIVY) | diff --git a/decode_string.js b/decode_string.js new file mode 100644 index 0000000..8be2999 --- /dev/null +++ b/decode_string.js @@ -0,0 +1,51 @@ +//https://leetcode.com/problems/decode-string/ + +/** + * @param {string} s + * @return {string} + */ +var decodeString = function(s) { + let arr = []; + for (let i = 0; i < s.length; i++) { + let isInteger = Number.isInteger(parseInt(s[i])); + let numberString = ""; + while (isInteger) { + isInteger = Number.isInteger(parseInt(s[i + 1])); + numberString = numberString + s[i]; + i++; + if (!isInteger) { + arr.push(numberString); + } + } + arr.push(s[i]); + } + let stack = []; + for (let i = 0; i < arr.length; i++) { + const current = arr[i]; + + if (current === "]") { + let lastElement = stack.shift(); + let tempString = lastElement; + while (lastElement !== "[") { + lastElement = stack.shift(); + if (lastElement !== "[") { + tempString = lastElement + tempString; + } + } + const number = stack.shift(); + const tempOut = tempString + .split("") + .reverse() + .join("") + .repeat(number) + .split(""); //? + stack.unshift(...tempOut); + } else { + stack.unshift(current); + } + } + + return stack.reverse().join(""); +}; + +decodeString("100[abc]3[cd]ef"); //? From 72cad73e4c30829404db5f430b15f3af10a83368 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Mon, 16 Sep 2019 22:11:19 -0700 Subject: [PATCH 85/91] 394. Decode String --- .idea/workspace.xml | 27 ++++++++++---------- README.md | 1 + longest_continuous_increasing_subsequence.js | 26 +++++++++++++++++++ 3 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 longest_continuous_increasing_subsequence.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 786ab00..c0371be 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,8 @@ - - + + @@ -177,14 +177,8 @@ - - - - 1561616278761 - 1561621577719 @@ -522,7 +516,14 @@ - @@ -553,7 +554,6 @@ diff --git a/README.md b/README.md index 356a1fb..cbe89c3 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ |:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:-------------------------------------------------------------------------------| | 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | | 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [two_sum.js](two_sum.js) | 52 ms | 35 MB | Easy | | | | +| 674 | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [Window Sliding Method](longest_continuous_increasing_subsequence.js) | | 35.1 MB | Easy | | | | | 443 | [String Compression](https://leetcode.com/problems/string-compression/) | [string_compression.js](string_compression.js) | | | Easy | | | | | 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [iterative](best_time_to_buy_and_sell_stock_II.js) | 56 ms | 35.4 MB | Easy | O(N) | O(1) | | | 1122 | [Relative Sort Array](https://leetcode.com/problems/relative-sort-array/) | [Ittrative](relative_sort_array.js) | 52 ms | 34.9 MB | Easy | | | | diff --git a/longest_continuous_increasing_subsequence.js b/longest_continuous_increasing_subsequence.js new file mode 100644 index 0000000..e23f7ce --- /dev/null +++ b/longest_continuous_increasing_subsequence.js @@ -0,0 +1,26 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var findLengthOfLCIS = function(nums) { + let counter = 1; + let out = 0; + let i = 0; + let j = 0; + + while (j < nums.length) { + if (nums[j] < nums[j + 1]) { + counter++; + j++; + } else { + i = j; + j++; + counter = 1; + } + out = Math.max(out, counter); + } + + return out; +}; + +findLengthOfLCIS([]); //? From 81828df498595d8e1b1e76a5fc11aee01a74e014 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Wed, 18 Sep 2019 16:07:07 -0700 Subject: [PATCH 86/91] 1085. Sum of Digits in the Minimum Number --- .idea/workspace.xml | 23 +++++++++++++---------- README.md | 1 + sum_of_digits_in_the_minimum_number.js | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 sum_of_digits_in_the_minimum_number.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index c0371be..236d0b2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,8 @@ - + + @@ -178,14 +179,9 @@ - - - - 1561621577719 - 1561847198469 @@ -523,7 +519,14 @@ - diff --git a/README.md b/README.md index cbe89c3..3141130 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ |:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:-------------------------------------------------------------------------------| | 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | | | 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [two_sum.js](two_sum.js) | 52 ms | 35 MB | Easy | | | | +| 1085 | [Sum of Digits in the Minimum Number](https://leetcode.com/problems/sum-of-digits-in-the-minimum-number/) | [Iterative](sum_of_digits_in_the_minimum_number.js) | 56 ms | 34.9 MB | Easy | | | | | 674 | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [Window Sliding Method](longest_continuous_increasing_subsequence.js) | | 35.1 MB | Easy | | | | | 443 | [String Compression](https://leetcode.com/problems/string-compression/) | [string_compression.js](string_compression.js) | | | Easy | | | | | 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [iterative](best_time_to_buy_and_sell_stock_II.js) | 56 ms | 35.4 MB | Easy | O(N) | O(1) | | diff --git a/sum_of_digits_in_the_minimum_number.js b/sum_of_digits_in_the_minimum_number.js new file mode 100644 index 0000000..15de70c --- /dev/null +++ b/sum_of_digits_in_the_minimum_number.js @@ -0,0 +1,16 @@ +/** + * @param {number[]} A + * @return {number} + */ +var sumOfDigits = function(A) { + let minimum = A.sort((a, b) => a - b)[0]; + let sum = minimum + .toString() + .split("") + .map(value => parseInt(value)) + .reduce((a, total) => total + parseInt(a)); //? + + return sum % 2 === 0 ? 1 : 0; +}; + +sumOfDigits([34, 23, 100, 24, 75, 33, 54, 80]); //? From 843d1e69db75d01ddd7c063c020a9251a1db9173 Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Mon, 23 Sep 2019 19:47:27 -0700 Subject: [PATCH 87/91] 76. Minimum Window Substring --- .idea/workspace.xml | 33 ++++++++++++------------- README.md | 1 + minimum_window_substring.js | 48 +++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 minimum_window_substring.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 236d0b2..e035670 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,11 +1,9 @@ - - - + + - @@ -557,8 +558,6 @@ diff --git a/README.md b/README.md index 3141130..e250c69 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ | 394 | [Decode String](https://leetcode.com/problems/decode-string/) | [Decode String](decode_string.js) | 68 ms | 33.7 MB | Medium | | | | | 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) | | 297 | [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [DFS Recursion](serialize_and_deserialize_binary_tree.js) | 80 ms | 43.6 MB | Hard | | | [:link:](https://www.youtube.com/watch?v=suj1ro8TIVY) | +| 76 | [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) | [Sliding Window](minimum_window_substring.js) | | | Hard | | | | ## Others diff --git a/minimum_window_substring.js b/minimum_window_substring.js new file mode 100644 index 0000000..827ffef --- /dev/null +++ b/minimum_window_substring.js @@ -0,0 +1,48 @@ +// https://leetcode.com/problems/minimum-window-substring/ + +let checkValueExist = function(a, b) { + let currentMap = {}; + a.split("").forEach( + value => (currentMap[value] = (currentMap[value] || 0) + 1) + ); + for (let val in b) { + if (!currentMap.hasOwnProperty(val) || !(b[val] <= currentMap[val])) { + return false; + } + } + + return true; +}; + +/** + * @param {string} s + * @param {string} t + * @return {string} + */ +var minWindow = function(s, t) { + let l = 0; + let r = t.length - 1; + let map = {}; + t.split("").forEach(value => (map[value] = (map[value] || 0) + 1)); + + let uniqueCount = Number.POSITIVE_INFINITY; + let outString = ""; + + while (r < s.length) { + let currentVal = s.substring(l, r + 1); + const isAllValueExist = checkValueExist(currentVal, map); + if (isAllValueExist) { + if (currentVal.length < uniqueCount) { + uniqueCount = currentVal.length; + outString = currentVal; + } + l++; + } else { + r++; + } + } + + return outString; +}; + +minWindow("abc", "b"); //? From e9bd9bf8ab47fcad3942a679827bb8b84c6a922d Mon Sep 17 00:00:00 2001 From: Tushar Borole Date: Tue, 24 Sep 2019 21:27:21 -0700 Subject: [PATCH 88/91] 54. Spiral Matrix --- .idea/workspace.xml | 33 +++++++++++++++++++++++---------- README.md | 1 + spiral_matrix.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 spiral_matrix.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index e035670..f326aa0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,9 +1,10 @@ - - + + + + +