Skip to content

[Jeehay28] Week05 Solutions #1381

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 3 commits into from
May 4, 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
21 changes: 21 additions & 0 deletions best-time-to-buy-and-sell-stock/Jeehay28.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Time Complexity: O(n), where n is the length of the prices array
// Space Complexity: O(1)

function maxProfit(prices: number[]): number {
// input: an array prices, prices[i] = the stock price of ith day
// output: the maximum profit || 0

// prices = [7, 1, 5, 3, 6, 4]
// buy = 1, sell = 6, profit = 6 -1 = 5

let minBuy = prices[0];
let maxProfit = 0;

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

return maxProfit;
}

62 changes: 62 additions & 0 deletions group-anagrams/Jeehay28.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

이번 문제 해결하면서 Object.groupBy()라는 메서드가 있다는 걸 알게 되어 공유드립니다. 혹시 처음 들어보셨다면 구경해보셔도 좋을 것 같아요! 참고로 Map에도 동명의 메서드가 있습니다.

function groupAnagrams(strs) {
  const result = Object.groupBy(strs, (str) => [...str].sort().join('')); // strs를 콜백함수에 따라 grouping
  return  Object.values(result);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Approach 2
// Time Complexity: O(n * L), where n = number of strings in strs, L = maximum length of a string in strs
// Space Complexity: O(n * L)

function groupAnagrams(strs: string[]): string[][] {

let sorted: { [key: string]: string[] } = {}

for (const str of strs) {

const arr = Array.from({ length: 26 }, () => 0)

for (const ch of str) {
arr[ch.charCodeAt(0) - "a".charCodeAt(0)] += 1
}

// const key = arr.join(""); // This can lead to key collisions.
const key = arr.join("#"); // ✅

if (!sorted[key]) {
sorted[key] = []
}

sorted[key].push(str)
}

return Object.values(sorted);
};


// Approach 1
// Time Complexity: O(n * LlogL), where n = number of strings, L = average length of the strings
// Space Complexity: O(n * L)

// function groupAnagrams(strs: string[]): string[][] {
// // input: an array of strings, strs
// // output: the anagrams

// // eat -> e, a, t -> a, e, t
// // tea -> t, e, a -> a, e, t
// // ate -> a, t, e -> a, e, t

// let map = new Map<string, string[]>();
// let result: string[][] = [];

// for (const str of strs) {
// const temp = str.split("").sort().join("");

// if (map.has(temp)) {
// map.set(temp, [...map.get(temp)!, str]);
// } else {
// map.set(temp, [str]);
// }
// }

// for (const el of map.values()) {
// result.push(el);
// }

// return result;
// }

64 changes: 64 additions & 0 deletions implement-trie-prefix-tree/Jeehay28.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Time Complexity: O(n)

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

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

class Trie {
root: TrieNode;

constructor() {
this.root = new TrieNode();
}

insert(word: string): void {
let node = this.root;

for (const ch of word) {
if (!(ch in node.children)) {
node.children[ch] = new TrieNode();
}
node = node.children[ch];
}
node.ending = true;
}

search(word: string): boolean {
let node = this.root;

for (const ch of word) {
if (!(ch in node.children)) {
return false;
}
node = node.children[ch];
}
return node.ending;
}

startsWith(prefix: string): boolean {
let node = this.root;

for (const ch of prefix) {
if (!(ch in node.children)) {
return false;
}

node = node.children[ch];
}
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)
*/