-
-
Notifications
You must be signed in to change notification settings - Fork 195
[choidabom] Week 5 #855
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
[choidabom] Week 5 #855
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,21 @@ | ||
/** | ||
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ | ||
* Runtime: 6ms, Memory: 62.30MB | ||
* | ||
* Time Complexity: O(N) | ||
* Space Complexity: O(1) | ||
*/ | ||
|
||
function maxProfit(prices: number[]): number { | ||
let buyingPrice = prices[0]; | ||
let profit = 0; | ||
|
||
for (const price of prices) { | ||
buyingPrice = Math.min(price, buyingPrice); | ||
profit = Math.max(profit, price - buyingPrice); | ||
} | ||
|
||
return profit; | ||
} | ||
|
||
maxProfit([7, 1, 5, 3, 6, 4]); |
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 @@ | ||
/** | ||
* https://www.lintcode.com/problem/659/ | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
|
||
function encode(strs: string[]): string { | ||
return strs.join("😀"); | ||
} | ||
|
||
function decode(str: string): string[] { | ||
return str.split("😀"); | ||
} | ||
|
||
decode(encode(["Hello", "World"])); | ||
/* | ||
* output | ||
* ["Hello","World"] | ||
*/ |
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,32 @@ | ||
/** | ||
* https://leetcode.com/problems/group-anagrams/description/ | ||
* Runtime: 27ms, Memory: 64.26MB | ||
* | ||
* Time Complexity: O(n * m * log m) | ||
* Space Complexity: O(n * m) | ||
* | ||
* 접근 | ||
* anagrams 관계인 단어는 정렬 시 모두 동일한 단어가 된다. | ||
* 그러므로 정렬된 단어가 key 값이 되고, str을 정렬했을 때 key값과 동일한 경우 value가 되도록 추가. | ||
*/ | ||
|
||
function groupAnagrams(strs: string[]): string[][] { | ||
let answer = []; | ||
const anagramMap = new Map<string, string[]>(); | ||
|
||
for (let str of strs) { | ||
const key = str.split("").sort().join(""); // O(m * log m) | ||
if (!anagramMap.has(key)) { | ||
anagramMap.set(key, []); | ||
} | ||
anagramMap.get(key)?.push(str); | ||
} | ||
|
||
anagramMap.forEach((value) => { | ||
answer.push(value); | ||
}); | ||
|
||
return answer; | ||
} | ||
|
||
groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]); |
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.
😄