Skip to content

Commit 807a383

Browse files
authored
Merge pull request #2163 from hyunolike/main
[hyunolike] WEEK 05 Solutions
2 parents 5f080b5 + 7d530fe commit 807a383

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
// 주식 최대 이익
4+
int minPrice = Integer.MAX_VALUE;
5+
int maxProfit = 0;
6+
7+
for (int price : prices) {
8+
minPrice = Math.min(minPrice, price);
9+
maxProfit = Math.max(maxProfit, price - minPrice);
10+
}
11+
12+
return maxProfit;
13+
}
14+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class Codec {
2+
3+
// Encodes a list of strings to a single string.
4+
public String encode(List<String> strs) {
5+
StringBuilder sb = new StringBuilder();
6+
7+
for (String s : strs) {
8+
sb.append(s.length()).append('#').append(s);
9+
}
10+
11+
return sb.toString();
12+
}
13+
14+
// Decodes a single string to a list of strings.
15+
public List<String> decode(String s) {
16+
List<String> res = new ArrayList<>();
17+
int i = 0;
18+
19+
while (i < s.length()) {
20+
int j = i;
21+
22+
// find '#'
23+
while (s.charAt(j) != '#') {
24+
j++;
25+
}
26+
27+
int length = Integer.parseInt(s.substring(i, j));
28+
j++; // move past '#'
29+
30+
String word = s.substring(j, j + length);
31+
res.add(word);
32+
33+
i = j + length; // move to next encoded segment
34+
}
35+
36+
return res;
37+
}
38+
}
39+

group-anagrams/hyunolike.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public List<List<String>> groupAnagrams(String[] strs) {
3+
// 같은 문자열 끼리 묶기
4+
// 그룹이 가장 적은 순으로 정렬
5+
6+
Map<String, List<String>> map = new HashMap<>();
7+
8+
// 각 문자열 char로 바꿔서 정렬하고 다시 문자열
9+
for(String str : strs) {
10+
char[] arr = str.toCharArray();
11+
Arrays.sort(arr);
12+
13+
String key = new String(arr); // 정렬된 문자열 키로 하기
14+
15+
map.computeIfAbsent(key, k -> new ArrayList<>()).add(str);
16+
}
17+
18+
return new ArrayList<>(map.values());
19+
// map 자료구조, 정렬까지 생각
20+
// 이후 프로세스 생각 어려움
21+
}
22+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class TrieNode {
2+
TrieNode[] children = new TrieNode[26];
3+
boolean isEnd;
4+
}
5+
6+
class Trie {
7+
8+
private TrieNode root;
9+
10+
public Trie() {
11+
root = new TrieNode();
12+
}
13+
14+
public void insert(String word) {
15+
TrieNode node = root;
16+
for (char c : word.toCharArray()) {
17+
int idx = c - 'a';
18+
if(node.children[idx] == null) {
19+
node.children[idx] = new TrieNode();
20+
}
21+
node = node.children[idx];
22+
}
23+
node.isEnd = true;
24+
}
25+
26+
public boolean search(String word) {
27+
TrieNode node = root;
28+
for(char c : word.toCharArray()) {
29+
int idx = c - 'a';
30+
if(node.children[idx] == null) return false;
31+
node = node.children[idx];
32+
}
33+
34+
return node.isEnd;
35+
}
36+
37+
public boolean startsWith(String prefix) {
38+
TrieNode node = root;
39+
for(char c : prefix.toCharArray()) {
40+
int idx = c - 'a';
41+
if(node.children[idx] == null) return false;
42+
node = node.children[idx];
43+
}
44+
return true;
45+
}
46+
}
47+
48+
/**
49+
* Your Trie object will be instantiated and called as such:
50+
* Trie obj = new Trie();
51+
* obj.insert(word);
52+
* boolean param_2 = obj.search(word);
53+
* boolean param_3 = obj.startsWith(prefix);
54+
*/
55+

word-break/hyunolike.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public boolean wordBreak(String s, List<String> wordDict) {
3+
int n = s.length();
4+
boolean[] dp = new boolean[n+1];
5+
dp[0] = true;
6+
7+
Set<String> wordSet = new HashSet<>(wordDict);
8+
9+
for(int i = 1; i <= n; i++) {
10+
for(int j = 0; j < i; j++) {
11+
if(dp[j] && wordSet.contains(s.substring(j, i))) {
12+
dp[i] = true;
13+
break;
14+
}
15+
}
16+
}
17+
return dp[n];
18+
}
19+
}
20+

word-break/hyunolike.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function wordBreak(s: string, wordDict: string[]): boolean {
2+
const n = s.length;
3+
const dp:boolean[] = Array(n+1).fill(false);
4+
dp[0] = true;
5+
6+
const wordSet = new Set(wordDict);
7+
8+
for(let i = 1; i <= n; i++) {
9+
for(let j = 0; j < i; j++) {
10+
if(dp[j] && wordSet.has(s.substring(j, i))) {
11+
dp[i] = true;
12+
break;
13+
}
14+
}
15+
}
16+
return dp[n];
17+
};

0 commit comments

Comments
 (0)