Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[byteho0n] Week5 #857

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions best-time-to-buy-and-sell-stock/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public int maxProfit(int[] prices) {
int minPrice = Integer.MAX_VALUE;
int maxProfit = 0;
for(int i = 0; i < prices.length; i++) {
minPrice = Math.min(minPrice, prices[i]);
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
}
return maxProfit;
}
}
36 changes: 36 additions & 0 deletions encode-and-decode-strings/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
public String encode(List<String> strs) {
StringBuilder res = new StringBuilder();
for (String s : strs) {
res.append(s.length()).append('|').append(s);
}
return res.toString();
}


/*
* number + | + string
* read number until |
* move pointer, read substring
*
* tc : O(n) when n is the length of encoded string
* sc : O(1)
* */
public List<String> decode(String str) {
List<String> res = new ArrayList<>();
int start = 0;
while (start < str.length()) {
int cur = start;
//read until |
while (str.charAt(cur) != '|') {
cur++;
}
int length = Integer.parseInt(str.substring(start, cur));
start = cur + 1;
cur = start + length;
res.add(str.substring(start, cur));
start = cur;
}
return res;
}
}
45 changes: 45 additions & 0 deletions group-anagrams/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**

input : array of strings
output : grouped anagrams

ex) eat ate tea > same group

solution 1) brute force
ds : hashmap
algo : sort
generate Key by sorting string O(nlogn)

save to hashmap

tc : O(m* nlogn) when m is length of array, n is max length of string
sc : O(m)

solutino 2) better?
cannot optimize m times read
how about generating key

sort = nlogn
read = n << use frequency
26 * n
tc : O(m * (26 * n)) ~= O(mn)
sc : O(m * 26) ~= O(m)
*/

class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
int n = strs.length;
for(String str : strs) {
int l = str.length();
char[] freq = new char[26];
for(int i = 0; i < l; i++) {
freq[str.charAt(i) - 'a']++;
}
String key = new String(freq);
map.putIfAbsent(key, new ArrayList<>());
map.get(key).add(str);
}
return List.copyOf(map.values());
}
}
54 changes: 54 additions & 0 deletions implement-trie-prefix-tree/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class Trie {

class Node {
boolean isEnd;
Node[] childs = new Node[26];
Node() {}
}

private Node root;

public Trie() {
this.root = new Node();
}

// O(m) when m is the length of word
public void insert(String word) {
Node curN = this.root;
for(char c : word.toCharArray()) {
if(curN.childs[c-'a'] == null) {
curN.childs[c-'a'] = new Node();
}
curN = curN.childs[c-'a'];
}
//end
curN.isEnd = true;
}
// O(k) when k is the length of searching word
public boolean search(String word) {
Node curN = this.root;
for(char c : word.toCharArray()) {
if(curN.childs[c-'a'] == null) return false;
curN = curN.childs[c-'a'];
}
return curN.isEnd;
}

// O(k) when k is the length of prefix
public boolean startsWith(String prefix) {
Node curN = this.root;
for(char c : prefix.toCharArray()) {
if(curN.childs[c-'a'] == null) return false;
curN = curN.childs[c-'a'];
}
return true;
}
}

/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/
94 changes: 94 additions & 0 deletions word-break/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
input : string s and array of strings
output : return true if s can be segmented into one or more words in dictionary
constraints :
1) same word can be used multiple times
2) every word in dictionary is unique
3) s.length [1, 300]
4) length of tokens in dictionary [1, 20]

ex)
leetcode >> leet code
applepenapple >> apple pen
applepenapple >> apple applepen


solution 1) bruteforce
for each character c, check if word exists in dictionary
that the first letter is c

match string
move to next
unmatch
next word

>> get all the combinations
O(n * m * k)
n is the length of string s,
m is number of tokens,
k is length of token

tc : O (nmk) (300 * 1000 * 20) ~= 6 * 10^6 < 1sec
sc : O (mk)

solution 2) optimize? heuristic?

using trie will reduce tc
for each character:
get all substring substr(i, j)
search trie

tc : O(n^2 + m*k)

solution 3) dp

let dp[i] true if possible to build s which length is i
with segemented tokens

build HashSet for tokens
iterate i from 1 to n
iterate j from 0 to i-1
dp[i] = dp[j] & substr(j,i) presents // substr O(n)
if true break;

tc : O(n^3 + m*k)
*/
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
Set<String> dict = new HashSet<>(wordDict);
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
for(int i = 1; i < dp.length; i++) {
for(int j = 0; j < i; j++) {
if(dp[j] && dict.contains(s.substring(j, i))){
dp[i] = true;
break;
}
}
}
return dp[dp.length - 1];
}
}


// class Solution1 {
// public boolean wordBreak(String s, List<String> wordDict) {
// Map<Character, List<String>> tokens = new HashMap<>();
// for(String word : wordDict) {
// tokens.putIfAbsent(word.charAt(word.length()-1), new ArrayList<>());
// tokens.get(word.charAt(word.length()-1)).add(word);
// }
// boolean[] dp = new boolean[s.length()+1];
// dp[0] = true;
// for(int i = 1; i < dp.length; i++) {
// List<String> token = tokens.get(s.charAt(i-1));
// if(token == null) {dp[i] = false; continue;}
// for(String word : token) {
// if(i - word.length() < 0) continue;
// dp[i] = dp[i-word.length()] && s.substring(i-word.length(), i).equals(word);
// if(dp[i]) break;
// }
// }
// return dp[dp.length-1];
// }
// }
Loading