Skip to content

[khyo] Week 5 #880

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 1 commit into from
Jan 12, 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
16 changes: 16 additions & 0 deletions best-time-to-buy-and-sell-stock/higeuni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
let answer = 0
let leftMin = prices[0]

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

return answer;
};

62 changes: 62 additions & 0 deletions group-anagrams/higeuni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @param {string[]} strs
* @return {string[][]}
*
* complexity
* time: O(n * m)
* space: O(n)
*
* 풀이
* 처음에는 각 알파벳에 숫자를 할당하여 합을 이용해서 풀이하는 방식으로 접근하려고 했으나,
* 합의 경우의 수가 너무 많아서 중복되는 경우가 생겨서 다른 풀이 방식을 생각했다.
* -> 이후 소수를 이용한 풀이를 생각했다. (소수의 곱을 이용하는 경우 중복되는 경우가 없다.)
* 하지만 최악의 경우 소수의 곱이 너무 커져서 오버플로우가 발생한다.
*
* 그래서 정렬을 통해 각 문자열을 정렬하여 키로 사용하는 방식으로 접근했다.
*/
var groupAnagrams = function(strs) {
const map = new Map();

for(const str of strs) {
const sortedStr = str.split('').sort().join('');
Copy link
Contributor

@EgonD3V EgonD3V Jan 12, 2025

Choose a reason for hiding this comment

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

이 라인이 시간복잡도 측면에서 핵심인 것 같은데, 다른 방식으로 key를 생성하면 시간복잡도를 줄일 수 있을 것 같습니다. 예를 들어 알파벳이니까 굳이 정렬을 안해도 a-z의 빈도를 나타내는 문자열로 변환하거나 할 수 있을 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

피드백 감사합니다!


if(!map.has(sortedStr)) {
map.set(sortedStr, []);
}
map.get(sortedStr).push(str);
}

return Array.from(map.values());
};

/**
* passed 되었으나, 최악의 경우에는 통과될 수 없지 않을까?
*
* @param {string[]} strs
* @return {string[][]}
*
* complexity
* time: O(n * m)
* space: O(n)
*/
var groupAnagrams = function(strs) {
const map = new Map();

const primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,
43,47,53,59,61,67,71,73,79,83,89,97,101];

for(const str of strs) {
let key = 1;
for(const char of str) {
key *= primes[char.charCodeAt(0) - 'a'.charCodeAt(0)];
}

if(!map.has(key)) {
map.set(key, []);
}
map.get(key).push(str);
}

return Array.from(map.values());
};

63 changes: 63 additions & 0 deletions implement-trie-prefix-tree/higeuni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var Trie = function() {
this.root = {};
};

/**
* @param {string} word
* @return {void}
*
* complexity
* time: O(n)
* space: O(n)
*/
Trie.prototype.insert = function(word) {
let cur = this.root;
for (let x of word) {
if (!cur[x]) cur[x] = {};
cur = cur[x];
}
cur.end = true;
};

/**
* @param {string} word
* @return {boolean}
*
* complexity
* time: O(n)
* space: O(1)
*/
Trie.prototype.search = function(word) {
let cur = this.find(word);
return cur !== null && cur.end === true;
};

/**
* @param {string} prefix
* @return {boolean}
*
* complexity
* time: O(n)
* space: O(1)
*/
Trie.prototype.startsWith = function(prefix) {
return this.find(prefix) !== null;
};

/**
* @param {string} str
* @return {object}
*
* complexity
* time: O(n)
* space: O(1)
*/
Trie.prototype.find = function(str) {
let cur = this.root;
for (let x of str) {
if (!cur[x]) return null;
cur = cur[x];
}
return cur;
};

Loading