diff --git a/3sum/jun0811.js b/3sum/jun0811.js new file mode 100644 index 000000000..0bf9df400 --- /dev/null +++ b/3sum/jun0811.js @@ -0,0 +1,38 @@ +/** + * @param {number[]} nums + * @return {number[][]} + */ + +// 중복 제거 중점.. +var threeSum = function (nums) { + nums.sort((a, b) => a - b); + const result = []; + + for (let i = 0; i < nums.length - 2; i++) { + if (i > 0 && nums[i] === nums[i - 1]) continue; + + let left = i + 1; + let right = nums.length - 1; + + while (left < right) { + const sum = nums[i] + nums[left] + nums[right]; + + if (sum === 0) { + result.push([nums[i], nums[left], nums[right]]); + + // 중복 미리 제거 + while (left < right && nums[left] === nums[left + 1]) left++; + while (left < right && nums[right] === nums[right - 1]) right--; + + left++; + right--; + } else if (sum < 0) { + left++; + } else { + right--; + } + } + } + + return result; +}; diff --git a/climbing-stairs/jun0811.js b/climbing-stairs/jun0811.js new file mode 100644 index 000000000..14e0712bf --- /dev/null +++ b/climbing-stairs/jun0811.js @@ -0,0 +1,17 @@ +/** + * @param {number} n + * @return {number} + */ + +// Time Complexity : O(N) +var climbStairs = function (n) { + if (n == 1) return 1; + if (n == 2) return 2; + + const dp = [1, 2]; + + for (let i = 2; i < n; i++) { + dp[i] = dp[i - 2] + dp[i - 1]; + } + return dp[n - 1]; +}; diff --git a/product-of-array-except-self/jun0811.js b/product-of-array-except-self/jun0811.js new file mode 100644 index 000000000..e203ace89 --- /dev/null +++ b/product-of-array-except-self/jun0811.js @@ -0,0 +1,19 @@ +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function (nums) { + const res = new Array(nums.length).fill(1); + + for (let i = 1; i < nums.length; i++) { + res[i] = res[i - 1] * nums[i - 1]; + } + + let right = 1; + for (let i = nums.length - 1; i >= 0; i--) { + res[i] *= right; + right *= nums[i]; + } + + return res; +}; diff --git a/valid-anagram/jun0811.js b/valid-anagram/jun0811.js new file mode 100644 index 000000000..289fbeb31 --- /dev/null +++ b/valid-anagram/jun0811.js @@ -0,0 +1,30 @@ +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ + +// Time Complexity : O(N) +var isAnagram = function (s, t) { + if (s.length != t.length) return false; + + const hashMap = {}; + + for (const chr of s) { + if (chr in hashMap) { + hashMap[chr] += 1; + } else { + hashMap[chr] = 1; + } + } + + for (const chr of t) { + if (chr in hashMap && hashMap[chr] > 0) { + hashMap[chr] -= 1; + } else { + return false; + } + } + + return true; +}; diff --git a/validate-binary-search-tree/jun0811.js b/validate-binary-search-tree/jun0811.js new file mode 100644 index 000000000..80fd835df --- /dev/null +++ b/validate-binary-search-tree/jun0811.js @@ -0,0 +1,24 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ +function check(node, min, max) { + if (node === null) return true; + if ((min !== null && node.val <= min) || (max !== null && node.val >= max)) { + return false; + } + + return check(node.left, min, node.val) && check(node.right, node.val, max); +} + +function isValidBST(root) { + return check(root, null, null); +}