-
-
Notifications
You must be signed in to change notification settings - Fork 245
[이병현] Week 5 #439
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
[이병현] Week 5 #439
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2b072ce
solve: best time to by and sell stock
tolluset c7fb410
solve: best time to by and sell stock 2
tolluset 5d9fc46
solve: group anagrams
tolluset d87dac3
solve: 3 sum
tolluset 808247f
refactor: use map
tolluset 3ae37aa
solve: implement trie prefix tree
tolluset 3f79d02
solve: word break
tolluset b773d4e
fix: adjust sc
tolluset 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,56 @@ | ||
/* | ||
* TC: O(n^2) | ||
* SC: O(n) | ||
* */ | ||
function threeSum(nums: number[]): number[][] { | ||
const n = nums.length; | ||
const res: number[][] = []; | ||
|
||
nums.sort((a, b) => a - b); | ||
|
||
for (let i = 0; i < n - 2; i++) { | ||
if (i > 0 && nums[i] === nums[i - 1]) { | ||
continue; | ||
} | ||
|
||
let left = i + 1, | ||
right = n - 1; | ||
|
||
while (left < right) { | ||
const sum = nums[i] + nums[left] + nums[right]; | ||
|
||
if (sum === 0) { | ||
res.push([nums[i], nums[left], nums[right]]); | ||
|
||
while (nums[left] === nums[left + 1]) { | ||
left++; | ||
} | ||
|
||
while (nums[right] === nums[right - 1]) { | ||
right++; | ||
} | ||
|
||
left++; | ||
right--; | ||
|
||
continue; | ||
} | ||
|
||
if (sum < 0) { | ||
left++; | ||
|
||
continue; | ||
} | ||
|
||
right--; | ||
} | ||
} | ||
|
||
return res; | ||
} | ||
|
||
const tc1 = threeSum([-1, 0, 1, 2, -1, -4]); // [[-1,-1,2],[-1,0,1]] | ||
console.info("🚀 : tolluset.ts:39: tc1=", tc1); | ||
|
||
const tc2 = threeSum([0, 0, 0]); // [[0,0,0]] | ||
console.info("🚀 : tolluset.ts:42: tc2=", tc2); |
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,63 @@ | ||
/* | ||
* TC: O(n) | ||
* SC: O(1) | ||
* */ | ||
function maxProfitV2(prices: number[]): number { | ||
const n = prices.length; | ||
|
||
let min = Infinity, | ||
max = 0; | ||
|
||
for (let i = 0; i < n; i++) { | ||
if (prices[i] < min) { | ||
min = prices[i]; | ||
continue; | ||
} | ||
|
||
if (prices[i] - min > max) { | ||
max = prices[i] - min; | ||
continue; | ||
} | ||
} | ||
|
||
return max; | ||
} | ||
|
||
const tc1V2 = maxProfitV2([7, 1, 5, 3, 6, 4]); | ||
console.info("🚀 : tolluset.ts:27: tc1V2=", tc1V2); // 5 | ||
|
||
const tc2V2 = maxProfitV2([7, 6, 4, 3, 1]); | ||
console.info("🚀 : tolluset.ts:30: tc2V2=", tc2V2); // 0 | ||
|
||
/* | ||
* @FAILED: Time Limit Exceeded | ||
* TC: O(n^2) | ||
* SC: O(1) | ||
* */ | ||
function maxProfit(prices: number[]): number { | ||
const n = prices.length; | ||
|
||
let max = 0; | ||
|
||
for (let i = 0; i < n; i++) { | ||
let currentMax = 0; | ||
|
||
for (let j = i + 1; j < n; j++) { | ||
if (prices[i] <= prices[j]) { | ||
const profit = prices[j] - prices[i]; | ||
|
||
currentMax = Math.max(currentMax, profit); | ||
} | ||
} | ||
|
||
max = Math.max(max, currentMax); | ||
} | ||
|
||
return max; | ||
} | ||
|
||
const tc1 = maxProfit([7, 1, 5, 3, 6, 4]); | ||
console.info("🚀 : tolluset.ts:5: tc1=", tc1); // 5 | ||
|
||
const tc2 = maxProfit([7, 6, 4, 3, 1]); | ||
console.info("🚀 : tolluset.ts:8: tc2=", tc2); // 0 |
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,72 @@ | ||
/* n: strs.length, m: strs.mean | ||
* TC: O(n * m * logm) | ||
* SC: O(n * m) | ||
* */ | ||
function groupAnagramsV2(strs: string[]): string[][] { | ||
const map = new Map<string, string[]>(); | ||
|
||
const strSort = (str: string) => str.split("").sort().join(""); | ||
|
||
for (const str of strs) { | ||
const sortedStr = strSort(str); | ||
|
||
if (map.has(sortedStr)) { | ||
map.get(sortedStr)!.push(str); | ||
} else { | ||
map.set(sortedStr, [str]); | ||
} | ||
} | ||
|
||
return Array.from(map.values()); | ||
} | ||
|
||
const tc1V2 = groupAnagramsV2(["eat", "tea", "tan", "ate", "nat", "bat"]); // [["bat"],["nat","tan"],["ate","eat","tea"]] | ||
console.info("🚀 : tolluset.ts:19: tc1V2=", tc1V2); | ||
|
||
/** | ||
* @FAILED - Time Limit Exceeded | ||
* TC: O(n^2) | ||
* SC: O(n) | ||
*/ | ||
function groupAnagrams(strs: string[]): string[][] { | ||
const n = strs.length; | ||
|
||
const res: string[][] = []; | ||
|
||
const strSort = (str: string) => str.split("").sort().join(""); | ||
|
||
for (let i = 0; i < n; i++) { | ||
const bucket: string[] = []; | ||
const cur = strs[i]; | ||
|
||
if (cur === "#") { | ||
continue; | ||
} | ||
|
||
bucket.push(cur); | ||
|
||
const sortedCur = strSort(cur); | ||
|
||
for (let j = i + 1; j < n; j++) { | ||
const tmpSortedStr = strSort(strs[j]); | ||
|
||
if (tmpSortedStr === "#") { | ||
continue; | ||
} | ||
|
||
if (sortedCur === tmpSortedStr) { | ||
bucket.push(strs[j]); | ||
strs[j] = "#"; | ||
} | ||
} | ||
|
||
strs[i] = "#"; | ||
|
||
res.push(bucket); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
const tc1 = groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]); // [["bat"],["nat","tan"],["ate","eat","tea"]] | ||
console.info("🚀 : tolluset.ts:7: tc1=", tc1); |
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,97 @@ | ||
class TrieNode { | ||
public children: Map<string, TrieNode>; | ||
public isEnd: boolean; | ||
|
||
constructor() { | ||
this.children = new Map(); | ||
this.isEnd = false; | ||
} | ||
} | ||
|
||
class Trie { | ||
private root: TrieNode; | ||
|
||
constructor() { | ||
this.root = new TrieNode(); | ||
} | ||
|
||
/** | ||
* TC: O(n) | ||
* SC: O(n) | ||
* */ | ||
insert(word: string): void { | ||
let node = this.root; | ||
|
||
for (const char of word) { | ||
if (!node.children.has(char)) { | ||
node.children.set(char, new TrieNode()); | ||
} | ||
|
||
node = node.children.get(char)!; | ||
} | ||
|
||
node.isEnd = true; | ||
} | ||
|
||
/** | ||
* TC: O(n) | ||
* SC: O(n) | ||
* */ | ||
search(word: string): boolean { | ||
let node = this.root; | ||
|
||
for (const char of word) { | ||
if (!node.children.has(char)) { | ||
return false; | ||
} | ||
|
||
node = node.children.get(char)!; | ||
} | ||
|
||
return node.isEnd; | ||
} | ||
|
||
/** | ||
* TC: O(n) | ||
* SC: O(n) | ||
tolluset marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
* */ | ||
startsWith(prefix: string): boolean { | ||
let node = this.root; | ||
|
||
for (const char of prefix) { | ||
if (!node.children.has(char)) { | ||
return false; | ||
} | ||
|
||
node = node.children.get(char)!; | ||
} | ||
|
||
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) | ||
*/ | ||
|
||
const trie = new Trie(); | ||
|
||
trie.insert("apple"); | ||
|
||
const tc1 = trie.search("apple"); // return True | ||
console.info("🚀 : tolluset.ts:59: tc1=", tc1); | ||
|
||
const tc2 = trie.search("app"); // return False | ||
console.info("🚀 : tolluset.ts:61: tc2=", tc2); | ||
|
||
const tc3 = trie.startsWith("app"); // return True | ||
console.info("🚀 : tolluset.ts:63: tc3=", tc3); | ||
|
||
trie.insert("app"); | ||
|
||
const tc4 = trie.search("app"); // return True | ||
console.info("🚀 : tolluset.ts:66: tc4=", tc4); |
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,40 @@ | ||
/* | ||
* TC: O(n^2) | ||
* SC: O(n) | ||
* */ | ||
function wordBreak(s: string, wordDict: string[]): boolean { | ||
const n = s.length; | ||
const wordSet = new Set(wordDict); | ||
const dp = Array(n + 1).fill(false); | ||
|
||
dp[0] = true; | ||
|
||
for (let i = 1; i <= n; i++) { | ||
for (let j = 0; j < i; j++) { | ||
if (dp[j] && wordSet.has(s.slice(j, i))) { | ||
dp[i] = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return dp[n]; | ||
} | ||
|
||
const tc1 = wordBreak("leetcode", ["leet", "code"]); // true | ||
console.info("🚀 : tolluset.ts:17: tc1=", tc1); | ||
|
||
const tc2 = wordBreak("applepenapple", ["apple", "pen"]); // true | ||
console.info("🚀 : tolluset.ts:20: tc2=", tc2); | ||
|
||
const tc3 = wordBreak("catsandog", ["cats", "dog", "sand", "and", "cat"]); // false | ||
console.info("🚀 : tolluset.ts:23: tc3=", tc3); | ||
|
||
const tc4 = wordBreak("cars", ["car", "ca", "rs"]); // true | ||
console.info("🚀 : tolluset.ts:27: tc4=", tc4); | ||
|
||
const tc5 = wordBreak("aaaaaaa", ["aaaa", "aaa"]); // true | ||
console.info("🚀 : tolluset.ts:32: tc5=", tc5); | ||
|
||
const tc6 = wordBreak("cbca", ["bc", "ca"]); // false | ||
console.info("🚀 : tolluset.ts:43: tc6=", tc6); |
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.