Skip to content

Commit ae92369

Browse files
authored
Merge pull request #2491 from Cyjin-jani/main
[Cyjin-jani] WEEK 05 Solutions
2 parents 8a4a45d + 72a46d1 commit ae92369

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// naive하게 풀면, time limit 초과함
2+
// O(n^2) 풀이가 되기 때문..
3+
const maxProfit_naive = function (prices) {
4+
let profit = 0;
5+
6+
for (let i = 0; i < prices.length - 1; i++) {
7+
const buy = prices[i];
8+
for (let j = i + 1; j < prices.length; j++) {
9+
const newProfit = prices[j] - buy;
10+
if (newProfit > profit) {
11+
profit = newProfit;
12+
}
13+
}
14+
}
15+
16+
return profit;
17+
};
18+
19+
// 투포인터 사용하여 풀기
20+
// tc: O(n)
21+
// sc: O(1)
22+
const maxProfit = function (prices) {
23+
let buyIdx = 0;
24+
let sellIdx = 1;
25+
let profit = 0;
26+
27+
while (sellIdx < prices.length) {
28+
let buyPrice = prices[buyIdx];
29+
let sellPrice = prices[sellIdx];
30+
31+
// 더 낮은 가격에 매수 가능한 날을 찾으면 바로 거기서부터 재탐색
32+
if (buyPrice > sellPrice) {
33+
buyIdx = sellIdx;
34+
} else {
35+
let newProfit = sellPrice - buyPrice;
36+
profit = Math.max(profit, newProfit);
37+
}
38+
39+
sellIdx++;
40+
}
41+
42+
return profit;
43+
};

group-anagrams/Cyjin-jani.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
const isAnagram = function (s, t) {
2+
if (s.length !== t.length) return false;
3+
4+
const data = new Map();
5+
6+
for (let char of s) {
7+
data.set(char, (data.get(char) || 0) + 1);
8+
}
9+
10+
for (let char of t) {
11+
if (!data.get(char)) return false;
12+
data.set(char, data.get(char) - 1);
13+
}
14+
15+
return true;
16+
};
17+
18+
// Time Limit Exceeded로 fail..
19+
// tc: O(n^2);
20+
// sc: O(n)
21+
const groupAnagrams_naive = function (strs) {
22+
if (strs.length < 2) return [strs];
23+
24+
const answer = [];
25+
26+
while (strs.length > 0) {
27+
let temp = [strs[0]];
28+
29+
if (strs.length < 2) return answer;
30+
31+
strs.splice(0, 1);
32+
33+
for (let j = 0; j < strs.length; j++) {
34+
if (isAnagram(temp[0], strs[j])) {
35+
// 같다면, temp 배열에 넣음
36+
temp.push(strs[j]);
37+
}
38+
}
39+
answer.push(temp);
40+
41+
temp.forEach((t) => {
42+
const idx = strs.indexOf(t);
43+
if (idx !== -1) {
44+
strs.splice(idx, 1);
45+
}
46+
});
47+
48+
if (strs.length === 1) {
49+
answer.push(strs);
50+
}
51+
}
52+
53+
return answer;
54+
};
55+
56+
// splice 같은 로직이 없어서 겨우 TLE을 통과했지만 여전히 O(n²)인 점은 변함이 없음.
57+
const groupAnagrams_set = function (strs) {
58+
const visited = new Set();
59+
const answer = [];
60+
61+
for (let i = 0; i < strs.length; i++) {
62+
if (visited.has(i)) continue;
63+
64+
const group = [strs[i]];
65+
66+
for (let j = i + 1; j < strs.length; j++) {
67+
if (!visited.has(j) && isAnagram(strs[i], strs[j])) {
68+
group.push(strs[j]);
69+
visited.add(j);
70+
}
71+
}
72+
73+
answer.push(group);
74+
}
75+
76+
return answer;
77+
};
78+
79+
//! AI로부터 힌트를 얻어 풀어봤습니다..
80+
// tc: O(nlogn)
81+
// sc: O(n)
82+
const groupAnagrams = function (strs) {
83+
const map = new Map();
84+
85+
for (const str of strs) {
86+
const key = str.split('').sort().join('');
87+
88+
if (!map.has(key)) {
89+
map.set(key, []);
90+
}
91+
map.get(key).push(str);
92+
}
93+
94+
return [...map.values()];
95+
};

0 commit comments

Comments
 (0)