Skip to content

Commit 1618f0c

Browse files
Merge pull request #1380 from YoungSeok-Choi/feature/week-5
[YoungSeok-Choi] Week 5 Solutions
2 parents de1491f + c86940f commit 1618f0c

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// NOTE: tc --> O(n)
2+
class Solution {
3+
public int maxProfit(int[] prices) {
4+
5+
int curMax = 0;
6+
int gMax = 0;
7+
8+
if(prices.length == 0) return 0;
9+
10+
int sell = prices[0];
11+
for(int i = 1; i < prices.length; i++) {
12+
curMax = Math.max(0, prices[i] - sell);
13+
14+
// NOTE: ์ƒˆ๋กญ๊ฒŒ ์‹œ์ž‘ํ•˜๋Š”๊ฒŒ ๋” ์ข‹์€๊ฒฝ์šฐ
15+
if(curMax == 0) {
16+
sell = prices[i];
17+
}
18+
19+
gMax = Math.max(curMax, gMax);
20+
}
21+
22+
return gMax;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class Solution {
5+
/*
6+
* @param strs: a list of strings
7+
* @return: encodes a list of strings to a single string.
8+
*/
9+
public String encode(List<String> strs) {
10+
List<String> temp = new ArrayList<>();
11+
12+
if(strs.size() == 0) return null;
13+
14+
for(String s : strs) {
15+
if(":".equals(s)) {
16+
temp.add("::");
17+
} else {
18+
temp.add(s);
19+
}
20+
}
21+
22+
return String.join(":;", temp);
23+
}
24+
25+
/*
26+
* @param str: A string
27+
* @return: decodes a single string to a list of strings
28+
*/
29+
public List<String> decode(String str) {
30+
List<String> temp = new ArrayList<>();
31+
32+
if(str == null) return new ArrayList<>();
33+
34+
// if(str.length() == 0) return new ArrayList<>();
35+
36+
for(String s : str.split(":;")) {
37+
if("::".equals(s)) {
38+
temp.add(":");
39+
} else {
40+
temp.add(s);
41+
}
42+
}
43+
return temp;
44+
}
45+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
// NOTE: tc -> O(n)
8+
class Solution {
9+
public List<List<String>> groupAnagrams(String[] strs) {
10+
11+
List<List<String>> result = new ArrayList<>();
12+
Map<String, List<String>> sMap = new HashMap<>();
13+
14+
for(int i = 0; i < strs.length; i++) {
15+
char[] cArr = strs[i].toCharArray();
16+
Arrays.sort(cArr);
17+
String sorted = new String(cArr);
18+
19+
if(sMap.containsKey(sorted)) {
20+
sMap.get(sorted).add(strs[i]);
21+
} else {
22+
List<String> temp = new ArrayList<>();
23+
temp.add(strs[i]);
24+
sMap.put(sorted, temp);
25+
}
26+
}
27+
28+
for(List<String> arr : sMap.values()) {
29+
result.add(arr);
30+
}
31+
32+
return result;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
// Map์œผ๋กœ ํ’€๋ ค๋ฒ„๋ ค์„œ ๋‹นํ™ฉ..
5+
// ์ด์ง„ํŠธ๋ฆฌ? ์–ด๋–ค์‹์œผ๋กœ ํ’€์–ด์•ผ ํ• ์ง€ ์ž๋ฃŒ๊ตฌ์กฐ ์ •ํ•˜๊ณ  ๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ..
6+
class Trie {
7+
8+
Map<String, Boolean> tMap;
9+
10+
public Trie() {
11+
this.tMap = new HashMap<>();
12+
}
13+
14+
public void insert(String word) {
15+
this.tMap.put(word, true);
16+
}
17+
18+
public boolean search(String word) {
19+
return this.tMap.containsKey(word);
20+
}
21+
22+
public boolean startsWith(String prefix) {
23+
for(String key : this.tMap.keySet()) {
24+
if(key.startsWith(prefix)) return true;
25+
}
26+
27+
return false;
28+
}
29+
}
30+
31+
/**
32+
* Your Trie object will be instantiated and called as such:
33+
* Trie obj = new Trie();
34+
* obj.insert(word);
35+
* boolean param_2 = obj.search(word);
36+
* boolean param_3 = obj.startsWith(prefix);
37+
*/

โ€Žword-break/YoungSeok-Choi.java

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import java.util.HashMap;
2+
import java.util.HashSet;
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Set;
6+
7+
// DFS ์™„์ „ํƒ์ƒ‰ํ•  ๋•Œ ๋ถˆํ•„์š”ํ•œ ํƒ์ƒ‰์„ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•์„ ํ•ญ์ƒ ๊ณ ๋ฏผํ•  ๊ฒƒ.
8+
class Solution {
9+
public int size = 0;
10+
public Map<String, Boolean> failedMap = new HashMap<>();
11+
public boolean wordBreak(String s, List<String> wordDict) {
12+
size = wordDict.size();
13+
14+
Set<Character> sSet = new HashSet<>();
15+
for (char c : s.toCharArray()) {
16+
sSet.add(c);
17+
}
18+
19+
Set<Character> wSet = new HashSet<>();
20+
for (char c : String.join("", wordDict).toCharArray()) {
21+
wSet.add(c);
22+
}
23+
24+
if(sSet.size() > wSet.size()) {
25+
return false;
26+
}
27+
28+
return dfs(s, wordDict);
29+
}
30+
31+
public boolean dfs(String s, List<String> wordDict) {
32+
if(s.length() == 0) return true;
33+
if(failedMap.containsKey(s)) return false;
34+
35+
for(int i = 0; i < size; i++) {
36+
String word = wordDict.get(i);
37+
if(s.startsWith(word)) {
38+
39+
s = s.substring(word.length());
40+
boolean result = dfs(s, wordDict);
41+
42+
if(result) {
43+
return true;
44+
} else {
45+
failedMap.put(s, true);
46+
}
47+
48+
s = word + s;
49+
}
50+
}
51+
52+
return false;
53+
}
54+
}
55+
56+
// ํŠน์ • ๋ฌธ์ž๋กœ ์‹œ์ž‘๋˜๋Š” ๊ฒƒ๋งŒ ํŒ๋‹จํ•˜์—ฌ ๋ฐ˜๋ณตํ•ด ํ’€๋ ค๊ณ  ํ–ˆ๋˜ ์ ‘๊ทผ๋ฒ•.
57+
// ์ „์ฒด ์กฐํ•ฉ์„ ๋ณด์•„์•ผ ํ•˜๋Š” "cars", ["cars", "ca", "rs"] ๊ฒฝ์šฐ์— ๋ฐ˜๋ก€๊ฐ€ ๋จ.
58+
class WrongSolution {
59+
public boolean wordBreak(String s, List<String> wordDict) {
60+
int size = wordDict.size();
61+
62+
while(true) {
63+
boolean isMatched = false;
64+
for(int i = 0; i < size; i++) {
65+
String word = wordDict.get(i);
66+
if(s.startsWith(word)) {
67+
isMatched = true;
68+
s = s.substring(word.length());
69+
break;
70+
}
71+
}
72+
73+
if(!isMatched) {
74+
break;
75+
}
76+
}
77+
78+
return s.length() == 0;
79+
}
80+
}

0 commit comments

Comments
ย (0)