Skip to content

Commit 634e572

Browse files
authored
Merge pull request #1403 from minji-go/week05
[minji-go] week 05 solutions
2 parents 660413f + b53a31e commit 634e572

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">week05-1.best-time-to-buy-and-sell-stock</a>
3+
* <li>Description: Return the maximum profit you can achieve from this transaction</li>
4+
* <li>Topics: Array, Dynamic Programming </li>
5+
* <li>Time Complexity: O(N), Runtime 2ms </li>
6+
* <li>Space Complexity: O(1), Memory 61.62MB </li>
7+
*/
8+
class Solution {
9+
public int maxProfit(int[] prices) {
10+
int min = prices[0];
11+
int profit = 0;
12+
for(int i=1; i<prices.length; i++) {
13+
profit = Math.max(profit, prices[i]-min);
14+
min = Math.min(min, prices[i]);
15+
}
16+
return profit;
17+
}
18+
}

group-anagrams/minji-go.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/group-anagrams/">week05-2.group-anagrams</a>
3+
* <li>Description: Given an array of strings strs, group the anagrams together</li>
4+
* <li>Topics: Array, Hash Table, String, Sorting</li>
5+
* <li>Time Complexity: O(N*KLogK), Runtime 6ms </li>
6+
* <li>Space Complexity: O(N*K), Memory 47.82MB </li>
7+
*/
8+
class Solution {
9+
public List<List<String>> groupAnagrams(String[] strs) {
10+
Map<String, List<String>> mapOfAnagrams = new HashMap<>();
11+
12+
for(String str : strs) {
13+
char[] chars = str.toCharArray();
14+
Arrays.sort(chars);
15+
String key = new String(chars);
16+
17+
List<String> anagrams = mapOfAnagrams.computeIfAbsent(key, k -> new ArrayList<>());
18+
anagrams.add(str);
19+
}
20+
21+
return new ArrayList<>(mapOfAnagrams.values());
22+
}
23+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/implement-trie-prefix-tree/">week05-3.implement-trie-prefix-tree</a>
3+
* <li>Description: Implement the Trie class </li>
4+
* <li>Topics: Hash Table, String, Design, Trie </li>
5+
* <li>Time Complexity: O(N*K), Runtime 40ms </li>
6+
* <li>Space Complexity: O(N*K), Memory 56.07MB </li>
7+
*/
8+
class Trie {
9+
private boolean end;
10+
private final Map<Character, Trie> children;
11+
12+
public Trie() {
13+
children = new HashMap<>();
14+
}
15+
16+
public void insert(String word) {
17+
Trie node = this;
18+
for(Character c : word.toCharArray()) {
19+
node = node.children.computeIfAbsent(c, k -> new Trie());
20+
}
21+
node.end = true;
22+
}
23+
24+
public boolean search(String word) {
25+
Trie node = this;
26+
for(Character c : word.toCharArray()) {
27+
node = node.children.get(c);
28+
if(node == null) {
29+
return false;
30+
}
31+
}
32+
return node.end;
33+
}
34+
35+
public boolean startsWith(String prefix) {
36+
Trie node = this;
37+
for(Character c : prefix.toCharArray()) {
38+
node = node.children.get(c);
39+
if(node == null) {
40+
return false;
41+
}
42+
}
43+
return node != null;
44+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/validate-binary-search-tree/">week02-5.validate-binary-search-tree</a>
3+
* <li>Description: determine if it is a valid binary search tree (BST). </li>
4+
* <li>Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree </li>
5+
* <li>Time Complexity: O(N), Runtime 0ms </li>
6+
* <li>Space Complexity: O(N), Memory 43.25MB </li>
7+
*/
8+
class Solution {
9+
public boolean isValidBST(TreeNode root) {
10+
return isValid(Integer.MIN_VALUE, Integer.MAX_VALUE, root);
11+
}
12+
13+
public boolean isValid(long min, long max, TreeNode node) {
14+
if(node == null) {
15+
return true;
16+
}
17+
18+
if (node.val < min || node.val > max) {
19+
return false;
20+
}
21+
22+
return isValid(min, (long) node.val - 1, node.left)
23+
&& isValid((long) node.val + 1, max, node.right);
24+
}
25+
26+
}
27+
28+

word-break/minji-go.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/word-break/">week05-4.word-break</a>
3+
* <li>Description: return true if s can be segmented into a space-separated sequence of one or more dictionary words.</li>
4+
* <li>Topics: Array, Hash Table, String, Dynamic Programming, Trie, Memoization</li>
5+
* <li>Time Complexity: O(N³), Runtime 7ms </li>
6+
* <li>Space Complexity: O(N+W), Memory 44.59MB </li>
7+
*/
8+
class Solution {
9+
public boolean wordBreak(String s, List<String> wordDict) {
10+
Trie trie = new Trie();
11+
for (String str : wordDict) {
12+
trie.insert(str);
13+
}
14+
15+
List<Integer> dp = new ArrayList<>();
16+
dp.add(-1);
17+
for (int i = 0; i < s.length(); i++) {
18+
for (int j = dp.size() - 1; j >= 0; j--) {
19+
int startIndex = dp.get(j);
20+
if (trie.search(s.substring(startIndex + 1, i + 1))) {
21+
dp.add(i);
22+
break;
23+
}
24+
}
25+
}
26+
27+
return dp.contains(s.length() - 1);
28+
}
29+
30+
class Trie {
31+
private boolean end;
32+
private final Map<Character, Trie> children = new HashMap<>();
33+
34+
public void insert(String word) {
35+
Trie node = this;
36+
for (Character c : word.toCharArray()) {
37+
node = node.children.computeIfAbsent(c, k -> new Trie());
38+
}
39+
node.end = true;
40+
}
41+
42+
public boolean search(String word) {
43+
Trie node = this;
44+
for (Character c : word.toCharArray()) {
45+
node = node.children.get(c);
46+
if (node == null) {
47+
return false;
48+
}
49+
}
50+
return node.end;
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)