diff --git a/best-time-to-buy-and-sell-stock/yhkee0404.go b/best-time-to-buy-and-sell-stock/yhkee0404.go new file mode 100644 index 000000000..4379b18c0 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/yhkee0404.go @@ -0,0 +1,9 @@ +func maxProfit(prices []int) int { + ans := 0 + bought := prices[0] + for i := 1; i != len(prices); i++ { + ans = max(ans, prices[i] - bought) + bought = min(bought, prices[i]) + } + return ans +} diff --git a/encode-and-decode-strings/yhkee0404.java b/encode-and-decode-strings/yhkee0404.java new file mode 100644 index 000000000..367f757e0 --- /dev/null +++ b/encode-and-decode-strings/yhkee0404.java @@ -0,0 +1,44 @@ +public class Solution { + /* + * @param strs: a list of strings + * @return: encodes a list of strings to a single string. + */ + public String encode(List strs) { + // write your code here + return strs.parallelStream() + .map(s -> + s.chars() + .parallel() + .mapToObj(c -> new StringBuilder(String.format("%02x", c))) + .collect( + StringBuilder::new, + StringBuilder::append, + (a, b) -> a.append(b) + ) + ).collect( + StringBuilder::new, + (a, b) -> a.append(b) + .append(' '), + (a, b) -> a.append(b) + ).toString(); + } + + /* + * @param str: A string + * @return: decodes a single string to a list of strings + */ + public List decode(String str) { + // write your code here + final List ans = new ArrayList<>(); + final StringTokenizer st = new StringTokenizer(str); + while (st.hasMoreTokens()) { + final String s = st.nextToken(); + final StringBuilder sb = new StringBuilder(); + for (int i = 0; i < s.length(); i += 2) { + sb.append((char) Integer.parseInt(s.substring(i, i + 2), 16)); + } + ans.add(sb.toString()); + } + return ans; + } +} diff --git a/group-anagrams/yhkee0404.rs b/group-anagrams/yhkee0404.rs new file mode 100644 index 000000000..6ba9c3cb9 --- /dev/null +++ b/group-anagrams/yhkee0404.rs @@ -0,0 +1,30 @@ +use itertools::Itertools; + +impl Solution { + pub fn group_anagrams(strs: Vec) -> Vec> { + let sorted_strings: Vec = strs + .iter() + .map(|s| { + s.chars() + .sorted() + .collect() + }).collect(); + let inverted_sorted_strings: Vec = (0..sorted_strings.len()) + .sorted_by_key(|&i| &sorted_strings[i]) + .collect(); + let mut ans: Vec> = vec![]; + let mut i = 0; + let mut j; + while i != sorted_strings.len() { + let mut u: Vec = vec![]; + j = i; + while j == i || j != inverted_sorted_strings.len() && sorted_strings[inverted_sorted_strings[i]] == sorted_strings[inverted_sorted_strings[j]] { + u.push(strs[inverted_sorted_strings[j]].clone()); + j += 1; + } + ans.push(u); + i = j + } + ans + } +} diff --git a/implement-trie-prefix-tree/yhkee0404.dart b/implement-trie-prefix-tree/yhkee0404.dart new file mode 100644 index 000000000..48dbc276d --- /dev/null +++ b/implement-trie-prefix-tree/yhkee0404.dart @@ -0,0 +1,40 @@ +class Trie { + + final int n; + final children = {}; + bool ended = false; + + Trie([this.n = 0]); + + void insert(String word) { + Trie u = this; + for (int rune in word.runes) { + u = u.children + .putIfAbsent(rune, () => Trie(rune)); + } + u.ended = true; + } + + bool search(String word) => _search(word)?.ended ?? false; + + bool startsWith(String prefix) => _search(prefix) != null; + + Trie? _search(String word) { + Trie? u = this; + for (int rune in word.runes) { + u = u!.children[rune]; + if (u == null) { + return null; + } + } + return u; + } +} + +/** + * Your Trie object will be instantiated and called as such: + * Trie obj = Trie(); + * obj.insert(word); + * bool param2 = obj.search(word); + * bool param3 = obj.startsWith(prefix); + */ diff --git a/word-break/yhkee0404.scala b/word-break/yhkee0404.scala new file mode 100644 index 000000000..a009183cb --- /dev/null +++ b/word-break/yhkee0404.scala @@ -0,0 +1,16 @@ +object Solution { + def wordBreak(s: String, wordDict: List[String]): Boolean = { + val dp = Array.fill(s.length + 1)(false) // S(s, wordDict, word) = O(s.length) + dp(0) = true + (0 to s.length - 1).exists { i => + if (! dp(i)) false + else wordDict.exists { word => + val j = i + word.length + if (j <= s.length && ! dp(j) && s.substring(i, j) == word) { + dp(j) = true // T(s, wordDict, word) = O(s.length * wordDict.length * word.length) + } + dp(s.length) + } + } + } +}