Skip to content
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

[강희찬] WEEK 5 Solution #442

Merged
merged 5 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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--;
Comment on lines +22 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

중복 제거를 직접 해줌으로써 집합을 안 쓰고 배열로만 해결할 수 있어서 좋네요! 👍


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];
}