diff --git a/3sum/reach0908.js b/3sum/reach0908.js new file mode 100644 index 000000000..18895e583 --- /dev/null +++ b/3sum/reach0908.js @@ -0,0 +1,46 @@ +/** + * 시간복잡도: O(n²) + * 공간복잡도: O(1) (결과 배열 제외) + * 풀이 방법: 정렬 후 투 포인터 방식 + * @param {number[]} nums + * @return {number[][]} + */ +const threeSum = function (nums) { + const sortedNums = nums.sort((a, b) => a - b); + const result = []; + + for (let i = 0; i < sortedNums.length; i += 1) { + // 첫 번째 요소의 중복 제거 + if (i > 0 && sortedNums[i] === sortedNums[i - 1]) { + continue; + } + + let left = i + 1; + let right = sortedNums.length - 1; + + while (left < right) { + const threeSum = sortedNums[i] + sortedNums[left] + sortedNums[right]; + + if (threeSum > 0) { + right -= 1; + } else if (threeSum < 0) { + left += 1; + } else { + result.push([sortedNums[i], sortedNums[left], sortedNums[right]]); + + // 중복 제거 + while (left < right && sortedNums[left] === sortedNums[left + 1]) { + left += 1; + } + while (left < right && sortedNums[right] === sortedNums[right - 1]) { + right -= 1; + } + + left += 1; + right -= 1; + } + } + } + + return result; +}; diff --git a/climbing-stairs/reach0908.js b/climbing-stairs/reach0908.js new file mode 100644 index 000000000..aa49b68c3 --- /dev/null +++ b/climbing-stairs/reach0908.js @@ -0,0 +1,18 @@ +/** + * @description + * time complexity: O(n) + * space complexity: O(n) + * runtime: 0ms + * 풀이 방법: 기본적인 DP 풀이 방법 + * @param {number} n + * @return {number} + */ +const climbStairs = function (n) { + const dp = [1, 2]; + + for (let i = 2; i < n; i += 1) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + + return dp[n - 1]; +}; diff --git a/product-of-array-except-self/reach0908.js b/product-of-array-except-self/reach0908.js new file mode 100644 index 000000000..498cf2e57 --- /dev/null +++ b/product-of-array-except-self/reach0908.js @@ -0,0 +1,28 @@ +/** + * @description + * time complexity: O(n) + * space complexity: O(n) + * runtime: 5ms + * 풀이 방법 : 40분고민하다가 다른사람 풀이보고 누적합을 이용하면 된다해서 구현함 + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function (nums) { + const result = new Array(nums.length).fill(1); + + let prefix = 1; + + for (let i = 0; i < nums.length; i += 1) { + result[i] = prefix; + prefix *= nums[i]; + } + + let postfix = 1; + + for (let i = nums.length - 1; i >= 0; i -= 1) { + result[i] *= postfix; + postfix *= nums[i]; + } + + return result; +}; diff --git a/valid-anagram/reach0908.js b/valid-anagram/reach0908.js new file mode 100644 index 000000000..7d325ffc7 --- /dev/null +++ b/valid-anagram/reach0908.js @@ -0,0 +1,40 @@ +/** + * @description + * time complexity: O(nlogn) split 시 새로운 배열을 생성하고 sort 시 nlogn 시간 소요 + * space complexity: O(n) split 시 새로운 배열을 생성함 + * runtime: 32ms + * 풀이 방법: 두 문자열을 정렬하여 비교하는 방법 + * @param {string} s + * @param {string} t + * @return {boolean} + */ +const isAnagram = function (s, t) { + return s.split("").sort().join("") === t.split("").sort().join(""); +}; + +/** + * @description + * time complexity: O(n) + * space complexity: O(n) + * runtime: 15ms + * 풀이 방법: 해쉬맵을 통해 카운트를 추가하거나 제거하는 방식, 유니코드도 대응가능 + * @param {string} s + * @param {string} t + * @return {boolean} + */ +const isAnagramSolution2 = function (s, t) { + if (s.length !== t.length) return false; + + const map = new Map(); + + for (let i = 0; i < s.length; i += 1) { + map.set(s[i], (map.get(s[i]) || 0) + 1); + map.set(t[i], (map.get(t[i]) || 0) - 1); + } + + for (const value of map.values()) { + if (value !== 0) return false; + } + + return true; +}; diff --git a/validate-binary-search-tree/reach0908.js b/validate-binary-search-tree/reach0908.js new file mode 100644 index 000000000..811abc058 --- /dev/null +++ b/validate-binary-search-tree/reach0908.js @@ -0,0 +1,26 @@ +/** + * 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, low = -Infinity, high = Infinity) { + if (!root) { + return true; + } + + if (root.val <= low || root.val >= high) { + return false; + } + + return ( + isValidBST(root.left, low, root.val) && + isValidBST(root.right, root.val, high) + ); +};