Skip to content

Commit 4f67fcf

Browse files
authored
Merge pull request #1841 from std-freejia/main
[std-freejia] week05 solutions
2 parents 49625e5 + 7c529eb commit 4f67fcf

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 121. Best Time to Buy and Sell Stock
3+
* 반복문 돌면서 가장 작은 값을 기억해둡니다
4+
* 매번 최소값과의 차이를 비교합니다
5+
*/
6+
class Solution {
7+
public int maxProfit(int[] prices) {
8+
int maxProfit = 0;
9+
int minPrice = Integer.MAX_VALUE;
10+
11+
for (int price: prices) {
12+
if (price < minPrice) { // 최소 값 찾기
13+
minPrice = price;
14+
}
15+
if (price - minPrice > maxProfit) {
16+
maxProfit = price - minPrice;
17+
}
18+
}
19+
return maxProfit;
20+
}
21+
}

group-anagrams/std-freejia.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public List<List<String>> groupAnagrams(String[] strs) {
3+
List<List<String>> answer = new ArrayList<>();
4+
// <정렬한 문자, 원본 문자 리스트>
5+
HashMap<String, List<String>> map = new HashMap<>();
6+
7+
for (String str : strs) {
8+
char[] arr = str.toCharArray();
9+
Arrays.sort(arr);
10+
String strKey = String.valueOf(arr);
11+
12+
if (!map.containsKey(strKey)) {
13+
ArrayList<String> list = new ArrayList<>();
14+
list.add(str);
15+
map.put(strKey, list);
16+
} else {
17+
map.get(strKey).add(str);
18+
}
19+
}
20+
answer.addAll(map.values());
21+
return answer;
22+
}
23+
}

0 commit comments

Comments
 (0)