Skip to content

Commit e78f04f

Browse files
authored
Merge pull request #857 from ekgns33/main
[byteho0n] Week5
2 parents 346b352 + 4119a96 commit e78f04f

File tree

5 files changed

+240
-0
lines changed

5 files changed

+240
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
int minPrice = Integer.MAX_VALUE;
4+
int maxProfit = 0;
5+
for(int i = 0; i < prices.length; i++) {
6+
minPrice = Math.min(minPrice, prices[i]);
7+
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
8+
}
9+
return maxProfit;
10+
}
11+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public String encode(List<String> strs) {
3+
StringBuilder res = new StringBuilder();
4+
for (String s : strs) {
5+
res.append(s.length()).append('|').append(s);
6+
}
7+
return res.toString();
8+
}
9+
10+
11+
/*
12+
* number + | + string
13+
* read number until |
14+
* move pointer, read substring
15+
*
16+
* tc : O(n) when n is the length of encoded string
17+
* sc : O(1)
18+
* */
19+
public List<String> decode(String str) {
20+
List<String> res = new ArrayList<>();
21+
int start = 0;
22+
while (start < str.length()) {
23+
int cur = start;
24+
//read until |
25+
while (str.charAt(cur) != '|') {
26+
cur++;
27+
}
28+
int length = Integer.parseInt(str.substring(start, cur));
29+
start = cur + 1;
30+
cur = start + length;
31+
res.add(str.substring(start, cur));
32+
start = cur;
33+
}
34+
return res;
35+
}
36+
}

group-anagrams/ekgns33.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
3+
input : array of strings
4+
output : grouped anagrams
5+
6+
ex) eat ate tea > same group
7+
8+
solution 1) brute force
9+
ds : hashmap
10+
algo : sort
11+
generate Key by sorting string O(nlogn)
12+
13+
save to hashmap
14+
15+
tc : O(m* nlogn) when m is length of array, n is max length of string
16+
sc : O(m)
17+
18+
solutino 2) better?
19+
cannot optimize m times read
20+
how about generating key
21+
22+
sort = nlogn
23+
read = n << use frequency
24+
26 * n
25+
tc : O(m * (26 * n)) ~= O(mn)
26+
sc : O(m * 26) ~= O(m)
27+
*/
28+
29+
class Solution {
30+
public List<List<String>> groupAnagrams(String[] strs) {
31+
Map<String, List<String>> map = new HashMap<>();
32+
int n = strs.length;
33+
for(String str : strs) {
34+
int l = str.length();
35+
char[] freq = new char[26];
36+
for(int i = 0; i < l; i++) {
37+
freq[str.charAt(i) - 'a']++;
38+
}
39+
String key = new String(freq);
40+
map.putIfAbsent(key, new ArrayList<>());
41+
map.get(key).add(str);
42+
}
43+
return List.copyOf(map.values());
44+
}
45+
}
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Trie {
2+
3+
class Node {
4+
boolean isEnd;
5+
Node[] childs = new Node[26];
6+
Node() {}
7+
}
8+
9+
private Node root;
10+
11+
public Trie() {
12+
this.root = new Node();
13+
}
14+
15+
// O(m) when m is the length of word
16+
public void insert(String word) {
17+
Node curN = this.root;
18+
for(char c : word.toCharArray()) {
19+
if(curN.childs[c-'a'] == null) {
20+
curN.childs[c-'a'] = new Node();
21+
}
22+
curN = curN.childs[c-'a'];
23+
}
24+
//end
25+
curN.isEnd = true;
26+
}
27+
// O(k) when k is the length of searching word
28+
public boolean search(String word) {
29+
Node curN = this.root;
30+
for(char c : word.toCharArray()) {
31+
if(curN.childs[c-'a'] == null) return false;
32+
curN = curN.childs[c-'a'];
33+
}
34+
return curN.isEnd;
35+
}
36+
37+
// O(k) when k is the length of prefix
38+
public boolean startsWith(String prefix) {
39+
Node curN = this.root;
40+
for(char c : prefix.toCharArray()) {
41+
if(curN.childs[c-'a'] == null) return false;
42+
curN = curN.childs[c-'a'];
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+
*/

word-break/ekgns33.java

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
input : string s and array of strings
3+
output : return true if s can be segmented into one or more words in dictionary
4+
constraints :
5+
1) same word can be used multiple times
6+
2) every word in dictionary is unique
7+
3) s.length [1, 300]
8+
4) length of tokens in dictionary [1, 20]
9+
10+
ex)
11+
leetcode >> leet code
12+
applepenapple >> apple pen
13+
applepenapple >> apple applepen
14+
15+
16+
solution 1) bruteforce
17+
for each character c, check if word exists in dictionary
18+
that the first letter is c
19+
20+
match string
21+
move to next
22+
unmatch
23+
next word
24+
25+
>> get all the combinations
26+
O(n * m * k)
27+
n is the length of string s,
28+
m is number of tokens,
29+
k is length of token
30+
31+
tc : O (nmk) (300 * 1000 * 20) ~= 6 * 10^6 < 1sec
32+
sc : O (mk)
33+
34+
solution 2) optimize? heuristic?
35+
36+
using trie will reduce tc
37+
for each character:
38+
get all substring substr(i, j)
39+
search trie
40+
41+
tc : O(n^2 + m*k)
42+
43+
solution 3) dp
44+
45+
let dp[i] true if possible to build s which length is i
46+
with segemented tokens
47+
48+
build HashSet for tokens
49+
iterate i from 1 to n
50+
iterate j from 0 to i-1
51+
dp[i] = dp[j] & substr(j,i) presents // substr O(n)
52+
if true break;
53+
54+
tc : O(n^3 + m*k)
55+
*/
56+
class Solution {
57+
public boolean wordBreak(String s, List<String> wordDict) {
58+
Set<String> dict = new HashSet<>(wordDict);
59+
boolean[] dp = new boolean[s.length() + 1];
60+
dp[0] = true;
61+
for(int i = 1; i < dp.length; i++) {
62+
for(int j = 0; j < i; j++) {
63+
if(dp[j] && dict.contains(s.substring(j, i))){
64+
dp[i] = true;
65+
break;
66+
}
67+
}
68+
}
69+
return dp[dp.length - 1];
70+
}
71+
}
72+
73+
74+
// class Solution1 {
75+
// public boolean wordBreak(String s, List<String> wordDict) {
76+
// Map<Character, List<String>> tokens = new HashMap<>();
77+
// for(String word : wordDict) {
78+
// tokens.putIfAbsent(word.charAt(word.length()-1), new ArrayList<>());
79+
// tokens.get(word.charAt(word.length()-1)).add(word);
80+
// }
81+
// boolean[] dp = new boolean[s.length()+1];
82+
// dp[0] = true;
83+
// for(int i = 1; i < dp.length; i++) {
84+
// List<String> token = tokens.get(s.charAt(i-1));
85+
// if(token == null) {dp[i] = false; continue;}
86+
// for(String word : token) {
87+
// if(i - word.length() < 0) continue;
88+
// dp[i] = dp[i-word.length()] && s.substring(i-word.length(), i).equals(word);
89+
// if(dp[i]) break;
90+
// }
91+
// }
92+
// return dp[dp.length-1];
93+
// }
94+
// }

0 commit comments

Comments
 (0)