Skip to content

Commit 77d46a2

Browse files
authored
Merge pull request #879 from HerrineKim/main
[HerrineKim] Week 5
2 parents 1732a3f + f5836b8 commit 77d46a2

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
3+
4+
// ์ตœ์†Œ๊ฐ’์„ ๊ณ„์† ๊ฐฑ์‹ ํ•˜๋ฉด์„œ ์ตœ๋Œ€ ์ด์ต์„ ๊ณ„์‚ฐ
5+
6+
/**
7+
* @param {number[]} prices
8+
* @return {number}
9+
*/
10+
var maxProfit = function (prices) {
11+
let minPrice = Infinity;
12+
let maxProfit = 0;
13+
14+
for (let price of prices) {
15+
if (price < minPrice) {
16+
minPrice = price;
17+
} else {
18+
maxProfit = Math.max(maxProfit, price - minPrice);
19+
}
20+
}
21+
22+
return maxProfit;
23+
};
24+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n * m log m)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
3+
4+
// HashMap ์‚ฌ์šฉ
5+
// ๊ฐ ๋ฌธ์ž์—ด์„ ์ •๋ ฌํ•˜์—ฌ ํ‚ค๋กœ ์‚ฌ์šฉ
6+
// ์ •๋ ฌ๋œ ๋ฌธ์ž์—ด์„ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ๋ฃนํ™”
7+
8+
/**
9+
* @param {string[]} strs
10+
* @return {string[][]}
11+
*/
12+
var groupAnagrams = function (strs) {
13+
const map = {};
14+
15+
for (const str of strs) {
16+
const key = str.split('').sort().join('');
17+
if (!map[key]) {
18+
map[key] = [];
19+
}
20+
map[key].push(str);
21+
}
22+
23+
return Object.values(map);
24+
};
25+

0 commit comments

Comments
ย (0)