Skip to content

Commit 611de3a

Browse files
authored
Merge pull request #453 from naringst/main
[나리] WEEK 05 Solutions
2 parents b840ada + ed2b293 commit 611de3a

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
6+
/**
7+
* Runtime: 73ms, Memory: 59.38MB
8+
* n = prices.length
9+
* Time complexity: O(n)
10+
* Space complexity: O(1)
11+
*
12+
*/
13+
14+
var maxProfit = function (prices) {
15+
let minPrice = prices[0];
16+
let maxPrice = prices[0];
17+
let answer = 0;
18+
19+
for (let i = 1; i < prices.length; i++) {
20+
answer = Math.max(answer, maxPrice - minPrice);
21+
22+
// 가장 뒤쪽 값이 max일 때
23+
if (maxPrice < prices[i]) {
24+
maxPrice = prices[i];
25+
answer = Math.max(answer, maxPrice - minPrice);
26+
continue;
27+
}
28+
// 가장 뒷쪽 값이 min일 때
29+
if (minPrice > prices[i]) {
30+
minPrice = prices[i];
31+
maxPrice = prices[i];
32+
}
33+
}
34+
return answer;
35+
};

group-anagrams/naringst.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
6+
/**
7+
* Runtime: 100ms, Memory: 62.34MB
8+
* n = strs.length
9+
* k = 문자열의 최대 길이
10+
* Time complexity: O(n * k log k)
11+
* -> klogk는 길이가 k인 문자열을 sort
12+
* -> n은 이걸 각 문자열마다 반복하기 때문
13+
*
14+
* Space complexity: O(n)
15+
*
16+
*/
17+
18+
var groupAnagrams = function (strs) {
19+
let answer = new Map();
20+
21+
for (let j = 0; j < strs.length; j++) {
22+
const sortedWord = strs[j].split("").sort().join("");
23+
answer.has(sortedWord)
24+
? answer.set(sortedWord, [...answer.get(sortedWord), strs[j]])
25+
: answer.set(sortedWord, [strs[j]]);
26+
}
27+
28+
return Array.from(answer.values());
29+
};

0 commit comments

Comments
 (0)