Skip to content

Commit d688a25

Browse files
authored
Merge pull request #881 from donghyeon95/main
[donghyeon95] Week5
2 parents 77d46a2 + 2325e53 commit d688a25

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
int result = 0;
4+
int maxProfit = 0;
5+
int buyStock = prices[0];
6+
7+
// ํ•œ๋ฒˆ ๋Œ๋ฉด์„œ ๋‚˜๋ณด๋‹ค ์ž‘์€ ๊ฒƒ์ด ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ์ด์ต์„ ๋ณธ๋‹ค
8+
9+
10+
for (int price: prices) {
11+
// ๋‚˜๋ณด๋‹ค ์ž‘์€ ๊ฒŒ ๋‚˜์˜ค๋ฉด MAX ์ด์ต์„ ๊ฐฑ์‹ ํ•˜๊ณ  ๊ฑฐ๊ธฐ๋ถ€์„œ ๋‹ค์‹œ ์‹œ์ž‘ํ•œ๋‹ค.
12+
if (price < buyStock) {
13+
result = Math.max(result, maxProfit);
14+
maxProfit = 0;
15+
buyStock = price;
16+
} else {
17+
maxProfit = Math.max(price - buyStock, maxProfit);
18+
}
19+
}
20+
21+
return Math.max(result, maxProfit);
22+
}
23+
}
24+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
public class Solution {
7+
/*
8+
* @param strs: a list of strings
9+
* @return: encodes a list of strings to a single string.
10+
*/
11+
String cDel = "&";
12+
String sDel = ";";
13+
14+
// Encodes a list of strings to a single string
15+
public String encode(List<String> strs) {
16+
if (strs.isEmpty()) return null;
17+
18+
StringBuilder result = new StringBuilder();
19+
for (int i =0; i<strs.size(); i++) {
20+
String str = strs.get(i);
21+
StringBuilder temp = new StringBuilder();
22+
for (char c : str.toCharArray()) {
23+
temp.append((int) c).append(cDel);
24+
}
25+
26+
result.append(temp);
27+
if (i != strs.size()-1) result.append(sDel);
28+
}
29+
return result.toString();
30+
}
31+
32+
// Decodes a single string to a list of strings
33+
public List<String> decode(String str) {
34+
if (str==null)
35+
return new ArrayList<>();
36+
37+
List<String> result = new ArrayList<>();
38+
String[] strs = str.split(sDel, -1);
39+
for (String s : strs) {
40+
if (s.isEmpty()) {
41+
result.add("");
42+
continue;
43+
}
44+
String[] chars = s.split(cDel);
45+
String decoded = Arrays.stream(chars)
46+
.filter(sr -> !sr.isEmpty())
47+
.mapToInt(Integer::parseInt)
48+
.mapToObj(ascii -> (char) ascii)
49+
.map(String::valueOf)
50+
.collect(Collectors.joining());
51+
result.add(decoded);
52+
}
53+
return result;
54+
}
55+
}
56+
57+

โ€Žgroup-anagrams/donghyeon95.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.HashMap;
4+
import java.util.List;
5+
6+
class Solution {
7+
public List<List<String>> groupAnagrams(String[] strs) {
8+
// ๋ฌธ์ž์—ด ์ •๋ ฌ์„ ํ•ด์„œ ๊ฐ™์€ ์• ๋“ค ๋ชจ์Œ์„ ์ฃผ๋ฉด ๋˜์ง€ ์•Š์„๊นŒ??
9+
// ์ด๋Ÿด ๊ฒฝ์šฐ ์ •๋ ฌ์— ๋งŽ์€ ์‹œ๊ฐ„์„ ์†Œ๋ชจ
10+
11+
HashMap<String, List<String>> hm = new HashMap<>();
12+
for (String str: strs) {
13+
String arrangedStr = rearangeStr(str);
14+
hm.putIfAbsent(arrangedStr, new ArrayList<>());
15+
hm.get(arrangedStr).add(str);
16+
}
17+
18+
return hm.values().stream().toList();
19+
}
20+
21+
public String rearangeStr (String str) {
22+
char[] chars = str.toCharArray();
23+
Arrays.sort(chars);
24+
25+
return new String(chars);
26+
}
27+
}
28+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.HashMap;
2+
3+
class Trie {
4+
HashMap<String, Boolean> trie;
5+
public Trie() {
6+
trie = new HashMap<>();
7+
}
8+
9+
public void insert(String word) {
10+
StringBuilder sb = new StringBuilder();
11+
for (char c: word.toCharArray()) {
12+
sb.append(c);
13+
trie.putIfAbsent(sb.toString(), false);
14+
}
15+
trie.put(sb.toString(), true);
16+
}
17+
18+
public boolean search(String word) {
19+
return trie.getOrDefault(word, false);
20+
}
21+
22+
public boolean startsWith(String prefix) {
23+
return trie.containsKey(prefix);
24+
}
25+
}
26+
27+
28+
29+

โ€Žword-break/donghyeon95.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.HashSet;
4+
import java.util.List;
5+
import java.util.Set;
6+
import java.util.stream.Collectors;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
13+
class Solution {
14+
public boolean wordBreak(String s, List<String> wordDict) {
15+
return dfs(s, wordDict, new HashSet<>());
16+
}
17+
18+
private boolean dfs(String s, List<String> wordDict, Set<String> dp) {
19+
// ์ข…๋ฃŒ ์กฐ๊ฑด: ๋ฌธ์ž์—ด์ด ๋น„์–ด ์žˆ์œผ๋ฉด ์„ฑ๊ณต
20+
if (s.isEmpty()) return true;
21+
22+
// ์ค‘๋ณต ํƒ์ƒ‰ ๋ฐฉ์ง€
23+
if (dp.contains(s)) return false;
24+
25+
for (String word : wordDict) {
26+
if (s.startsWith(word)) {
27+
// ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์žฌ๊ท€ ํ˜ธ์ถœ
28+
if (dfs(s.substring(word.length()), wordDict, dp)) {
29+
return true;
30+
}
31+
}
32+
}
33+
34+
// ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•˜์ง€ ์•Š๊ณ  ๋„˜์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ๋„ ํƒ์ƒ‰
35+
dp.add(s); // ํƒ์ƒ‰์ด ์‹คํŒจํ•œ ์ƒํƒœ ์ €์žฅ
36+
return false;
37+
}
38+
}
39+
40+
41+

0 commit comments

Comments
ย (0)