From f0fe78b2c3695eefa44fdb7aec568d06b5d855c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=B8=EC=B0=AC?= Date: Mon, 7 Apr 2025 22:28:31 +0900 Subject: [PATCH 1/5] valid-anagram solution --- valid-anagram/lhc0506.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 valid-anagram/lhc0506.js 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; +}; From ba678b459dfb0b5d6be70ed8899b480b01929e3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=B8=EC=B0=AC?= Date: Tue, 8 Apr 2025 23:44:03 +0900 Subject: [PATCH 2/5] climbing-stairs solution --- climbing-stairs/lhc0506.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 climbing-stairs/lhc0506.js 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; +}; From 4b1490ffa4a0b2a68ab652919daf0cd00219d571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=B8=EC=B0=AC?= Date: Fri, 11 Apr 2025 00:26:36 +0900 Subject: [PATCH 3/5] product-of-array-except-self solution --- product-of-array-except-self/lhc0506.js | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 product-of-array-except-self/lhc0506.js 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) From 3b32de0f1d3d361a71e7514d52424ab441c7e2f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=B8=EC=B0=AC?= Date: Sat, 12 Apr 2025 09:01:02 +0900 Subject: [PATCH 4/5] 3sum solution --- 3sum/lhc0506.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 3sum/lhc0506.js 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 에러 발생 +// 추후 다시 풀어볼 예정 From 271d463a428a3b2c0c37cfdd5e6ec171cb33bda0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=B8=EC=B0=AC?= Date: Sat, 12 Apr 2025 09:03:48 +0900 Subject: [PATCH 5/5] validate-binary-search-tree solution --- validate-binary-search-tree/lhc0506.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 validate-binary-search-tree/lhc0506.js 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)