-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hyer0705] WEEK 05 solutions #1848
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
6 commits
Select commit
Hold shift + click to select a range
1ad5d21
best time to buy and sell stock solution
hyer0705 635eff6
group anagrams solution
hyer0705 88c4d77
encode and decode strings solution
hyer0705 050a6b2
implement trie (prefix tree) solution
hyer0705 064d73a
word break solution
hyer0705 b5fc3ec
word break solution
hyer0705 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,14 @@ | ||
function maxProfit(prices: number[]): number { | ||
let minPrice = Infinity; | ||
let maxProfit = 0; | ||
|
||
for (const price of prices) { | ||
if (price < minPrice) { | ||
minPrice = price; | ||
} else { | ||
maxProfit = Math.max(maxProfit, price - minPrice); | ||
} | ||
} | ||
|
||
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,47 @@ | ||
class Solution { | ||
private DELIMITER = "*"; | ||
|
||
/** | ||
* @param strs: a list of strings | ||
* @returns: encodes a list of strings to a single string. | ||
*/ | ||
public encode(strs: string[]): string { | ||
let encoded = ""; | ||
|
||
for (const str of strs) { | ||
encoded += `${str.length}${this.DELIMITER}${str}`; | ||
} | ||
|
||
return encoded; | ||
} | ||
|
||
/** | ||
* @param str: A string | ||
* @returns: decodes a single string to a list of strings | ||
*/ | ||
public decode(str: string): string[] { | ||
const decoded: string[] = []; | ||
|
||
let stack: string[] = []; | ||
let pointer = 0; | ||
|
||
while (pointer < str.length) { | ||
const char = str[pointer]; | ||
|
||
if (char === this.DELIMITER) { | ||
let strLength: number = Number(stack.join("")); | ||
stack = []; | ||
|
||
const word = str.substring(pointer + 1, pointer + 1 + strLength); | ||
pointer = pointer + 1 + strLength; | ||
|
||
decoded.push(word); | ||
} else { | ||
stack.push(char); | ||
pointer++; | ||
} | ||
} | ||
|
||
return decoded; | ||
} | ||
} |
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,15 @@ | ||
function groupAnagrams(strs: string[]): string[][] { | ||
const anagramsMap = new Map<string, string[]>(); | ||
|
||
for (const word of strs) { | ||
const anagramKey = [...word].sort().join(); | ||
|
||
if (anagramsMap.has(anagramKey)) { | ||
anagramsMap.get(anagramKey)!.push(word); | ||
} else { | ||
anagramsMap.set(anagramKey, [word]); | ||
} | ||
} | ||
|
||
return Array.from(anagramsMap.values()); | ||
} |
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,69 @@ | ||
class TNode { | ||
isEndOf: boolean; | ||
children: Map<string, TNode>; | ||
|
||
constructor() { | ||
this.isEndOf = false; | ||
this.children = new Map<string, TNode>(); | ||
} | ||
} | ||
|
||
class Trie { | ||
private root: TNode; | ||
|
||
constructor() { | ||
this.root = new TNode(); | ||
} | ||
|
||
insert(word: string): void { | ||
let currentNode: TNode = this.root; | ||
|
||
for (const ch of word) { | ||
if (currentNode.children.has(ch)) { | ||
currentNode = currentNode.children.get(ch)!; | ||
} else { | ||
const newNode = new TNode(); | ||
|
||
currentNode.children.set(ch, newNode); | ||
currentNode = currentNode.children.get(ch)!; | ||
} | ||
} | ||
currentNode.isEndOf = true; | ||
} | ||
|
||
search(word: string): boolean { | ||
let currentNode = this.root; | ||
|
||
for (const ch of word) { | ||
if (!currentNode.children.has(ch)) { | ||
return false; | ||
} | ||
|
||
currentNode = currentNode.children.get(ch)!; | ||
} | ||
|
||
return currentNode.isEndOf; | ||
} | ||
|
||
startsWith(prefix: string): boolean { | ||
let currentNode = this.root; | ||
|
||
for (const ch of prefix) { | ||
if (!currentNode.children.has(ch)) { | ||
return false; | ||
} | ||
|
||
currentNode = currentNode.children.get(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) | ||
*/ |
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,93 @@ | ||
// using set | ||
function wordBreak(s: string, wordDict: string[]): boolean { | ||
const sLen = s.length; | ||
|
||
const dp: boolean[] = new Array(sLen + 1).fill(false); | ||
dp[0] = true; | ||
|
||
const wordSet = new Set<string>(wordDict); | ||
|
||
for (let i = 1; i <= sLen; i++) { | ||
for (let j = 0; j < i; j++) { | ||
if (dp[j] && wordSet.has(s.substring(j, i))) { | ||
dp[i] = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return dp[sLen]; | ||
} | ||
|
||
// using trie | ||
class TNode { | ||
isEndOf: boolean; | ||
children: Map<string, TNode>; | ||
|
||
constructor() { | ||
this.isEndOf = false; | ||
this.children = new Map<string, TNode>(); | ||
} | ||
} | ||
|
||
class Trie { | ||
root: TNode; | ||
|
||
constructor() { | ||
this.root = new TNode(); | ||
} | ||
|
||
insert(word: string): void { | ||
let currentNode = this.root; | ||
|
||
for (const ch of word) { | ||
if (!currentNode.children.has(ch)) { | ||
currentNode.children.set(ch, new TNode()); | ||
} | ||
|
||
currentNode = currentNode.children.get(ch)!; | ||
} | ||
|
||
currentNode.isEndOf = true; | ||
} | ||
|
||
search(word: string): boolean { | ||
let currentNode = this.root; | ||
|
||
for (const ch of word) { | ||
if (!currentNode.children.has(ch)) { | ||
return false; | ||
} | ||
|
||
currentNode = currentNode.children.get(ch)!; | ||
} | ||
|
||
return currentNode.isEndOf; | ||
} | ||
} | ||
|
||
function wordBreak(s: string, wordDict: string[]): boolean { | ||
const sLen = s.length; | ||
|
||
const dp: boolean[] = new Array(sLen + 1).fill(false); | ||
dp[0] = true; | ||
|
||
const trie = new Trie(); | ||
|
||
for (const word of wordDict) { | ||
trie.insert(word); | ||
} | ||
|
||
for (let i = 1; i <= sLen; i++) { | ||
for (let j = 0; j < i; j++) { | ||
if (dp[j]) { | ||
if (trie.search(s.substring(j, i))) { | ||
dp[i] = true; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return dp[sLen]; | ||
} |
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.
Trie랑 TNode class 때문에 그러신 건가요? 해당 문제를 trie로도 풀 수 있다기에 한 번 풀어봤습니다.