-
-
Notifications
You must be signed in to change notification settings - Fork 340
[sadie100] WEEK 05 Solutions #2499
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
+51
−0
Merged
Changes from all commits
Commits
Show all changes
2 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,21 @@ | ||
| /* | ||
| prices를 순회하며 현재 최저값과의 차이를 구해서 최대 profit을 갱신한 뒤 최저값(구매가)를 갱신, 최종값을 리턴한다 | ||
|
|
||
| 시간복잡도 O(N) - N은 prices의 length | ||
| */ | ||
|
|
||
| function maxProfit(prices: number[]): number { | ||
| let buy | ||
| let result = 0 | ||
|
|
||
| for (let price of prices) { | ||
| if (buy === undefined) { | ||
| buy = price | ||
| continue | ||
| } | ||
| result = Math.max(result, price - buy) | ||
| buy = Math.min(price, buy) | ||
| } | ||
|
|
||
| return result | ||
| } |
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
|
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 @@ | ||
| /* | ||
| strs의 문자열들을 등장빈도를 바탕으로 계산한 특수 문자열로 전환하고 같은 것끼리 묶는다. | ||
|
|
||
| 시간복잡도 : O(N * K) - N은 strs의 개수, K는 strs의 길이. N 순회 안에 K 순회 | ||
| */ | ||
|
|
||
| function groupAnagrams(strs: string[]): string[][] { | ||
| const anagramMap = {} | ||
| const result = strs.reduce((acc, cur) => { | ||
| const charArray = new Array(26).fill(0) | ||
|
|
||
| for (let char of cur) { | ||
| const charIdx = char.charCodeAt(0) - 'a'.charCodeAt(0) | ||
| charArray[charIdx] += 1 | ||
| } | ||
|
|
||
| const sortedValue = charArray.join('#') | ||
|
|
||
| const targetIdx = anagramMap[sortedValue] | ||
| if (targetIdx !== undefined) { | ||
| acc[targetIdx].push(cur) | ||
| } else { | ||
| anagramMap[sortedValue] = acc.length | ||
| acc.push([cur]) | ||
| } | ||
| return acc | ||
| }, []) | ||
|
|
||
| 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.
🏷️ 알고리즘 패턴 분석