Skip to content

Commit c37dddf

Browse files
authored
Merge pull request #868 from limlimjo/main
[jj7779607] Week5
2 parents 2940e67 + 24a308f commit c37dddf

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function(prices) {
6+
7+
// 최대 수익
8+
let maxProfit = 0;
9+
// prices 배열의 0번째를 최소 가격으로 시작
10+
let minPrice = prices[0];
11+
12+
// prices 배열 for문 돌려서 최대 수익 찾기
13+
for(let i=0; i<prices.length; i++) {
14+
minPrice = Math.min(minPrice, prices[i]);
15+
let current = prices[i] - minPrice;
16+
maxProfit = Math.max(current, maxProfit);
17+
}
18+
// 최대 수익 반환
19+
return maxProfit;
20+
21+
};
22+
23+
// 시간 복잡도: O(n)
24+
// 공간 복잡도: O(1)

group-anagrams/limlimjo.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
var groupAnagrams = function (strs) {
6+
// map에 담기
7+
const map = new Map();
8+
9+
// for문 돌기
10+
for (const str of strs) {
11+
// 정렬된 단어
12+
const sortWord = str.split("").sort().join("");
13+
// map에 가지고 있지 않을 때
14+
if (!map.has(sortWord)) {
15+
map.set(sortWord, []);
16+
}
17+
map.get(sortWord).push(str);
18+
}
19+
20+
return Array.from(map.values());
21+
};

0 commit comments

Comments
 (0)