Skip to content
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
28 changes: 28 additions & 0 deletions best-time-to-buy-and-sell-stock/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @description
* brainstorming:
* brute force
*
* time complexity: O(n)
* space complexity: O(1)
*/
var maxProfit = function (prices) {
let answer, min, max;
prices.forEach((price, i) => {
if (i === 0) {
min = price;
max = price;
answer = 0;
return;
}

if (price > max) max = price;
if (price < min) {
min = price;
max = price;
}
answer = Math.max(answer, max - min);
});

return answer;
};
23 changes: 23 additions & 0 deletions group-anagrams/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @description
* brainstorming:
* brute force + hashtable
*
* time complexity: O(n* k log k)
* space complexity: O(n* k)
*/
var groupAnagrams = function (strs) {
const map = new Map();
const answer = [];

strs.forEach((str) => {
const convertedStr = str.split("").sort().join();
if (map.has(convertedStr))
map.set(convertedStr, map.get(convertedStr).concat(str));
else map.set(convertedStr, [str]);
});

map.forEach((value) => answer.push(value));

return answer;
Comment on lines +20 to +22
Copy link
Member

Choose a reason for hiding this comment

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

[안 중요] 요렇게 하면 더 깔끔하지 않을까 생각이 들었습니다. 11번째 줄도 필요가 없어지고요.

Suggested change
map.forEach((value) => answer.push(value));
return answer;
return return [...map.values()];

Copy link
Member Author

Choose a reason for hiding this comment

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

iterator는 spread operate가 가능하군요!

};