Skip to content

Commit a267cd5

Browse files
authored
Merge pull request #850 from mintheon/main
[mintheon] Week 5
2 parents 1bfe1f3 + 03adfe9 commit a267cd5

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
/**
3+
시간복잡도: O(n)
4+
공간복잡도: O(1)
5+
*/
6+
public int maxProfit(int[] prices) {
7+
int buyPrice = prices[0];
8+
int maxProfit = 0;
9+
10+
for(int i = 0; i < prices.length; i++) {
11+
buyPrice = Math.min(buyPrice, prices[i]);
12+
maxProfit = Math.max(maxProfit, prices[i] - buyPrice);
13+
}
14+
15+
return maxProfit;
16+
}
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class Codec {
5+
String SPLIT = ":";
6+
7+
// Encodes a list of strings to a single string.
8+
public String encode(List<String> strs) {
9+
StringBuilder encoded = new StringBuilder();
10+
11+
for(String str : strs) {
12+
encoded.append(str.length()).append(SPLIT).append(str);
13+
}
14+
15+
return encoded.toString();
16+
}
17+
18+
// Decodes a single string to a list of strings.
19+
public List<String> decode(String s) {
20+
List<String> decoded = new ArrayList<>();
21+
22+
int pointer = 0;
23+
while(pointer < s.length()) {
24+
int index = s.indexOf(SPLIT, pointer);
25+
int length = Integer.parseInt(s.substring(pointer, index));
26+
27+
String str = s.substring(index + 1, index + 1 + length);
28+
decoded.add(str);
29+
30+
pointer = index + 1 + length;
31+
}
32+
33+
return decoded;
34+
}
35+
}
36+
37+
// Your Codec object will be instantiated and called as such:
38+
// Codec codec = new Codec();
39+
// codec.decode(codec.encode(strs));

group-anagrams/mintheon.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
class Solution {
8+
/**
9+
시간복잡도: O(n)
10+
공간복잡도: O(n)
11+
*/
12+
public List<List<String>> groupAnagrams(String[] strs) {
13+
Map<String, List<String>> anagramMap = new HashMap<>();
14+
15+
for(String str : strs) {
16+
char[] charStr = str.toCharArray();
17+
Arrays.sort(charStr);
18+
String sortedStr = String.valueOf(charStr);
19+
20+
if(!anagramMap.containsKey(sortedStr)) {
21+
anagramMap.put(sortedStr, new ArrayList<>());
22+
23+
}
24+
25+
anagramMap.get(sortedStr).add(str);
26+
}
27+
28+
return new ArrayList<>(anagramMap.values());
29+
}
30+
}

0 commit comments

Comments
 (0)