-
-
Notifications
You must be signed in to change notification settings - Fork 194
[ganu] Week 5 #845
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
[ganu] Week 5 #845
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cc061c9
feat: 121. Best Time to Buy and Sell Stock
gwbaik9717 040d1c5
feat: 49. Group Anagrams
gwbaik9717 1e90ed0
refactor: 49. Group Anagrams
gwbaik9717 1ce40c1
139. Word Break
gwbaik9717 a302c0f
208. Implement Trie (Prefix Tree)
gwbaik9717 8f6c82f
feat: 659.encode-and-decode-strings
gwbaik9717 e4aaeab
refactor: 49. Group Anagrams
gwbaik9717 2d16acd
refactor: 208. Implement Trie (Prefix Tree)
gwbaik9717 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,18 @@ | ||
// Time complexity: O(n) | ||
// Space complexity: O(1) | ||
|
||
/** | ||
* @param {number[]} prices | ||
* @return {number} | ||
*/ | ||
var maxProfit = function (prices) { | ||
let answer = 0; | ||
let minValue = Number.MAX_SAFE_INTEGER; | ||
|
||
for (const price of prices) { | ||
minValue = Math.min(minValue, price); | ||
answer = Math.max(answer, price - minValue); | ||
} | ||
|
||
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,43 @@ | ||
// n: length of strs, m: length of strs[i] | ||
// Time complexity: O(nm) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* @param {string[]} strs | ||
* @return {string[][]} | ||
*/ | ||
var groupAnagrams = function (strs) { | ||
const answer = []; | ||
const anagramDict = new Map(); | ||
|
||
const getKey = (str) => { | ||
const counter = Array.from( | ||
{ length: "z".charCodeAt() - "a".charCodeAt() + 1 }, | ||
() => 0 | ||
); | ||
|
||
for (const chr of str) { | ||
const index = chr.charCodeAt() - "a".charCodeAt(); | ||
counter[index]++; | ||
} | ||
|
||
return counter.join("#"); | ||
}; | ||
|
||
for (let i = 0; i < strs.length; i++) { | ||
const str = strs[i]; | ||
const key = getKey(str); | ||
|
||
if (!anagramDict.has(key)) { | ||
anagramDict.set(key, []); | ||
} | ||
|
||
anagramDict.get(key).push(str); | ||
} | ||
|
||
for (const [_, value] of anagramDict) { | ||
answer.push(value); | ||
} | ||
|
||
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,70 @@ | ||
var Node = function () { | ||
this.children = new Map(); | ||
this.isEnd = false; | ||
}; | ||
|
||
var Trie = function () { | ||
this.head = new Node(); | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {void} | ||
*/ | ||
Trie.prototype.insert = function (word) { | ||
let current = this.head; | ||
|
||
for (const chr of word) { | ||
if (!current.children.has(chr)) { | ||
current.children.set(chr, new Node()); | ||
} | ||
|
||
current = current.children.get(chr); | ||
} | ||
|
||
current.isEnd = true; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.search = function (word) { | ||
let current = this.head; | ||
|
||
for (const chr of word) { | ||
gwbaik9717 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!current.children.has(chr)) { | ||
return false; | ||
} | ||
|
||
current = current.children.get(chr); | ||
} | ||
|
||
return current.isEnd; | ||
}; | ||
|
||
/** | ||
* @param {string} prefix | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.startsWith = function (prefix) { | ||
let current = this.head; | ||
|
||
for (const chr of prefix) { | ||
if (!current.children.has(chr)) { | ||
return false; | ||
} | ||
|
||
current = current.children.get(chr); | ||
} | ||
|
||
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,25 @@ | ||
// n: len(s), m: len(wordDict) | ||
// Time complexity: O(n^2*m) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* @param {string} s | ||
* @param {string[]} wordDict | ||
* @return {boolean} | ||
*/ | ||
var wordBreak = function (s, wordDict) { | ||
const dp = Array.from({ length: s.length + 1 }, () => false); | ||
dp[0] = true; | ||
|
||
for (let i = 1; i <= s.length; i++) { | ||
for (const word of wordDict) { | ||
const sliced = s.slice(i - word.length, i); | ||
|
||
if (word === sliced && !dp[i]) { | ||
dp[i] = dp[i - word.length]; | ||
} | ||
} | ||
} | ||
|
||
return dp.at(-1); | ||
}; |
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.
Uh oh!
There was an error while loading. Please reload this page.