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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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);
}