-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #442 from HC-kang/main
[강희찬] WEEK 5 Solution
- Loading branch information
Showing
5 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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]; | ||
} |