Skip to content

Commit 1d344bb

Browse files
authored
Merge pull request #2504 from Hyeri1ee/main
[Hyeri1ee] WEEK 05 Solutions
2 parents 4c4c415 + b2768a1 commit 1d344bb

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
3+
4+
5+
6+
7+
//O(n)에 증가 한 경우 profitMax를 갱신시키며 판단 -> time limit
8+
public int maxProfit(int[] prices) {
9+
int profitMax = 0;
10+
/*
11+
for(int i=0; i< prices.length - 1; i++){
12+
int buy= prices[i];
13+
14+
15+
for(int d =i+1; d < prices.length; d++){
16+
int sellCandid = prices[d];
17+
if (sellCandid - buy > profitMax){
18+
profitMax = sellCandid - buy;
19+
}
20+
21+
}
22+
}//end of for
23+
24+
*/
25+
26+
int priceMin = Integer.MAX_VALUE;
27+
for(int i =0; i < prices.length; i++){
28+
if (prices[i] < priceMin){
29+
priceMin = prices[i];
30+
}
31+
else{
32+
profitMax = Math.max(profitMax, prices[i] - priceMin);
33+
}
34+
}
35+
36+
return profitMax;
37+
38+
}
39+
40+
}
41+

group-anagrams/Hyeri1ee.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
//static HashMap<
5+
public List<List<String>> groupAnagrams(String[] strs) {
6+
HashMap<String, List<String>> map = new HashMap<>();
7+
8+
for(int w = 0 ;w < strs.length; w++){
9+
String word = strs[w];
10+
char[] arr = word.toCharArray();
11+
12+
//정렬한거
13+
Arrays.sort(arr);
14+
String newWord = new String(arr);
15+
16+
map.putIfAbsent(newWord, new ArrayList<>());
17+
map.get(newWord).add(word);
18+
19+
}
20+
21+
22+
List<List<String>> list = new ArrayList<>();
23+
for(List<String> group : map.values()){
24+
list.add(group);
25+
}
26+
27+
return list;
28+
}
29+
}
30+

0 commit comments

Comments
 (0)