-
-
Notifications
You must be signed in to change notification settings - Fork 245
[Jeehay Park(박지혜)] Week 2 #724
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
4 commits
Select commit
Hold shift + click to select a range
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,86 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[][]} | ||
*/ | ||
|
||
|
||
// Time Complexity: O(n^2) | ||
// Space Complexity: O(n^2) | ||
|
||
var threeSum = function (nums) { | ||
|
||
const sorted = [...nums].sort((a, b) => a - b); | ||
|
||
let result = []; | ||
|
||
// Loop through the array and pick each number as the first number for the triplet | ||
for (let i = 0; i < sorted.length - 2; i++) { | ||
|
||
// skip duplicate values for sorted[middle] | ||
if(i > 0 && sorted[i - 1] === sorted[i]) { | ||
continue; | ||
} | ||
|
||
let left = i + 1; // Left pointer starts right after the current middle | ||
let right = sorted.length - 1; // Right pointer starts at the last element | ||
|
||
while (left < right) { | ||
const sum = sorted[i] + sorted[left] + sorted[right]; | ||
|
||
if (sum === 0) { | ||
result.push([sorted[left], sorted[i], sorted[right]]); | ||
|
||
// skip duplicates for sorted[left] and sorted[right] | ||
while(left < right && sorted[left] === sorted[left + 1]){ | ||
left += 1; // Move left pointer to the right to skip duplicate values | ||
} | ||
|
||
while(left < right && sorted[right] === sorted[right - 1]) { | ||
right -= 1; // Move right pointer to the left to skip duplicate values | ||
} | ||
|
||
left += 1; | ||
right -= 1; | ||
|
||
} else if (sum > 0) { | ||
right -= 1; | ||
|
||
} else { | ||
left += 1 | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
|
||
}; | ||
|
||
|
||
// var threeSum = function (nums) { | ||
|
||
// i != j, i != k, and j != k can be interpreted as i < j < k | ||
|
||
// three nested loops | ||
// time complexity of O(n³) | ||
// Time Limit Exceeded | ||
|
||
// let result = []; | ||
|
||
// for (let i = 0; i < nums.length - 2; i++) { | ||
// for (let j = i + 1; j < nums.length - 1; j++) { | ||
// for (let k = j + 1; k < nums.length; k++) { | ||
// if (nums[i] + nums[j] + nums[k] === 0) { | ||
// const str = [nums[i], nums[j], nums[k]].sort((a, b) => a - b).join(",") | ||
// result.push(str) | ||
// } | ||
// } | ||
// } | ||
|
||
// } | ||
|
||
// result = [... new Set(result)].map(str => str.split(",").map(str => +str)) | ||
|
||
// 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,41 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
|
||
// Time Complexity: O(n^2) | ||
// First O(n): From the loop that runs up to n times. | ||
// Second O(n): From the factorial calculations in each iteration. | ||
// overall time complexity is O(n^2). | ||
|
||
// Space Complexity: O(n) | ||
// the factorial function's recursion has a space complexity of O(n) | ||
// O(1) means constant space complexity. It implies that the amount of memory used does not grow with the size of the input. | ||
// The other variables in the function only use a constant amount of space, resulting in an O(1) space usage. | ||
|
||
var climbStairs = function (n) { | ||
let ways = 0; | ||
|
||
let maxStepTwo = Math.floor(n / 2); | ||
|
||
const factorial = (num) => { | ||
if (num === 0 || num === 1) { | ||
return 1; | ||
} | ||
|
||
for (let i = 2; i <= num; i++) { | ||
return num * factorial(num - 1); | ||
} | ||
}; | ||
|
||
for (let i = 0; i <= maxStepTwo; i++) { | ||
const stepTwo = i; | ||
const stepOne = n - 2 * i; | ||
const steps = stepTwo + stepOne; | ||
|
||
ways += factorial(steps) / (factorial(stepTwo) * factorial(stepOne)); | ||
} | ||
|
||
return ways; | ||
}; | ||
|
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,55 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {boolean} | ||
*/ | ||
|
||
// 시간 복잡도: O(n) | ||
// 공간 복잡도: O(n) | ||
|
||
var isAnagram = function (s, t) { | ||
|
||
if (s.length !== t.length) { | ||
return false; | ||
} | ||
|
||
let obj = {}; | ||
|
||
for (let k of s) { | ||
obj[k] = (obj[k] || 0) + 1; | ||
|
||
} | ||
|
||
for (let k of t) { | ||
if (obj[k] === undefined || obj[k] === 0) { | ||
return false; | ||
} | ||
obj[k]--; | ||
} | ||
|
||
return true; | ||
|
||
}; | ||
|
||
// 시간 복잡도: O(n log n) | ||
// 공간 복잡도: O(n) | ||
|
||
// var isAnagram = function (s, t) { | ||
|
||
// if (s.length !== t.length) { | ||
// return false; | ||
// } | ||
|
||
// let sArr = s.split("").sort(); | ||
// let tArr = t.split("").sort(); | ||
|
||
// for (let i = 0; i < sArr.length; i++) { | ||
// if (sArr[i] !== tArr[i]) { | ||
// return false; | ||
// } | ||
// } | ||
|
||
// return true; | ||
|
||
// }; | ||
|
Oops, something went wrong.
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를 사용하시면 시간 복잡도를 O(n) 으로 줄일 수 있을것 같아요 :)