-
-
Notifications
You must be signed in to change notification settings - Fork 195
[byol-han] WEEK 02 solutions #1223
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b5c94f9
valid anagram solution
0ea55a8
Merge branch 'DaleStudy:main' into main
byol-han 08a8463
Merge branch 'DaleStudy:main' into main
byol-han 7e9affb
climbing stairs solution
6945bd0
product of array except self solution
2c24c86
3sum solution
b84cb92
validate binary search tree solution
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[][]} | ||
*/ | ||
var threeSum = function (nums) { | ||
// 오름차순 정렬 | ||
nums.sort((a, b) => a - b); | ||
const result = []; | ||
|
||
for (let i = 0; i < nums.length - 2; i++) { | ||
// 중복된 숫자는 스킵 | ||
if (i > 0 && nums[i] === nums[i - 1]) continue; | ||
|
||
let left = i + 1; | ||
let right = nums.length - 1; | ||
|
||
while (left < right) { | ||
const sum = nums[i] + nums[left] + nums[right]; | ||
|
||
if (sum < 0) { | ||
left++; | ||
} else if (sum > 0) { | ||
right--; | ||
} else { | ||
result.push([nums[i], nums[left], nums[right]]); | ||
// 중복된 left,right 값 스킵 | ||
while (left < right && nums[left] === nums[left + 1]) left++; | ||
while (left < right && nums[right] === nums[right - 1]) right--; | ||
|
||
left++; | ||
right--; | ||
} | ||
} | ||
} | ||
return result; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var climbStairs = function (n) { | ||
if (n <= 2) return n; | ||
|
||
let first = 1; | ||
let second = 2; | ||
|
||
for (let i = 3; i <= n; i++) { | ||
let count = first + second; | ||
first = second; | ||
second = count; | ||
} | ||
return second; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[]} | ||
*/ | ||
var productExceptSelf = function (nums) { | ||
// 1. | ||
// 각 인덱스에서 자기 자신 제외한 배열 만든 뒤 곱셈 수행 → 시간복잡도 O(n²) | ||
// (중첩 루프로 인해 시간복잡도 O(n²), 큰 입력에서는 시간 초과 발생) | ||
let result = []; | ||
for (let i = 0; i < nums.length; i++) { | ||
const productNums = [...nums.slice(0, i), ...nums.slice(i + 1)]; | ||
|
||
let product = 1; | ||
for (let j = 0; j < productNums.length; j++) { | ||
product *= productNums[j]; | ||
} | ||
result.push(product); | ||
} | ||
return result; | ||
|
||
// 2. | ||
const n = nums.length; | ||
// 정답 배열을 1로 초기화 (곱셈에 영향을 주지 않도록) | ||
const answer = new Array(n).fill(1); | ||
|
||
// 왼쪽 누적 곱 계산 | ||
let left = 1; | ||
for (let i = 0; i < n; i++) { | ||
answer[i] = left; | ||
left *= nums[i]; | ||
} | ||
|
||
// 오른쪽 누적 곱 계산 | ||
let right = 1; | ||
for (let i = n - 1; i >= 0; i--) { | ||
answer[i] *= right; | ||
right *= nums[i]; | ||
} | ||
|
||
return answer; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {boolean} | ||
*/ | ||
var isAnagram = function (s, t) { | ||
if (s.length !== t.length) return false; | ||
|
||
// 문자열 t를 배열로 변환해서 문자 제거할 수 있게 함 | ||
let tArr = t.split(""); | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
let index = tArr.indexOf(s[i]); // s[i]가 tArr에 있는지 확인 | ||
if (index === -1) { | ||
return false; | ||
} | ||
tArr.splice(index, 1); | ||
} | ||
return true; | ||
}; |
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(Binary Search Tree) 잘 이해못함. 더 공부해야함. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* 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 helper(node, lower = -Infinity, upper = Infinity) { | ||
if (!node) return true; | ||
|
||
const val = node.val; | ||
|
||
// 현재 노드가 범위를 벗어나면 false | ||
if (val <= lower || val >= upper) { | ||
return false; | ||
} | ||
|
||
// 오른쪽 서브트리: 최소값은 현재 노드 값 | ||
if (!helper(node.right, val, upper)) { | ||
return false; | ||
} | ||
|
||
// 왼쪽 서브트리: 최대값은 현재 노드 값 | ||
if (!helper(node.left, lower, val)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return helper(root); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
DP로 푸는건 생각 못하고있었는데 훨씬 간단하게 풀 수 있는 문제였군요....