From d3f2234b3099e290e4fe21edd2ade29bcbcdd690 Mon Sep 17 00:00:00 2001 From: jeongwoo903 Date: Mon, 7 Apr 2025 21:04:16 +0900 Subject: [PATCH 1/6] solve(w02): 242. Valid Anagram --- valid-anagram/jeongwoo903.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 valid-anagram/jeongwoo903.js diff --git a/valid-anagram/jeongwoo903.js b/valid-anagram/jeongwoo903.js new file mode 100644 index 000000000..1d1f20400 --- /dev/null +++ b/valid-anagram/jeongwoo903.js @@ -0,0 +1,18 @@ +/* + * 시간 복잡도: O(n log n) + * 공간 복잡도: O(n) + */ + +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function(s, t) { + if(s.length !== t.length) return false; + + const strA = [...s].sort().join(''); + const strB = [...t].sort().join(''); + + return strA === strB; +}; From 5ce8df8822b9d465f72cff6b79c240fe566dbfcd Mon Sep 17 00:00:00 2001 From: jeongwoo903 Date: Wed, 9 Apr 2025 02:48:59 +0900 Subject: [PATCH 2/6] solve(w02): 70. Climbing Stairs --- climbing-stairs/jeongwoo903.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 climbing-stairs/jeongwoo903.js diff --git a/climbing-stairs/jeongwoo903.js b/climbing-stairs/jeongwoo903.js new file mode 100644 index 000000000..be80f29ae --- /dev/null +++ b/climbing-stairs/jeongwoo903.js @@ -0,0 +1,20 @@ +/* + * 시간 복잡도: O(n) + * 공간 복잡도: O(n) + */ + +/** + * @param {number} n + * @return {number} + */ +var climbStairs = function(n) { + if (n === 1) return 1; + + const dp = [1, 2] + + for (let i = 2; i < n; i++) { + dp[i] = dp[i-1] + dp[i-2] + } + + return dp[n-1] +}; From f71a7436067516d65d7de410c63c293645397062 Mon Sep 17 00:00:00 2001 From: jeongwoo903 Date: Thu, 10 Apr 2025 14:33:01 +0900 Subject: [PATCH 3/6] solve(w02): 238. Product of Array Except Self --- product-of-array-except-self/jeongwoo903.js | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 product-of-array-except-self/jeongwoo903.js diff --git a/product-of-array-except-self/jeongwoo903.js b/product-of-array-except-self/jeongwoo903.js new file mode 100644 index 000000000..e2864c2f4 --- /dev/null +++ b/product-of-array-except-self/jeongwoo903.js @@ -0,0 +1,25 @@ +/* + * 시간 복잡도: O(n) + * 공간 복잡도: O(n) + */ + +/** + * @param {number[]} nums + * @return {number[]} + */ + +var productExceptSelf = function(nums) { + const lefts = nums.reduce((products, num, i) => { + products.push(i === 0 ? 1 : products[i - 1] * nums[i - 1]); + return products; + }, [] + ); + + return nums.reduceRight((result, num, i) => { + const rights = i === nums.length - 1 ? 1 : result.rightProduct * nums[i + 1]; + result.products[i] *= rights; + result.rightProduct = rights; + return result; + }, { products: lefts, rights: 1 } + ).products; +}; From f5d85e4fe65c74d5e9dcd669458c8a16405d6e59 Mon Sep 17 00:00:00 2001 From: jeongwoo903 Date: Thu, 10 Apr 2025 19:36:08 +0900 Subject: [PATCH 4/6] solve(w02): 15. 3Sum --- 3sum/jeongwoo903.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 3sum/jeongwoo903.js diff --git a/3sum/jeongwoo903.js b/3sum/jeongwoo903.js new file mode 100644 index 000000000..5a30cb6bd --- /dev/null +++ b/3sum/jeongwoo903.js @@ -0,0 +1,42 @@ +/* +* 투 포인터 방식 사용 +* 시간 복잡도: O(n^2) +* 공간 복잡도; O(n^2) +*/ + +/** + * @param {number[]} nums + * @return {number[][]} + */ +const threeSum = (nums) => { + const sorted = [...nums].sort((a, b) => a - b); + const results = []; + + sorted.forEach((val, i) => { + if (i > 0 && val === sorted[i - 1]) return; + + let left = i + 1; + let right = sorted.length - 1; + + while (left < right) { + const sum = val + sorted[left] + sorted[right]; + + if (sum === 0) { + results.push([val, sorted[left], sorted[right]]); + + // 중복된 left/right 스킵 + const currentLeft = sorted[left]; + const currentRight = sorted[right]; + + while (left < right && sorted[left] === currentLeft) left++; + while (left < right && sorted[right] === currentRight) right--; + } else if (sum < 0) { + left++; + } else { + right--; + } + } + }); + + return results; +}; From c302622c263b109b41fe394c1f7fd2b95b9e90b6 Mon Sep 17 00:00:00 2001 From: jeongwoo903 Date: Sat, 12 Apr 2025 01:17:19 +0900 Subject: [PATCH 5/6] solve(w02): 98. Validate Binary Search Tree --- validate-binary-search-tree/jeongwoo903.js | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 validate-binary-search-tree/jeongwoo903.js diff --git a/validate-binary-search-tree/jeongwoo903.js b/validate-binary-search-tree/jeongwoo903.js new file mode 100644 index 000000000..e1d8898af --- /dev/null +++ b/validate-binary-search-tree/jeongwoo903.js @@ -0,0 +1,31 @@ +/* +* 시간 복잡도: O(n) +* 공간 복잡도; O(h) +* -> h는 트리의 높이 +*/ + +/** + * 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) { + function bst(node , min, max) { + if (!node) return true; + + if (node.val <= min || node.val >= max) return false; + + return ( + bst(node.left, min, node.val) && helper(node.right, node.val, max) + ); + } + + return bst(root, -Infinity, Infinity); +}; \ No newline at end of file From 72b3f50cd3bb8be2fefc3fa78ed3f20f677ca99a Mon Sep 17 00:00:00 2001 From: jeongwoo903 Date: Sat, 12 Apr 2025 01:21:02 +0900 Subject: [PATCH 6/6] =?UTF-8?q?chore:=20end=20of=20line=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- validate-binary-search-tree/jeongwoo903.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validate-binary-search-tree/jeongwoo903.js b/validate-binary-search-tree/jeongwoo903.js index e1d8898af..ca0d57afd 100644 --- a/validate-binary-search-tree/jeongwoo903.js +++ b/validate-binary-search-tree/jeongwoo903.js @@ -28,4 +28,4 @@ var isValidBST = function(root) { } return bst(root, -Infinity, Infinity); -}; \ No newline at end of file +};