Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 27 additions & 0 deletions best-time-to-buy-and-sell-stock/hyejjun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
let minPrice = Infinity;
let maxProfit = 0;

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

return maxProfit;

};

console.log(maxProfit([7, 1, 5, 3, 6, 4]));
console.log(maxProfit([7, 6, 4, 3, 1]));

/*
시간 복잡도: O(n)
공간 복잡도: O(1)
*/
29 changes: 29 additions & 0 deletions group-anagrams/hyejjun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function (strs) {
let map = new Map();

for (let str of strs) {
let sortedStr = str.split('').sort().join('');

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

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

console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]));
console.log(groupAnagrams([""]));
console.log(groupAnagrams(["a"]));


/*
시간 복잡도: O(n*k log k)
공간 복잡도: O(n*k)
*/
Copy link
Member

@sounmind sounmind Sep 11, 2024

Choose a reason for hiding this comment

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

안녕하세요 혜준님!
시간, 공간 복잡도를 이야기할 때 n이나 k가 무엇을 가리키는지 명시하면 더 알고리즘을 이해하기 수월할 것 같아요.

Copy link
Contributor

Choose a reason for hiding this comment

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

오... 각 값을 정렬 후 각 key, value값으로 사용 하니 이렇게 간단히 풀리는군요.
Array.from() 도 처음보네요.
공부가 되었습니다 고생하셨습니다!