-
-
Notifications
You must be signed in to change notification settings - Fork 195
[hoyeongkwak] WEEK 02 Solutions #1268
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
8 commits
Select commit
Hold shift + click to select a range
3ce4c7c
3sum solution
36e83e6
climbing stairs solution
hoyeongkwak aae15b9
product of array expect self solution
hoyeongkwak a9c1996
valid anagram solution
hoyeongkwak 0aee6c4
validate binary search tree solution
hoyeongkwak 67c9a25
week1 solution
hoyeongkwak b7e5af5
Revert "week1 solution"
hoyeongkwak 5a77dce
solution add empty line
hoyeongkwak 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,30 @@ | ||
/* | ||
time complexity : O(n^2) | ||
space complexity : O(1) | ||
*/ | ||
function threeSum(nums: number[]): number[][] { | ||
const result: number[][] = [] | ||
const sortedNums = nums.sort((a, b) => a - b) | ||
|
||
for(let i = 0; i < sortedNums.length - 2; i++) { | ||
if (i > 0 && sortedNums[i] === sortedNums[i - 1]) continue | ||
let low = i + 1 | ||
let high = sortedNums.length - 1 | ||
while (low < high) { | ||
const threeSum = sortedNums[i] + sortedNums[low] + sortedNums[high] | ||
if (threeSum < 0) { | ||
low += 1 | ||
} else if (threeSum > 0) { | ||
high -= 1 | ||
} else { | ||
result.push([sortedNums[i], sortedNums[low], sortedNums[high]]) | ||
while (low < high && sortedNums[low] === sortedNums[low + 1]) low++ | ||
while (low < high && sortedNums[high] === sortedNums[high - 1]) high-- | ||
|
||
low += 1 | ||
high -= 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,15 @@ | ||
/* | ||
시간복잡도 : O(n) | ||
공간복잡도 : O(1) | ||
*/ | ||
function climbStairs(n: number): number { | ||
if (n < 3) return n | ||
let prev = 1 | ||
let curr = 2 | ||
for (let i = 0; i < n - 2; i++) { | ||
const tempPrev = prev | ||
prev = curr | ||
curr = tempPrev + curr | ||
} | ||
return curr | ||
} |
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,19 @@ | ||
/* | ||
time complexity : O(n) | ||
space complexity : O(1) | ||
*/ | ||
function productExceptSelf(nums: number[]): number[] { | ||
const results = new Array(nums.length).fill(1) | ||
let before = 1 | ||
let after = 1 | ||
for (let i = 0; i < nums.length - 1; i++) { | ||
before *= nums[i] | ||
results[i + 1] *= before | ||
} | ||
for (let i = nums.length - 1; i > 0; i--) { | ||
after *= nums[i] | ||
results[i - 1] *= after | ||
} | ||
|
||
return results | ||
} |
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 @@ | ||
function isAnagram(s: string, t: string): boolean { | ||
// 시간복잡도 O(nlogn), 공간복잡도 O(n) | ||
// const sSorted = s.split('').sort().join(',') | ||
// const tSorted = t.split('').sort().join(',') | ||
// return sSorted === tSorted | ||
|
||
/* | ||
시간복잡도 O(n), 공간복잡도 O(1) | ||
*/ | ||
if (s.length != t.length) return false | ||
const count = new Array(26).fill(0) | ||
for (let i = 0; i < s.length; i++) { | ||
count[s.charCodeAt(i) - 97]++ | ||
count[t.charCodeAt(i) - 97]-- | ||
} | ||
return count.every(c => c === 0) | ||
} |
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,34 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* class TreeNode { | ||
* val: number | ||
* left: TreeNode | null | ||
* right: TreeNode | null | ||
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
* } | ||
*/ | ||
/* | ||
time complexity : O(n) | ||
space complexity : O(n) | ||
*/ | ||
function isValidBST(root: TreeNode | null): boolean { | ||
let prev = -Infinity | ||
let isValid = true | ||
const inOrder = (node: TreeNode | null): void => { | ||
if (!isValid || !node) return | ||
inOrder(node.left) | ||
|
||
if (prev >= node.val) { | ||
isValid = false | ||
return | ||
} | ||
prev = node.val | ||
inOrder(node.right) | ||
} | ||
inOrder(root) | ||
return isValid | ||
} |
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.
아스키 코드를 활용해서 푼게 인상적이네요! 👍👍