Skip to content

[HerrineKim] Week 5 #879

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 4 commits 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
24 changes: 24 additions & 0 deletions best-time-to-buy-and-sell-stock/HerrineKim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 시간복잡도: O(n)
// 공간복잡도: O(1)

// 최소값을 계속 갱신하면서 최대 이익을 계산

/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
let minPrice = Infinity;
let maxProfit = 0;

for (let price of prices) {
if (price < minPrice) {
minPrice = price;
} else {
maxProfit = Math.max(maxProfit, price - minPrice);
}
}

return maxProfit;
};

25 changes: 25 additions & 0 deletions group-anagrams/HerrineKim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 시간복잡도: O(n * m log m)
// 공간복잡도: O(n)

// HashMap 사용
// 각 문자열을 정렬하여 키로 사용
// 정렬된 문자열을 키로 사용하여 그룹화

/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function (strs) {
const map = {};

for (const str of strs) {
const key = str.split('').sort().join('');
if (!map[key]) {
map[key] = [];
}
Comment on lines +16 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

요 부분을 개선하면 시간복잡도를 개선할 수 있을 거 같은데, 한 번 고민해보셔도 좋을 듯 합니다 😃

map[key].push(str);
}

return Object.values(map);
};

Loading