Skip to content

Commit 7ed9255

Browse files
committed
update java solution
1 parent 1ea255c commit 7ed9255

File tree

7 files changed

+232
-5
lines changed

7 files changed

+232
-5
lines changed

README.md

+5-5
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class Trie {
2+
3+
TrieNode root;
4+
5+
public Trie() {
6+
root = new TrieNode();
7+
}
8+
9+
public void insert(String word) {
10+
TrieNode node = root;
11+
for (int i = 0; i < word.length(); i++) {
12+
Character c = word.charAt(i);
13+
if (!node.children.containsKey(c)) {
14+
node.children.put(c, new TrieNode());
15+
}
16+
node = node.children.get(c);
17+
}
18+
node.isWord = true;
19+
}
20+
21+
public boolean search(String word) {
22+
TrieNode node = root;
23+
for (int i = 0; i < word.length(); i++) {
24+
Character c = word.charAt(i);
25+
if (!node.children.containsKey(c)) {
26+
return false;
27+
}
28+
node = node.children.get(c);
29+
}
30+
return node.isWord;
31+
}
32+
33+
public boolean startsWith(String prefix) {
34+
TrieNode node = root;
35+
for (int i = 0; i < prefix.length(); i++) {
36+
Character c = prefix.charAt(i);
37+
if (!node.children.containsKey(c)) {
38+
return false;
39+
}
40+
node = node.children.get(c);
41+
}
42+
return true;
43+
}
44+
}
45+
46+
class TrieNode {
47+
HashMap<Character, TrieNode> children;
48+
boolean isWord;
49+
50+
public TrieNode() {
51+
children = new HashMap<>();
52+
isWord = false;
53+
}
54+
}
55+
56+
/**
57+
* Your Trie object will be instantiated and called as such:
58+
* Trie obj = new Trie();
59+
* obj.insert(word);
60+
* boolean param_2 = obj.search(word);
61+
* boolean param_3 = obj.startsWith(prefix);
62+
*/
63+
64+
// time O(L) for insert(), search(), startsWith(), L is the word's length
65+
// space O(nL), n is the number of words, L is the max len
66+
// using trie and standard trie
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int[] productExceptSelf(int[] nums) {
3+
int[] res = new int[nums.length];
4+
Arrays.fill(res, 1);
5+
int total = 1;
6+
for (int i = 0; i < nums.length; i++) {
7+
res[i] = total;
8+
total *= nums[i];
9+
}
10+
total = 1;
11+
for (int i = nums.length - 1; i >= 0; i--) {
12+
res[i] *= total;
13+
total *= nums[i];
14+
}
15+
return res;
16+
}
17+
}
18+
19+
// time O(n)
20+
// space O(1), not counting output list
21+
// using array and prefix sum and standard prefix sum
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class MinStack {
2+
ArrayDeque<Long> stack;
3+
long min;
4+
5+
public MinStack() {
6+
stack = new ArrayDeque<>();
7+
min = 0;
8+
}
9+
10+
public void push(int val) {
11+
if (stack.isEmpty()) {
12+
min = val;
13+
stack.push(0L);
14+
} else {
15+
long diff = val - min;
16+
if (diff < 0) {
17+
min = val;
18+
stack.push(diff);
19+
} else {
20+
stack.push(diff);
21+
}
22+
}
23+
}
24+
25+
public void pop() {
26+
Long diff = stack.peek();
27+
if (diff < 0) {
28+
min += Math.abs(diff);
29+
stack.pop();
30+
} else {
31+
stack.pop();
32+
}
33+
if (stack.isEmpty()) {
34+
min = 0;
35+
}
36+
}
37+
38+
public int top() {
39+
Long diff = stack.peek();
40+
if (diff < 0) {
41+
return (int) min;
42+
}
43+
return (int) (min + diff);
44+
}
45+
46+
public int getMin() {
47+
return (int) min;
48+
}
49+
}
50+
51+
/**
52+
* Your MinStack object will be instantiated and called as such:
53+
* MinStack obj = new MinStack();
54+
* obj.push(val);
55+
* obj.pop();
56+
* int param_3 = obj.top();
57+
* int param_4 = obj.getMin();
58+
*/
59+
60+
// time O(1)
61+
// space O(n)
62+
// using stack and queue and implement stack/queue and one stack
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public boolean canFinish(int numCourses, int[][] prerequisites) {
3+
HashMap<Integer, List<Integer>> graph = new HashMap<>();
4+
HashMap<Integer, Integer> indegrees = new HashMap<>();
5+
for (int i = 0; i < numCourses; i++) {
6+
graph.put(i, new ArrayList<>());
7+
indegrees.put(i, 0);
8+
}
9+
for (int[] prereq: prerequisites) {
10+
int q = prereq[0];
11+
int p = prereq[1];
12+
graph.get(p).add(q);
13+
indegrees.put(q, indegrees.get(q) + 1);
14+
}
15+
16+
ArrayDeque<Integer> queue = new ArrayDeque<>();
17+
for (Integer node: indegrees.keySet()) {
18+
if (Objects.equals(indegrees.get(node), 0)) {
19+
queue.offer(node);
20+
}
21+
}
22+
ArrayList<Integer> res = new ArrayList<>();
23+
while (!queue.isEmpty()) {
24+
Integer node = queue.poll();
25+
res.add(node);
26+
for (Integer neighbor: graph.get(node)) {
27+
indegrees.put(neighbor, indegrees.get(neighbor) - 1);
28+
if (Objects.equals(indegrees.get(neighbor), 0)) {
29+
queue.offer(neighbor);
30+
}
31+
}
32+
}
33+
return res.size() == numCourses;
34+
}
35+
}
36+
37+
// time O(V+E)
38+
// space O(V+E), due to building graph
39+
// using graph and kahn and topological sort
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int coinChange(int[] coins, int amount) {
3+
int[] dp = new int[amount + 1];
4+
int max = Integer.MAX_VALUE;
5+
Arrays.fill(dp, max);
6+
dp[0] = 0;
7+
8+
for (int i = 0; i < coins.length; i++) {
9+
for (int j = 1; j < amount + 1; j++) {
10+
if (coins[i] <= j) {
11+
int cand = dp[j - coins[i]] == max ? max : dp[j - coins[i]] + 1;
12+
dp[j] = Math.min(dp[j], cand);
13+
}
14+
}
15+
}
16+
return dp[amount] == max ? - 1 : dp[amount];
17+
}
18+
}
19+
20+
// time O(m * n), m is the amount and n is the number of coins
21+
// space O(m), due to the dp list's size
22+
// using dynamic programming and knapsack (complete knapsack)

[Q]basic/basic.md

+17
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,23 @@ public class Main {
20892089
System.out.println(intFromString); // 1001
20902090
System.out.println("-----");
20912091

2092+
// char from int in O(1)
2093+
int asciiForA = 65;
2094+
char charA = (char) asciiForA;
2095+
System.out.println(charA); // A
2096+
System.out.println("-----");
2097+
2098+
// char to int in O(1)
2099+
char charZ = 'Z';
2100+
int asciiForZ = charZ + 0;
2101+
System.out.println(asciiForZ); // 90
2102+
System.out.println("-----");
2103+
2104+
// char to String in O(1)
2105+
String stringZ = charZ + "";
2106+
System.out.println(stringZ); // Z
2107+
System.out.println("-----");
2108+
20922109
// char related methods
20932110
char c1 = 'a';
20942111
char c2 = 'A';

0 commit comments

Comments
 (0)