Skip to content

Commit 41435c7

Browse files
authored
Merge pull request #451 from TonyKim9401/main
[TONY] WEEK 5 Solution
2 parents 2624cbe + 6762aa0 commit 41435c7

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

3sum/TonyKim9401.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// TC: O(n^2)
2+
// SC: O(n)
3+
public class Solution {
4+
public List<List<Integer>> threeSum(int[] nums) {
5+
6+
Arrays.sort(nums);
7+
8+
List<List<Integer>> output = new ArrayList<>();
9+
Map<Integer, Integer> map = new HashMap<>();
10+
11+
for (int i = 0; i < nums.length; ++i) map.put(nums[i], i);
12+
13+
for (int i = 0; i < nums.length - 2; ++i) {
14+
if (nums[i] > 0) break;
15+
16+
for (int j = i + 1; j < nums.length - 1; ++j) {
17+
int cValue = -1 * (nums[i] + nums[j]);
18+
19+
if (map.containsKey(cValue) && map.get(cValue) > j) {
20+
output.add(List.of(nums[i], nums[j], cValue));
21+
}
22+
j = map.get(nums[j]);
23+
}
24+
25+
i = map.get(nums[i]);
26+
}
27+
28+
return output;
29+
}
30+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
class Solution {
4+
public int maxProfit(int[] prices) {
5+
int bestProfit = 0;
6+
int buyPrice = prices[0];
7+
for (int i = 1; i < prices.length; i++) {
8+
if (buyPrice > prices[i]) {
9+
buyPrice = prices[i];
10+
continue;
11+
}
12+
bestProfit = Math.max(bestProfit, prices[i] - buyPrice);
13+
}
14+
return bestProfit;
15+
}
16+
}

group-anagrams/TonyKim9401.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// TC: O(n * m log m)
2+
// SC: O(n * m)
3+
class Solution {
4+
public List<List<String>> groupAnagrams(String[] strs) {
5+
List<List<String>> output = new ArrayList<>();
6+
Map<String, List<String>> map = new HashMap<>();
7+
8+
for (int i = 0; i < strs.length; i++) {
9+
char[] charArray = strs[i].toCharArray();
10+
Arrays.sort(charArray);
11+
String target = new String(charArray);
12+
13+
if (map.containsKey(target)) {
14+
map.get(target).add(strs[i]);
15+
} else {
16+
List<String> inside = new ArrayList<>();
17+
inside.add(strs[i]);
18+
map.put(target, inside);
19+
}
20+
}
21+
22+
for (String key : map.keySet()) output.add(map.get(key));
23+
return output;
24+
}
25+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class TrieNode {
2+
TrieNode[] children;
3+
boolean isEndOfWord;
4+
5+
public TrieNode() {
6+
children = new TrieNode[26];
7+
isEndOfWord = false;
8+
}
9+
}
10+
11+
class Trie {
12+
13+
private TrieNode root;
14+
15+
public Trie() {
16+
root = new TrieNode();
17+
}
18+
19+
// TC: O(n)
20+
// SC: O(n * m)
21+
// -> word length * new TrieNode spaces
22+
public void insert(String word) {
23+
TrieNode node = root;
24+
for (char c : word.toCharArray()) {
25+
int idx = c - 'a';
26+
if (node.children[idx] == null) {
27+
node.children[idx] = new TrieNode();
28+
}
29+
node = node.children[idx];
30+
}
31+
node.isEndOfWord = true;
32+
}
33+
34+
// TC: O(n)
35+
// SC: O(1)
36+
public boolean search(String word) {
37+
TrieNode node = searchPrefix(word);
38+
return node != null && node.isEndOfWord;
39+
}
40+
41+
// TC: O(n)
42+
// SC: O(1)
43+
private TrieNode searchPrefix(String word) {
44+
TrieNode node = root;
45+
for (char c : word.toCharArray()) {
46+
int idx = c - 'a';
47+
if (node.children[idx] == null) return null;
48+
node = node.children[idx];
49+
}
50+
return node;
51+
}
52+
53+
public boolean startsWith(String prefix) {
54+
return searchPrefix(prefix) != null;
55+
}
56+
}

word-break/TonyKim9401.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// TC: O(n^2)
2+
// -> use 2 for-loops to search
3+
// SC: O(n)
4+
// -> boolean array's size
5+
class Solution {
6+
public boolean wordBreak(String s, List<String> wordDict) {
7+
Set<String> set = new HashSet(wordDict);
8+
9+
boolean[] dp = new boolean[s.length() + 1];
10+
dp[0] = true;
11+
12+
for (int i = 1; i <= s.length(); i++) {
13+
for (int j = 0; j < i; j++) {
14+
if (dp[j] && set.contains(s.substring(j, i))) {
15+
dp[i] = true;
16+
break;
17+
}
18+
}
19+
}
20+
return dp[s.length()];
21+
}
22+
}

0 commit comments

Comments
 (0)