Skip to content

Commit 45f646b

Browse files
authored
Merge pull request #2498 from juhui-jeong/main
[juhui-jeong] WEEK 05 Solutions
2 parents 6680459 + 2c75c1a commit 45f646b

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
시간복잡도: O(n²)
3+
공간복잡도: O(1)
4+
class Solution {
5+
public int maxProfit(int[] prices) {
6+
int maxStock = 0;
7+
for(int i = 0; i < prices.length; i++) {
8+
for (int j = 0; j < prices.length; j++) {
9+
if (i <= j) break;
10+
if (prices[i] - prices[j] > maxStock) {
11+
maxStock = prices[i] - prices[j];
12+
}
13+
}
14+
}
15+
return maxStock;
16+
}
17+
}
18+
*/
19+
// 시간복잡도: O(n)
20+
// 공간복잡도: O(1)
21+
class Solution {
22+
public int maxProfit(int[] prices) {
23+
int maxStock = 0;
24+
int minPrice = prices[0];
25+
for (int price : prices) {
26+
if (price < minPrice) {
27+
minPrice = price;
28+
} else {
29+
maxStock = Math.max(price - minPrice, maxStock);
30+
}
31+
}
32+
return maxStock;
33+
}
34+
}

group-anagrams/juhui-jeong.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
시간복잡도: O(k log k)
3+
공간복잡도: O(n * k)
4+
*/
5+
class Solution {
6+
public List<List<String>> groupAnagrams(String[] strs) {
7+
Map<String, List<String>> map = new HashMap<>();
8+
9+
for (String str: strs) {
10+
char[] chars = str.toCharArray();
11+
Arrays.sort(chars);
12+
String key = new String(chars);
13+
14+
if (!map.containsKey(key)) {
15+
map.put(key, new ArrayList<>());
16+
}
17+
map.get(key).add(str);
18+
}
19+
return new ArrayList<>(map.values());
20+
}
21+
}

0 commit comments

Comments
 (0)