Skip to content

[HoonDongKang] Week 5 Solutions #1384

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 5 commits into from
May 3, 2025
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
40 changes: 40 additions & 0 deletions best-time-to-buy-and-sell-stock/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* [Problem]: [121] Best Time to Buy and Sell Stock
*
* (https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
*/
function maxProfit(prices: number[]): number {
//시간복잡도: O(n^2);
//공간복잡도: O(1);
// Time Limit Exceeded
function doublyLoopFunc(prices: number[]): number {
let result = 0;
for (let i = 0; i < prices.length; i++) {
for (let j = i + 1; j < prices.length; j++) {
let profit = prices[j] - prices[i];
result = Math.max(profit, result);
}
}

return result;
}

// 시간 복잡도: O(n)
// 공간 복잡도: O(1)
function twoPointerFunc(prices: number[]): number {
let minPrice = prices[0];
let maxProfit = 0;

for (let i = 1; i < prices.length; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
} else {
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
}
}

return maxProfit;
}

return twoPointerFunc(prices);
}
37 changes: 37 additions & 0 deletions encode-and-decode-strings/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* [Problem]: [659] Encode and Decode Strings
*
* (https://www.lintcode.com/problem/659/)
*/

//시간복잡도 O(n)
//공간복잡도 O(1)

function encode(strs: string[]): string {
return strs.join("😄");
}
function decode(s: string): string[] {
return s.split("😄");
}

// 시간복잡도: O(n)
// 공간복잡도: O(n)
function encode(strs: string[]): string {
return strs.map((str) => `${str.length}:${str}`).join("");
}

// 시간복잡도: O(n)
// 공간복잡도: O(n)
function decode(str: string): string[] {
const result: string[] = [];
let index = 0;
while (index < str.length) {
const separatorIndex = str.indexOf(":", index);
const length = +str.slice(index, separatorIndex);
const endIndex = separatorIndex + 1 + length;

result.push(str.slice(separatorIndex + 1, endIndex));
index = endIndex;
}
return result;
}
40 changes: 40 additions & 0 deletions group-anagrams/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* [Problem]: [49] Group Anagrams
*
* (https://leetcode.com/problems/group-anagrams/description/)
*/
function groupAnagrams(strs: string[]): string[][] {
//시간복잡도 O(n * w log w)
//공간복잡도 O(n * w)
function mappedFunc(strs: string[]): string[][] {
const map = new Map<string, string[]>();

for (let str of strs) {
let sorted = [...str].sort().join("");

map.set(sorted, [...(map.get(sorted) ?? []), str]);
}

return [...map.values()];
}

//시간복잡오 O(n)
//공간복잡도 O(n * w)
function hashedMappedFunc(strs: string[]): string[][] {
const map = new Map<string, string[]>();

for (let str of strs) {
const count: number[] = new Array(26).fill(0);

for (const char of str) {
count[char.charCodeAt(0) - 97]++;
}

const key = count.join("#");
map.set(key, [...(map.get(key) ?? []), str]);
}

return [...map.values()];
}
return hashedMappedFunc(strs);
}
65 changes: 65 additions & 0 deletions implement-trie-prefix-tree/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* [Problem]: [208] Implement Trie (Prefix Tree)
* (https://leetcode.com/problems/implement-trie-prefix-tree/description/)
*/

class TrieNode {
children: { [key: string]: TrieNode };
isEnd: boolean;

constructor() {
this.children = {};
this.isEnd = false;
}
}

class Trie {
constructor(private root = new TrieNode()) {}

//시간복잡도: O(n)
//공간복잡도: O(n)
insert(word: string): void {
let node = this.root;
for (let char of word) {
if (!node.children[char]) {
node.children[char] = new TrieNode();
}

node = node.children[char];
}

node.isEnd = true;
}

//시간복잡도: O(n)
//공간복잡도: O(n)
search(word: string): boolean {
let node = this.root;
for (let char of word) {
if (!node.children[char]) return false;
node = node.children[char];
}

return node.isEnd;
}

//시간복잡도: O(n)
//공간복잡도: O(n)
startsWith(prefix: string): boolean {
let node = this.root;
for (let char of prefix) {
if (!node.children[char]) return false;
node = node.children[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)
*/
51 changes: 51 additions & 0 deletions word-break/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* [Problem]: [139] Word Break
* (https://leetcode.com/problems/word-break/description/)
*/
function wordBreak(s: string, wordDict: string[]): boolean {
//시간복잡도: O(n^2)
//공간복잡도: O(n)
function dfsFunc(s: string, wordDict: string[]): boolean {
let wordSet = new Set(wordDict);
let memo = new Map<number, boolean>();

function dfs(start: number): boolean {
if (start === s.length) return true;
if (memo.has(start)) return memo.get(start)!;

for (let end = start + 1; end <= s.length; end++) {
const word = s.slice(start, end);

if (wordSet.has(word) && dfs(end)) {
memo.set(start, true);
return true;
}
}

memo.set(start, false);
return false;
}

return dfs(0);
}

//시간복잡도: O(n^2)
//공간복잡도: O(n)
function dpFunc(s: string, wordDict: string[]): boolean {
const wordSet = new Set(wordDict);
const dp = new Array(s.length + 1).fill(false);
dp[0] = true;

for (let end = 1; end <= s.length; end++) {
for (let start = 0; start < end; start++) {
const isExists = wordSet.has(s.slice(start, end));
if (isExists && dp[start]) {
dp[end] = true;
break;
}
}
}
return dp[s.length];
}
return dpFunc(s, wordDict);
}