-
Notifications
You must be signed in to change notification settings - Fork 126
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
[YeomChaeeun] Week 3 #774
Merged
Merged
[YeomChaeeun] Week 3 #774
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 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 @@ | ||
/** | ||
* 연속되는 서브 배열로 최대 합을 구하기 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n) | ||
* - 공간 복잡도: O(1) | ||
* @param nums | ||
*/ | ||
function maxSubArray(nums: number[]): number { | ||
if(nums.length === 1) return nums[0] | ||
|
||
let currentSum = nums[0] | ||
let maxSum = nums[0] | ||
for(let i = 1; i < nums.length; i++) { | ||
currentSum = Math.max(nums[i], currentSum + nums[i]) | ||
// 최대값 갱신 | ||
maxSum = Math.max(maxSum, currentSum) | ||
} | ||
|
||
return maxSum | ||
} |
This file contains 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,47 @@ | ||
/** | ||
* 본인 인덱스에 있는 값을 제외한 모든 수의 곱 | ||
* 알고리즘 복잡도 | ||
* - 시간복잡도: O(n) | ||
* - 공간복잡도: O(1) | ||
* @param nums | ||
*/ | ||
function productExceptSelf(nums: number[]): number[] { | ||
let len = nums.length | ||
let output = Array(len).fill(1) | ||
|
||
/* ex) [1, 2, 3, 4] | ||
left >>> | ||
i = 0 -> 1 = 1 | ||
i = 1 -> 1 * 1 = 1 | ||
i = 2 -> 1 * 1 * 2 = 2 | ||
i = 3 -> 1 * 1 * 2 * 3 = 6 | ||
*/ | ||
// 왼쪽부터 누적 곱 | ||
let left = 1 | ||
for (let i = 0; i < len; i++) { | ||
output[i] *= left | ||
left *= nums[i] | ||
} | ||
|
||
/* | ||
right >>> | ||
i = 3 -> 1 = 1 | ||
i = 2 -> 1 * 4 = 4 | ||
i = 1 -> 1 * 4 * 3 = 12 | ||
i = 3 -> 1 * 4 * 3 * 2 = 24 | ||
|
||
output >>> | ||
i = 0 -> 1 * 24 = 24 | ||
i = 1 -> 1 * 12 = 12 | ||
i = 2 -> 2 * 4= 8 | ||
i = 3 -> 6 * 1 = 6 | ||
*/ | ||
// 오른쪽부터 누적 곱을 output 각 자리에 곱함 | ||
let right = 1 | ||
for (let i = len - 1; i >= 0; i--) { | ||
output[i] *= right | ||
right *= nums[i] | ||
} | ||
|
||
return output | ||
} |
This file contains 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 @@ | ||
/** | ||
* 정수를 비트로 변환후 뒤집어서 다시 정수로 반환 | ||
* 알고리즘 복잡도 | ||
* - 시간복잡도: O(1) | ||
* - 공간복잡도: O(1) | ||
* @param n | ||
*/ | ||
function reverseBits(n: number): number { | ||
// 2진수 배열로 변환 | ||
let arr = n.toString(2).split('') | ||
let len = arr.length | ||
// 32비트 정렬 - 부족한 앞쪽에 0으로 채움 | ||
for (let i = 0; i < (32 - len); i++) { | ||
arr.unshift('0'); | ||
} | ||
// 뒤집은 후 합침 | ||
let result = arr.reverse().join('') | ||
// 2진수 정수로 변환하여 반환 | ||
return parseInt(result,2) | ||
} |
This file contains 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 @@ | ||
/** | ||
* 배열 두 수의 합이 target 과 같은 값 | ||
* 알고리즘 복잡도: | ||
* - 시간복잡도: O(n^2) | ||
* - 공간복잡도: O(1) | ||
* @param nums | ||
* @param target | ||
*/ | ||
function twoSum(nums: number[], target: number): number[] { | ||
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. 모든 경우의 수를 확인하는 방법으로 푸셨군요 :) 복잡도를 더 낮출 수 있는 방법이 없을 지 생각해보는건 어떨까요? |
||
let result: number[] = []; | ||
|
||
for(let i = 0; i < nums.length; i++) { | ||
for(let j = i + 1; j < nums.length; j++) { | ||
if(nums[i] + nums[j] === target) { | ||
result.push(i, j); | ||
return result; | ||
} | ||
} | ||
} | ||
} |
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.
요 함수의 시간복잡도가 어떻게될까요? 시간복잡도 계산시에 언어에서 제공하는 함수의 시간복잡도도 고려해보시면 좋을 것 같아요!
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.
안녕하세요! javaScript toString() 메서드에 대해 찾아보았는데요!
숫자인 경우 - O(log n), 배열 또는 객체인 경우 - O(n) 로 처리되는걸 알게되었습니다!
내장 함수에 대한 복잡도는 크게 고려하지 않았었는데 알려주셔서 감사합니다!😊