diff --git a/3sum/lhc0506.js b/3sum/lhc0506.js new file mode 100644 index 000000000..d44586649 --- /dev/null +++ b/3sum/lhc0506.js @@ -0,0 +1,50 @@ +/** + * @param {number[]} nums + * @return {number[][]} + */ +var threeSum = function(nums) { + const result = []; + const numsMap = {}; + + nums.forEach((num, index) => { + numsMap[num] = (numsMap[num] || 0) + 1; + + }); + + for (let i = 0; i < nums.length - 1; i++) { + for (let j = i + 1; j < nums.length; j++) { + const a = nums[i]; + const b = nums[j]; + const targetNum = -(a + b); + + if (!numsMap[targetNum]) { + continue; + } + + if ((targetNum === a || targetNum === b) && numsMap[targetNum] === 1) { + continue; + } + + if (targetNum === a && targetNum === b && numsMap[targetNum] <= 2) { + continue; + } + + const triplet = [a, b, targetNum].sort((x, y) => x - y); + const key = triplet.join(','); + + if (seen.has(key)) { + continue; + } + + seen.add(key); + + result.push(triplet); + } + } + + return result; +}; + + +//시간 복잡도 O(n²) , 공간 복잡도 O(n²) 라고 생각했는데 Time Limit Exceeded 에러 발생 +// 추후 다시 풀어볼 예정 diff --git a/climbing-stairs/lhc0506.js b/climbing-stairs/lhc0506.js new file mode 100644 index 000000000..2c475d930 --- /dev/null +++ b/climbing-stairs/lhc0506.js @@ -0,0 +1,17 @@ +/** + * @param {number} n + * @return {number} + */ +var climbStairs = function(n) { + let preStairs = 0; + let curStairs = 1; + + + for (let i = 0; i < n; i++) { + let sum = preStairs + curStairs; + preStairs = curStairs; + curStairs = sum; + } + + return curStairs; +}; diff --git a/product-of-array-except-self/lhc0506.js b/product-of-array-except-self/lhc0506.js new file mode 100644 index 000000000..5915b11c2 --- /dev/null +++ b/product-of-array-except-self/lhc0506.js @@ -0,0 +1,54 @@ + +// 풀이 1 +// /** +// * @param {number[]} nums +// * @return {number[]} +// */ +// var productExceptSelf = function(nums) { +// let hasZero = false; +// const allProductNums = nums.reduce((acc, cur) => { +// if (cur === 0) { +// if (!hasZero) { +// hasZero = true; +// return acc; +// } + +// return 0; +// } +// return acc * cur; +// }, 1); + +// return nums.map(num => { +// if (num === 0) { +// return allProductNums; +// } + +// return hasZero ? 0 : allProductNums / num; +// }); +// }; + +//풀이 2 +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function(nums) { + const result = Array(nums.length).fill(1); + + let beforeProductNum = 1; + for (let i = 0; i < nums.length; i++) { + result[i] *= beforeProductNum; + beforeProductNum *= nums[i]; + } + + let afterProductNum = 1; + for (let i = nums.length - 1; i >= 0; i--) { + result[i] *= afterProductNum; + afterProductNum *= nums[i]; + } + + return result; +}; + + +//두 풀이 모두 시간 복잡도 O(n), 공간 복잡도는 O(n) diff --git a/valid-anagram/lhc0506.js b/valid-anagram/lhc0506.js new file mode 100644 index 000000000..302bbebca --- /dev/null +++ b/valid-anagram/lhc0506.js @@ -0,0 +1,26 @@ +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function(s, t) { + if (s.length !== t.length) { + return false; + } + + const charCountMap = {}; + + for (let char of s) { + charCountMap[char] = (charCountMap[char] || 0) + 1; + } + + for (let char of t) { + if (!charCountMap[char]) { + return false; + } + + charCountMap[char] -= 1; + } + + return true; +}; diff --git a/validate-binary-search-tree/lhc0506.js b/validate-binary-search-tree/lhc0506.js new file mode 100644 index 000000000..ab3ea2630 --- /dev/null +++ b/validate-binary-search-tree/lhc0506.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} + */ +var isValidBST = function(root) { + const dfs = (node, min, max) => { + if (!node) return true; + if (!((node.val > min) && (node.val < max))) return false; + + return dfs(node.left, min, node.val) && dfs(node.right, node.val, max); + } + + return dfs(root, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER); +}; + +// 시간 복잡도 O(n) , 공간 복잡도 최악의 경우 O(n)