-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Jeehay28] Week05 Solutions #1381
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
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 @@ | ||
// Time Complexity: O(n), where n is the length of the prices array | ||
// Space Complexity: O(1) | ||
|
||
function maxProfit(prices: number[]): number { | ||
// input: an array prices, prices[i] = the stock price of ith day | ||
// output: the maximum profit || 0 | ||
|
||
// prices = [7, 1, 5, 3, 6, 4] | ||
// buy = 1, sell = 6, profit = 6 -1 = 5 | ||
|
||
let minBuy = prices[0]; | ||
let maxProfit = 0; | ||
|
||
for (let i = 1; i < prices.length; i++) { | ||
minBuy = Math.min(prices[i], minBuy); | ||
maxProfit = Math.max(prices[i] - minBuy, maxProfit); | ||
} | ||
|
||
return maxProfit; | ||
} | ||
|
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,62 @@ | ||
// Approach 2 | ||
// Time Complexity: O(n * L), where n = number of strings in strs, L = maximum length of a string in strs | ||
// Space Complexity: O(n * L) | ||
|
||
function groupAnagrams(strs: string[]): string[][] { | ||
|
||
let sorted: { [key: string]: string[] } = {} | ||
|
||
for (const str of strs) { | ||
|
||
const arr = Array.from({ length: 26 }, () => 0) | ||
|
||
for (const ch of str) { | ||
arr[ch.charCodeAt(0) - "a".charCodeAt(0)] += 1 | ||
} | ||
|
||
// const key = arr.join(""); // This can lead to key collisions. | ||
const key = arr.join("#"); // ✅ | ||
|
||
if (!sorted[key]) { | ||
sorted[key] = [] | ||
} | ||
|
||
sorted[key].push(str) | ||
} | ||
|
||
return Object.values(sorted); | ||
}; | ||
|
||
|
||
// Approach 1 | ||
// Time Complexity: O(n * LlogL), where n = number of strings, L = average length of the strings | ||
// Space Complexity: O(n * L) | ||
|
||
// function groupAnagrams(strs: string[]): string[][] { | ||
// // input: an array of strings, strs | ||
// // output: the anagrams | ||
|
||
// // eat -> e, a, t -> a, e, t | ||
// // tea -> t, e, a -> a, e, t | ||
// // ate -> a, t, e -> a, e, t | ||
|
||
// let map = new Map<string, string[]>(); | ||
// let result: string[][] = []; | ||
|
||
// for (const str of strs) { | ||
// const temp = str.split("").sort().join(""); | ||
|
||
// if (map.has(temp)) { | ||
// map.set(temp, [...map.get(temp)!, str]); | ||
// } else { | ||
// map.set(temp, [str]); | ||
// } | ||
// } | ||
|
||
// for (const el of map.values()) { | ||
// result.push(el); | ||
// } | ||
|
||
// 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,64 @@ | ||
// Time Complexity: O(n) | ||
|
||
class TrieNode { | ||
children: { [key: string]: TrieNode }; | ||
ending: boolean; | ||
|
||
constructor(ending = false) { | ||
this.children = {}; | ||
this.ending = ending; | ||
} | ||
} | ||
|
||
class Trie { | ||
root: TrieNode; | ||
|
||
constructor() { | ||
this.root = new TrieNode(); | ||
} | ||
|
||
insert(word: string): void { | ||
let node = this.root; | ||
|
||
for (const ch of word) { | ||
if (!(ch in node.children)) { | ||
node.children[ch] = new TrieNode(); | ||
} | ||
node = node.children[ch]; | ||
} | ||
node.ending = true; | ||
} | ||
|
||
search(word: string): boolean { | ||
let node = this.root; | ||
|
||
for (const ch of word) { | ||
if (!(ch in node.children)) { | ||
return false; | ||
} | ||
node = node.children[ch]; | ||
} | ||
return node.ending; | ||
} | ||
|
||
startsWith(prefix: string): boolean { | ||
let node = this.root; | ||
|
||
for (const ch of prefix) { | ||
if (!(ch in node.children)) { | ||
return false; | ||
} | ||
|
||
node = node.children[ch]; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* Your Trie object will be instantiated and called as such: | ||
* var obj = new Trie() | ||
* obj.insert(word) | ||
* var param_2 = obj.search(word) | ||
* var param_3 = obj.startsWith(prefix) | ||
*/ |
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.
이번 문제 해결하면서
Object.groupBy()
라는 메서드가 있다는 걸 알게 되어 공유드립니다. 혹시 처음 들어보셨다면 구경해보셔도 좋을 것 같아요! 참고로Map
에도 동명의 메서드가 있습니다.