Skip to content

[minji-go] week 05 solutions #1403

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

Merged
merged 5 commits into from
May 2, 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
18 changes: 18 additions & 0 deletions best-time-to-buy-and-sell-stock/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* <a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">week05-1.best-time-to-buy-and-sell-stock</a>
* <li>Description: Return the maximum profit you can achieve from this transaction</li>
* <li>Topics: Array, Dynamic Programming </li>
* <li>Time Complexity: O(N), Runtime 2ms </li>
* <li>Space Complexity: O(1), Memory 61.62MB </li>
*/
class Solution {
public int maxProfit(int[] prices) {
int min = prices[0];
int profit = 0;
for(int i=1; i<prices.length; i++) {
profit = Math.max(profit, prices[i]-min);
min = Math.min(min, prices[i]);
}
return profit;
}
}
23 changes: 23 additions & 0 deletions group-anagrams/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* <a href="https://leetcode.com/problems/group-anagrams/">week05-2.group-anagrams</a>
* <li>Description: Given an array of strings strs, group the anagrams together</li>
* <li>Topics: Array, Hash Table, String, Sorting</li>
* <li>Time Complexity: O(N*KLogK), Runtime 6ms </li>
* <li>Space Complexity: O(N*K), Memory 47.82MB </li>
*/
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> mapOfAnagrams = new HashMap<>();

for(String str : strs) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
String key = new String(chars);

List<String> anagrams = mapOfAnagrams.computeIfAbsent(key, k -> new ArrayList<>());
anagrams.add(str);
}

return new ArrayList<>(mapOfAnagrams.values());
}
}
44 changes: 44 additions & 0 deletions implement-trie-prefix-tree/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* <a href="https://leetcode.com/problems/implement-trie-prefix-tree/">week05-3.implement-trie-prefix-tree</a>
* <li>Description: Implement the Trie class </li>
* <li>Topics: Hash Table, String, Design, Trie </li>
* <li>Time Complexity: O(N*K), Runtime 40ms </li>
* <li>Space Complexity: O(N*K), Memory 56.07MB </li>
*/
class Trie {
private boolean end;
private final Map<Character, Trie> children;

public Trie() {
children = new HashMap<>();
}

public void insert(String word) {
Trie node = this;
for(Character c : word.toCharArray()) {
node = node.children.computeIfAbsent(c, k -> new Trie());
}
node.end = true;
}

public boolean search(String word) {
Trie node = this;
for(Character c : word.toCharArray()) {
node = node.children.get(c);
if(node == null) {
return false;
}
}
return node.end;
}

public boolean startsWith(String prefix) {
Trie node = this;
for(Character c : prefix.toCharArray()) {
node = node.children.get(c);
if(node == null) {
return false;
}
}
return node != null;
}
28 changes: 28 additions & 0 deletions validate-binary-search-tree/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* <a href="https://leetcode.com/problems/validate-binary-search-tree/">week02-5.validate-binary-search-tree</a>
* <li>Description: determine if it is a valid binary search tree (BST). </li>
* <li>Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree </li>
* <li>Time Complexity: O(N), Runtime 0ms </li>
* <li>Space Complexity: O(N), Memory 43.25MB </li>
*/
class Solution {
public boolean isValidBST(TreeNode root) {
return isValid(Integer.MIN_VALUE, Integer.MAX_VALUE, root);
}

public boolean isValid(long min, long max, TreeNode node) {
if(node == null) {
return true;
}

if (node.val < min || node.val > max) {
return false;
}

return isValid(min, (long) node.val - 1, node.left)
&& isValid((long) node.val + 1, max, node.right);
}

}


53 changes: 53 additions & 0 deletions word-break/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* <a href="https://leetcode.com/problems/word-break/">week05-4.word-break</a>
* <li>Description: return true if s can be segmented into a space-separated sequence of one or more dictionary words.</li>
* <li>Topics: Array, Hash Table, String, Dynamic Programming, Trie, Memoization</li>
* <li>Time Complexity: O(N³), Runtime 7ms </li>
* <li>Space Complexity: O(N+W), Memory 44.59MB </li>
*/
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
Trie trie = new Trie();
for (String str : wordDict) {
trie.insert(str);
}

List<Integer> dp = new ArrayList<>();
dp.add(-1);
for (int i = 0; i < s.length(); i++) {
for (int j = dp.size() - 1; j >= 0; j--) {
int startIndex = dp.get(j);
if (trie.search(s.substring(startIndex + 1, i + 1))) {
dp.add(i);
break;
}
}
}

return dp.contains(s.length() - 1);
}

class Trie {
private boolean end;
private final Map<Character, Trie> children = new HashMap<>();

public void insert(String word) {
Trie node = this;
for (Character c : word.toCharArray()) {
node = node.children.computeIfAbsent(c, k -> new Trie());
}
node.end = true;
}

public boolean search(String word) {
Trie node = this;
for (Character c : word.toCharArray()) {
node = node.children.get(c);
if (node == null) {
return false;
}
}
return node.end;
}
}
}