diff --git a/3sum/seueooo.js b/3sum/seueooo.js new file mode 100644 index 0000000000..c8a66de527 --- /dev/null +++ b/3sum/seueooo.js @@ -0,0 +1,37 @@ +/** +정렬, 투포인터 +정렬 후, 첫 번째 수를 고정하고 나머지 두 수를 투포인터로 탐색 +중복되는 경우는 스킵 +시간 복잡도: O(n^2) +공간 복잡도: O(n) + * @param {number[]} nums + * @return {number[][]} + */ +var threeSum = function (nums) { + nums.sort((a, b) => a - b); + let n = nums.length; + let answer = []; + for (let i = 0; i < n - 2; i++) { + let l = i + 1; + let h = n - 1; + if (nums[i] > 0) break; + // 중복 제거 + if (i > 0 && nums[i] === nums[i - 1]) continue; + + while (l < h) { + const sum = nums[i] + nums[l] + nums[h]; + if (sum > 0) { + h--; + } else if (sum < 0) { + l++; + } else { + answer.push([nums[i], nums[l], nums[h]]); + while (l < h && nums[l] === nums[l + 1]) l++; + while (l < h && nums[h] === nums[h - 1]) h--; + l++; + h--; + } + } + } + return answer; +}; diff --git a/combination-sum/seueooo.js b/combination-sum/seueooo.js new file mode 100644 index 0000000000..e8c96937b4 --- /dev/null +++ b/combination-sum/seueooo.js @@ -0,0 +1,34 @@ +/* +백트래킹, dfs +시간복잡도 O(N^(T / M)) +공간복잡도 O(T / M) + +N = candidates.length +T = target +M = candidates 중 최솟값 + + * @param {number[]} candidates + * @param {number} target + * @return {number[][]} + */ +var combinationSum = function (candidates, target) { + let result = []; + + function dfs(start, path, sum) { + if (sum === target) { + result.push([...path]); + return; + } + if (sum > target) return; + + for (let i = start; i < candidates.length; i++) { + path.push(candidates[i]); + dfs(i, path, sum + candidates[i]); + path.pop(); + } + } + + dfs(0, [], 0); + + return result; +}; diff --git a/decode-ways/seueooo.js b/decode-ways/seueooo.js new file mode 100644 index 0000000000..d9625b2550 --- /dev/null +++ b/decode-ways/seueooo.js @@ -0,0 +1,25 @@ +/** +0부터 i번 인덱스 문자열 내에서 해석가능한 조합 개수를 dp에 저장 + * @param {string} s + * @return {number} + */ +var numDecodings = function (s) { + if (s[0] === "0") return 0; + let dp = new Array(s.length).fill(0); + dp[0] = 1; + for (let i = 1; i < s.length; i++) { + if (s[i] !== "0") { + dp[i] += dp[i - 1]; + } + + // 두 자리 수인 경우 (마지막 두덩이를 하나로 생각) + const two = Number(s.slice(i - 1, i + 1)); + + if (two >= 10 && two <= 26) { + if (i === 1) { + dp[i] += 1; + } else dp[i] += dp[i - 2]; + } + } + return dp[s.length - 1]; +}; diff --git a/maximum-subarray/seueooo.js b/maximum-subarray/seueooo.js new file mode 100644 index 0000000000..aa7a14870b --- /dev/null +++ b/maximum-subarray/seueooo.js @@ -0,0 +1,18 @@ +/** + * 구간 내 최대 합을 dp에 저장 + * 시간복잡도 O(n) + * 공간복잡도 O(1) + * @param {number[]} nums + * @return {number} + */ +var maxSubArray = function (nums) { + let prev = nums[0]; + let max = nums[0]; + + for (let i = 1; i < nums.length; i++) { + prev = Math.max(prev + nums[i], nums[i]); + max = Math.max(max, prev); + } + + return max; +}; diff --git a/number-of-1-bits/seueooo.js b/number-of-1-bits/seueooo.js new file mode 100644 index 0000000000..3e8331ffa5 --- /dev/null +++ b/number-of-1-bits/seueooo.js @@ -0,0 +1,15 @@ +/** + * 정수 n의 이진수 표현에서 1의 개수를 반환 + * 시간복잡도 O(log n) - 매 반복마다 n이 절반으로 줄어듦 + * 공간복잡도 O(1) + * @param {number} n + * @return {number} + */ +var hammingWeight = function (n) { + let count = 0; + while (n > 0) { + if (n % 2 === 1) count++; + n = Math.floor(n / 2); + } + return count; +}; diff --git a/valid-palindrome/seueooo.js b/valid-palindrome/seueooo.js new file mode 100644 index 0000000000..6af80feb67 --- /dev/null +++ b/valid-palindrome/seueooo.js @@ -0,0 +1,16 @@ +/** + * 정규표현식으로 알파벳과 숫자만 남기고 소문자로 변환 후, 양 끝에서부터 비교 + * 시간복잡도 O(n) + * 공간복잡도 O(n) + * @param {string} s + * @return {boolean} + */ +var isPalindrome = function (s) { + let newArr = s.replace(/[^a-zA-Z0-9]/g, "").toLowerCase(); + for (let i = 0; i < Math.ceil(newArr.length / 2); i++) { + if (newArr[i] !== newArr[newArr.length - 1 - i]) { + return false; + } + } + return true; +}; diff --git a/validate-binary-search-tree/seueooo.js b/validate-binary-search-tree/seueooo.js new file mode 100644 index 0000000000..439666572c --- /dev/null +++ b/validate-binary-search-tree/seueooo.js @@ -0,0 +1,27 @@ +/** +중위 순회하면서 계속 값이 커지는지 확인 +시간복잡도 O(n) +공간복잡도 O(n) + * 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) { + let prev = -Infinity; + + const inOrder = (node) => { + if (!node) return true; + if (!inOrder(node.left)) return false; + if (node.val <= prev) return false; + prev = node.val; + return inOrder(node.right); + }; + return inOrder(root); +};