-
-
Notifications
You must be signed in to change notification settings - Fork 247
[solbijae] WEEK 02 solutions #1742
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,16 @@ | ||
function climbStairs(n: number): number { | ||
if (n <= 3) return n; | ||
|
||
// 첫 시도: 시간 복잡도 O(2^n), 공간 복잡도 O(n) | ||
// return climbStairs(n - 1) + climbStairs(n - 2); | ||
|
||
// 두번째 시도: 시간 복잡도 O(n), 공간 복잡도 O(1) | ||
let prev1 = 2, prev2 = 1; | ||
for (let i = 3; i <= n; i++) { | ||
const curr = prev1 + prev2; | ||
prev2 = prev1; | ||
prev1 = curr; | ||
} | ||
|
||
return prev1; | ||
}; |
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 @@ | ||
function productExceptSelf(nums: number[]): number[] { | ||
// 시간 복잡도: O(n), 공간 복잡도: O(1) | ||
const n = nums.length; | ||
const answer = new Array(n).fill(1); | ||
|
||
// answer[i]에 left 곱 저장 | ||
for (let i = 1; i < n; i++) { | ||
answer[i] = answer[i - 1] * nums[i - 1]; | ||
} | ||
|
||
// right 곱을 한 변수에 저장하면서 answer에 곱하기 | ||
let rightProduct = 1; | ||
for (let i = n - 1; i >= 0; i--) { | ||
answer[i] *= rightProduct; | ||
rightProduct *= 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,17 @@ | ||
function isAnagram(s: string, t: string): boolean { | ||
// 첫 시도: 시간 복잡도 O(n log n), 공간 복잡도 O(n) | ||
// const sSort = s.split('').sort(); | ||
// const tSort = t.split('').sort(); | ||
// return JSON.stringify(sSort) === JSON.stringify(tSort); | ||
|
||
// 시간 복잡도 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); | ||
}; |
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.
Uh oh!
There was an error while loading. Please reload this page.