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
35 changes: 35 additions & 0 deletions best-time-to-buy-and-sell-stock/naringst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @param {number[]} prices
* @return {number}
*/

/**
* Runtime: 73ms, Memory: 59.38MB
* n = prices.length
* Time complexity: O(n)
* Space complexity: O(1)
*
*/

var maxProfit = function (prices) {
let minPrice = prices[0];
let maxPrice = prices[0];
let answer = 0;

for (let i = 1; i < prices.length; i++) {
answer = Math.max(answer, maxPrice - minPrice);

// 가장 뒤쪽 값이 max일 때
if (maxPrice < prices[i]) {
maxPrice = prices[i];
answer = Math.max(answer, maxPrice - minPrice);
continue;
}
// 가장 뒷쪽 값이 min일 때
if (minPrice > prices[i]) {
minPrice = prices[i];
maxPrice = prices[i];
}
}
return answer;
};
29 changes: 29 additions & 0 deletions group-anagrams/naringst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @param {string[]} strs
* @return {string[][]}
*/

/**
* Runtime: 100ms, Memory: 62.34MB
* n = strs.length
* k = 문자열의 최대 길이
* Time complexity: O(n * k log k)
* -> klogk는 길이가 k인 문자열을 sort
* -> n은 이걸 각 문자열마다 반복하기 때문
*
* Space complexity: O(n)
Copy link
Contributor

Choose a reason for hiding this comment

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

공간복잡도가 n 만 고려되고, k 는 고려되지 않은 것 같습니다~ 한 번 더 확인해주시면 좋을 것 같네요!

*
*/

var groupAnagrams = function (strs) {
let answer = new Map();

for (let j = 0; j < strs.length; j++) {
const sortedWord = strs[j].split("").sort().join("");
answer.has(sortedWord)
? answer.set(sortedWord, [...answer.get(sortedWord), strs[j]])
: answer.set(sortedWord, [strs[j]]);
}

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