-
-
Notifications
You must be signed in to change notification settings - Fork 362
[seueooo] WEEK 03 Solutions #2725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e242311
ca45b97
5d852dc
54237ee
9e83077
5d5ffbe
10172b5
bcd0425
7b8764b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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--; | ||
| } | ||
| } | ||
|
Comment on lines
+14
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 꼼꼼하게 최적화 하셔서 풀어주셨네요 :) |
||
| } | ||
| return answer; | ||
| }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 가능한 모든 조합을 중복 없이 탐색하는 DFS 방식이다. 가지치기 조건(sum > target)으로 일부 경우를 절감한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| 백트래킹, dfs | ||
| 시간복잡도 O(N^(T / M)) | ||
| 공간복잡도 O(T / M) | ||
|
|
||
| N = candidates.length | ||
| T = target | ||
| M = candidates 중 최솟값 | ||
|
|
||
|
Comment on lines
+6
to
+9
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 주석은 코드랑 안 맞는거 같아서 지워주셔도 좋을 거 같아요!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요 부분은 코드랑은 관련 없고 시간복잡도 계산에서 T / M의 의미를 설명하려고 둔 주석이에요 ! |
||
| * @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]); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| path.pop(); | ||
| } | ||
| } | ||
|
|
||
| dfs(0, [], 0); | ||
|
|
||
| return result; | ||
| }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 한 자리 수 가능 여부와 두 자리 수 가능 여부를 조합해 상태를 갱신하는 표준 풀이이다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]; | ||
|
seueooo marked this conversation as resolved.
|
||
| } | ||
| } | ||
| return dp[s.length - 1]; | ||
| }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 이전 누적합과 현재 원소를 비교해 최대 구간 합을 갱신한다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 가장 간단한 자리수 개수 세기 방식으로 동작한다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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; | ||||||||||||||||||||||||||||||
|
Comment on lines
+8
to
+14
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사소한 부분이긴 한데 요런 스타일도 한 번 고려해보셔도 좋을 거 같습니다!
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 시프트 연산자를 거의 사용해보지 않아서 잘 몰랐는데 좋은 정보 알아갑니다 :) |
||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 전처리 문자열 길이에 비례하는 추가 공간과 선형 시간 복잡도를 가진다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 전위/중위 순회에서의 누적 비교로 BST 여부를 판별한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
|
Comment on lines
+17
to
+26
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inOrder 방식으로 풀어주셨군요! 전역변수 prev를 사용하고 있는데 순수 함수 형태로 풀어보셔도 좋을 거 같아요! |
||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정렬 이후 중복 제거를 통해 필요 연산을 줄이고, 각 i마다 l/h를 이동시켜 전체를 두 포인터로 탐색한다.
개선 제안: 현재 구현이 적절해 보입니다.