-
-
Notifications
You must be signed in to change notification settings - Fork 195
[seungseung88] WEEK 02 solutions #1203
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
9 commits
Select commit
Hold shift + click to select a range
3aa6b3b
valid-anagram 풀이1 #218
seungseung88 abbbf42
Merge branch 'DaleStudy:main' into main
seungseung88 306ede0
Climbing Stairs 풀이1 #230
seungseung88 35696ee
Merge remote-tracking branch 'refs/remotes/origin/main'
seungseung88 c858905
풀이1. Product of Array Except Self #239
seungseung88 5609517
풀이2. Climbing Stairs #230
seungseung88 0383e00
풀이2: Climbing Stairs #230
seungseung88 92c6959
풀이1: 3Sum #241
seungseung88 56d90d1
풀이1: Validate Binary Search Tree #251
seungseung88 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,35 @@ | ||
/** | ||
* | ||
* 시간 복잡도: O(n log n) + O(n^2) => O(n^2) | ||
* 공간 복잡도: O(1) | ||
* - 자바스크립트 배열은 원래의 배열 자체를 바꿈 | ||
*/ | ||
const threeSum = (numbers) => { | ||
let result = []; | ||
numbers.sort((a, b) => a - b); | ||
|
||
for (let i = 0; i < numbers.length; i += 1) { | ||
if (i > 0 && numbers[i] === numbers[i - 1]) continue; | ||
|
||
let l = i + 1; | ||
let r = numbers.length - 1; | ||
|
||
while (l < r) { | ||
const threeSum = numbers[i] + numbers[l] + numbers[r]; | ||
|
||
if (threeSum > 0) { | ||
r -= 1; | ||
} else if (threeSum < 0) { | ||
l += 1; | ||
} else { | ||
result.push([numbers[i], numbers[l], numbers[r]]); | ||
while (l < r && numbers[l] === numbers[l + 1]) l += 1; | ||
while (l < r && numbers[r] === numbers[r - 1]) r -= 1; | ||
l += 1; | ||
r -= 1; | ||
} | ||
} | ||
} | ||
|
||
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,33 @@ | ||
/** | ||
* 시간복잡도: O(n) | ||
* - for문 O(n) | ||
* 공간복잡도: O(n) | ||
* - arr O(n) | ||
*/ | ||
// const climbStairs = (n) => { | ||
// const arr = [1, 2]; | ||
|
||
// for (let i = 2; i < n; i += 1) { | ||
// arr[i] = arr[i - 1] + arr[i - 2]; | ||
// } | ||
|
||
// return arr[n - 1]; | ||
// }; | ||
|
||
/** | ||
* 시간복잡도: O(n) | ||
* - for문 O(n) | ||
* 공간복잡도: O(1) | ||
*/ | ||
const climbStairs = (n) => { | ||
let one = 1; | ||
let two = 1; | ||
|
||
for (let i = 0; i <= n - 2; i += 1) { | ||
let temp = one + two; | ||
one = two; | ||
two = temp; | ||
} | ||
|
||
return two; | ||
}; |
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,63 @@ | ||
/** | ||
* 시간복잡도: O(n) | ||
* - 첫번째 for문 O(n) | ||
* - 두번째 for문 O(n) | ||
* | ||
* 공간 복잡도: O(1) | ||
* - 추가 배열 생성 X | ||
*/ | ||
// const productExceptSelf = (nums) => { | ||
// let multiplyResult = 1; | ||
// let countZero = 0; | ||
|
||
// for (let i = 0; i < nums.length; i += 1) { | ||
// if (nums[i] === 0) { | ||
// countZero += 1; | ||
|
||
// if (countZero >= 2) { | ||
// multiplyResult = 0; | ||
// break; | ||
// } | ||
|
||
// continue; | ||
// } | ||
// multiplyResult *= nums[i]; | ||
// } | ||
|
||
// for (let i = 0; i < nums.length; i += 1) { | ||
// if (countZero === 1) { | ||
// if (nums[i] === 0) { | ||
// nums[i] = multiplyResult; | ||
// } else { | ||
// nums[i] = 0; | ||
// } | ||
// } else if (countZero >= 2) { | ||
// nums[i] = 0; | ||
// } else { | ||
// nums[i] = multiplyResult / nums[i]; | ||
// } | ||
// } | ||
|
||
// return nums; | ||
// }; | ||
|
||
// 누적 합 이용 | ||
const productExceptSelf = (nums) => { | ||
const result = 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; | ||
}; |
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,24 @@ | ||
/** | ||
* 시간복잡도: O(n) | ||
* - 첫번째 for문 O(n) | ||
* - 두번째 for문 최대 O(n) | ||
* 공간복잡도: O(n) | ||
* - count O(n) | ||
*/ | ||
|
||
const isAnagram = (s, t) => { | ||
if (s.length !== t.length) return false; | ||
|
||
const count = {}; | ||
|
||
for (let i = 0; i < s.length; i += 1) { | ||
count[s[i]] = (count[s[i]] || 0) + 1; | ||
count[t[i]] = (count[t[i]] || 0) - 1; | ||
} | ||
|
||
for (const i of Object.values(count)) { | ||
if (i !== 0) return false; | ||
} | ||
|
||
return true; | ||
}; |
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 {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); | ||
}; |
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.
for in 연산으로 바꾸셔서 Object.values 메소드 배열 생성으로 인한 비용을 줄여서 작성하시면 더 효율적일 거 같습니다!