-
-
Notifications
You must be signed in to change notification settings - Fork 195
[강희찬] WEEK 5 Solution #442
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
5 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,36 @@ | ||
/** | ||
* https://leetcode.com/problems/3sum/description | ||
* T.C: O(n^2) | ||
* S.C: O(1) | ||
*/ | ||
function threeSum(nums: number[]): number[][] { | ||
nums.sort((a, b) => a - b); | ||
|
||
const result: number[][] = []; | ||
|
||
for (let i = 0; i < nums.length - 2; i++) { // O(n) | ||
if (i > 0 && nums[i] == nums[i - 1]) continue; | ||
|
||
let left = i + 1; | ||
let right = nums.length - 1; | ||
|
||
while (left < right) { // O(n) | ||
const sum = nums[i] + nums[left] + nums[right]; | ||
if (sum == 0) { | ||
result.push([nums[i], nums[left], nums[right]]); | ||
|
||
while (left < right && nums[left] == nums[left + 1]) left++; | ||
while (left < right && nums[right] == nums[right - 1]) right--; | ||
|
||
left++; | ||
right--; | ||
} else if (sum < 0) { | ||
left++; | ||
} else { | ||
right--; | ||
} | ||
} | ||
} | ||
|
||
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,19 @@ | ||
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ | ||
// T.C: O(N) | ||
// S.C: O(1) | ||
function maxProfit(prices: number[]): number { | ||
let min = prices[0]; | ||
let max = prices[0]; | ||
let candidate = prices[0]; | ||
|
||
for (let i = 1; i < prices.length; i++) { | ||
if (prices[i] < candidate) { | ||
candidate = prices[i]; | ||
} else if (prices[i] - candidate > max - min) { | ||
min = candidate; | ||
max = prices[i]; | ||
} | ||
} | ||
|
||
return max - min; | ||
} |
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://leetcode.com/problems/group-anagrams/description | ||
* T.C: O(n * m * log(m)), n = strs.length, m = max(strs[i].length) | ||
* S.C: O(n * m) | ||
*/ | ||
function groupAnagrams(strs: string[]): string[][] { | ||
const map = new Map<string, string[]>(); | ||
|
||
for (const str of strs) { // O(n) | ||
const sorted = str.split('').sort().join(''); // O(m * log(m)) | ||
|
||
if (map.has(sorted)) | ||
map.get(sorted)!.push(str); | ||
else | ||
map.set(sorted, [str]); | ||
} | ||
|
||
return Array.from(map.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,47 @@ | ||
/** | ||
* https://leetcode.com/problems/implement-trie-prefix-tree | ||
*/ | ||
class Trie { | ||
constructor(private root: Record<string, any> = {}) {} | ||
|
||
insert(word: string): void { | ||
let node = this.root; | ||
for (const char of word) { | ||
if (!node[char]) { | ||
node[char] = {}; | ||
} | ||
node = node[char]; | ||
} | ||
node.isEnd = true; | ||
} | ||
|
||
search(word: string): boolean { | ||
let node = this.root; | ||
for (const char of word) { | ||
if (!node[char]) { | ||
return false; | ||
} | ||
node = node[char]; | ||
} | ||
return !!node.isEnd; | ||
} | ||
|
||
startsWith(prefix: string): boolean { | ||
let node = this.root; | ||
for (const char of prefix) { | ||
if (!node[char]) { | ||
return false; | ||
} | ||
node = node[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) | ||
*/ |
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,23 @@ | ||
/** | ||
* https://leetcode.com/problems/word-break | ||
* T.C. O(s^2) | ||
* S.C. O(s + w) | ||
*/ | ||
function wordBreak(s: string, wordDict: string[]): boolean { | ||
const wordSet = new Set(wordDict); | ||
|
||
const dp = new Array(s.length + 1).fill(false); | ||
dp[0] = true; | ||
|
||
for (let i = 1; i <= s.length; i++) { | ||
for (let j = 0; j < i; j++) { | ||
if (!dp[j]) continue; | ||
if (j-i > 20) break; | ||
if (wordSet.has(s.slice(j, i))) { | ||
dp[i] = true; | ||
break; | ||
} | ||
} | ||
} | ||
return dp[s.length]; | ||
} |
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.
중복 제거를 직접 해줌으로써 집합을 안 쓰고 배열로만 해결할 수 있어서 좋네요! 👍