Skip to content

Commit

Permalink
Merge pull request #442 from HC-kang/main
Browse files Browse the repository at this point in the history
[강희찬] WEEK 5 Solution
  • Loading branch information
HC-kang authored Sep 15, 2024
2 parents 7109b99 + 5cb3615 commit 62207e0
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 3sum/HC-kang.ts
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;
}
19 changes: 19 additions & 0 deletions best-time-to-buy-and-sell-stock/HC-kang.ts
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;
}
19 changes: 19 additions & 0 deletions group-anagrams/HC-kang.ts
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());
}
47 changes: 47 additions & 0 deletions implement-trie-prefix-tree/HC-kang.ts
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)
*/
23 changes: 23 additions & 0 deletions word-break/HC-kang.ts
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];
}

0 comments on commit 62207e0

Please sign in to comment.