Skip to content

Commit 4a9e658

Browse files
committed
Solution is Added
1 parent 8670f6f commit 4a9e658

File tree

28 files changed

+442
-0
lines changed

28 files changed

+442
-0
lines changed
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [820. Short Encoding of Words](https://leetcode.com/problems/short-encoding-of-words/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.Arrays;
2+
import java.util.HashSet;
3+
import java.util.Set;
4+
5+
class ShortEncodingOfWords {
6+
public int minimumLengthEncoding(String[] words) {
7+
Set<String> wordSet = new HashSet<>(Arrays.asList(words));
8+
for (String s : words) {
9+
for (int i = 1; i < s.length(); i++) {
10+
wordSet.remove(s.substring(i));
11+
}
12+
}
13+
14+
int length = 0;
15+
for (String s : wordSet) {
16+
length += s.length() + 1;
17+
}
18+
return length;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.Arrays;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
5+
class BinaryTreesWithFactors {
6+
private final int mod = (int) (Math.pow(10, 9) + 7);
7+
8+
public int numFactoredBinaryTrees(int[] arr) {
9+
if (arr.length == 0) return 0;
10+
11+
Arrays.sort(arr);
12+
Map<Integer, Long> map = new HashMap<>();
13+
14+
for (int i = 0; i < arr.length; i++) {
15+
long count = 1L;
16+
for (int j = 0; j < i; j++) {
17+
if (arr[i] % arr[j] == 0 && map.containsKey(arr[i] / arr[j])) {
18+
count += map.get(arr[j]) * map.get(arr[i] / arr[j]);
19+
}
20+
}
21+
map.put(arr[i], count);
22+
}
23+
24+
long totalCount = 0L;
25+
for (Map.Entry<Integer, Long> entry : map.entrySet()) {
26+
totalCount += entry.getValue();
27+
}
28+
29+
return (int) (totalCount % mod);
30+
}
31+
}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [823. Binary Trees With Factors](https://leetcode.com/problems/binary-trees-with-factors/)
2+
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [834. Sum of Distances in Tree](https://leetcode.com/problems/sum-of-distances-in-tree/)
2+
3+
4+
5+
Complexity Analysis:
6+
7+
- Time Complexity: $O(n)$. $n$ is the number of tree nodes. We only need to traverse nodes twice to get the answer, so the time complexity is $O(2n)=O(n)$.
8+
- Space Complexity: $O(n)$. We have three arrays, dp, size and ans. The space complexity is $O(3n)=O(n)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
class SumOfDistancesInTree {
5+
int[] ans, size, dp;
6+
List<List<Integer>> graph;
7+
8+
public int[] sumOfDistancesInTree(int n, int[][] edges) {
9+
ans = new int[n];
10+
size = new int[n];
11+
dp = new int[n];
12+
graph = new ArrayList<>();
13+
for (int i = 0; i < n; i++)
14+
graph.add(new ArrayList<>());
15+
for (int[] edge : edges) {
16+
graph.get(edge[0]).add(edge[1]);
17+
graph.get(edge[1]).add(edge[0]);
18+
}
19+
dfs(0, -1);
20+
dfs2(0, -1);
21+
return ans;
22+
}
23+
24+
public void dfs(int u, int f) {
25+
size[u] = 1;
26+
dp[u] = 0;
27+
for (int v : graph.get(u)) {
28+
if (v == f) continue;
29+
dfs(v, u);
30+
dp[u] += dp[v] + size[v];
31+
size[u] += size[v];
32+
}
33+
}
34+
35+
public void dfs2(int u, int f) {
36+
ans[u] = dp[u];
37+
for (int v : graph.get(u)) {
38+
if (v == f) continue;
39+
int parentU = dp[u], parentV = dp[v];
40+
int childU = size[u], childV = size[v];
41+
42+
dp[u] -= dp[v] + size[v];
43+
size[u] -= size[v];
44+
dp[v] += dp[u] + size[u];
45+
size[v] += size[u];
46+
47+
dfs2(v, u);
48+
49+
dp[u] = parentU;
50+
dp[v] = parentV;
51+
size[u] = childU;
52+
size[v] = childV;
53+
}
54+
}
55+
}

0835.image-overlap/ImageOverlap.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class ImageOverlap {
2+
private int shiftAndCount(int xShift, int yShift, int[][] matrix, int[][] reference) {
3+
int leftShiftCount = 0, rightShiftCount = 0;
4+
int rRow = 0;
5+
for (int mRow = yShift; mRow < matrix.length; mRow++) {
6+
int rCol = 0;
7+
for (int mCol = xShift; mCol < matrix.length; mCol++) {
8+
if (matrix[mRow][mCol] == 1 && matrix[mRow][mCol] == reference[rRow][rCol])
9+
leftShiftCount += 1;
10+
if (matrix[mRow][rCol] == 1 && matrix[mRow][rCol] == reference[rRow][mCol])
11+
rightShiftCount += 1;
12+
rCol += 1;
13+
}
14+
rRow += 1;
15+
}
16+
return Math.max(leftShiftCount, rightShiftCount);
17+
}
18+
19+
public int largestOverlap(int[][] img1, int[][] img2) {
20+
int maxOverlaps = 0;
21+
for (int yShift = 0; yShift < img1.length; yShift++) {
22+
for (int xShift = 0; xShift < img1.length; xShift++) {
23+
maxOverlaps = Math.max(maxOverlaps, shiftAndCount(xShift, yShift, img1, img2));
24+
maxOverlaps = Math.max(maxOverlaps, shiftAndCount(xShift, yShift, img2, img1));
25+
}
26+
}
27+
return maxOverlaps;
28+
}
29+
}

0835.image-overlap/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [835. Image Overlap](https://leetcode.com/problems/image-overlap/)
2+

0838.push-dominoes/PushDominoes.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class PushDominoes {
2+
public String pushDominoes(String dominoes) {
3+
int n = dominoes.length();
4+
int[] indexes = new int[n + 2];
5+
char[] symbols = new char[n + 2];
6+
int len = 1;
7+
indexes[0] = -1;
8+
symbols[0] = 'L';
9+
10+
for (int i = 0; i < n; i++) {
11+
if (dominoes.charAt(i) != '.') {
12+
indexes[len] = i;
13+
symbols[len++] = dominoes.charAt(i);
14+
}
15+
}
16+
17+
indexes[len] = n;
18+
symbols[len++] = 'R';
19+
20+
char[] res = dominoes.toCharArray();
21+
for (int index = 0; index < len - 1; index++) {
22+
int i = indexes[index], j = indexes[index + 1];
23+
char x = symbols[index], y = symbols[index + 1];
24+
if (x == y) {
25+
for (int k = i + 1; k < j; k++) {
26+
res[k] = x;
27+
}
28+
} else if (x > y) {
29+
for (int k = i + 1; k < j; k++) {
30+
res[k] = k - i == j - k ? '.' : k - i < j - k ? 'R' : 'L';
31+
}
32+
}
33+
}
34+
return String.valueOf(res);
35+
}
36+
}

0838.push-dominoes/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [838. Push Dominoes](https://leetcode.com/problems/push-dominoes/)
2+

0841.keys-and-rooms/KeysAndRooms.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.LinkedList;
2+
import java.util.List;
3+
import java.util.Queue;
4+
5+
class KeysAndRooms {
6+
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
7+
int n = rooms.size(), num = 0;
8+
boolean[] visited = new boolean[n];
9+
Queue<Integer> queue = new LinkedList<>();
10+
queue.offer(0);
11+
visited[0] = true;
12+
while (!queue.isEmpty()) {
13+
int roomNumber = queue.poll();
14+
num++;
15+
for (int key : rooms.get(roomNumber)) {
16+
if (!visited[key]) {
17+
visited[key] = true;
18+
queue.offer(key);
19+
}
20+
}
21+
}
22+
return num == n;
23+
}
24+
}

0841.keys-and-rooms/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [841. Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n+m)$. $n$ is the room number and $m$ is the number of the keys in all rooms.
7+
- Space Complexity: $O(n)$. We used $n$ which is the number of room for the stack of recursion.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Stack;
2+
3+
class BackspaceStringCompare {
4+
private String filterString(String s) {
5+
Stack<Character> stack = new Stack<>();
6+
for (char c : s.toCharArray()) {
7+
stack.push(c);
8+
}
9+
StringBuilder res = new StringBuilder();
10+
int popCount = 0;
11+
while (!stack.isEmpty()) {
12+
if (stack.peek() == '#') {
13+
popCount++;
14+
stack.pop();
15+
} else if (popCount != 0) {
16+
stack.pop();
17+
popCount--;
18+
} else {
19+
res.append(stack.pop());
20+
}
21+
}
22+
return res.toString();
23+
}
24+
25+
public boolean backspaceCompare(String s, String t) {
26+
String newS = filterString(s);
27+
String newT = filterString(t);
28+
return newS.equals(newT);
29+
}
30+
}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [844. Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class MirrorReflection {
2+
public int mirrorReflection(int p, int q) {
3+
while (p % 2 == 0 && q % 2 == 0) {
4+
p /= 2;
5+
q /= 2;
6+
}
7+
if (p % 2 == 0) return 2;
8+
if (q % 2 == 0) return 0;
9+
return 1;
10+
}
11+
}

0858.mirror-reflection/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [858. Mirror Reflection](https://leetcode.com/problems/mirror-reflection/)

0869.reordered-power-of-2/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [869. Reordered Power of 2](https://leetcode.com/problems/reordered-power-of-2/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.Arrays;
2+
3+
class ReorderedPowerOf2 {
4+
private int[] count(int n) {
5+
int[] res = new int[10];
6+
while (n > 0) {
7+
res[n % 10]++;
8+
n /= 10;
9+
}
10+
return res;
11+
}
12+
13+
public boolean reorderedPowerOf2(int n) {
14+
int[] arrays = count(n);
15+
for (int i = 0; i < 31; i++) {
16+
if (Arrays.equals(arrays, count(1 << i))) {
17+
return true;
18+
}
19+
}
20+
return false;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class MinimumNumberOfRefuelingStops {
2+
public int minRefuelStops(int target, int startFuel, int[][] stations) {
3+
long[] dp = new long[stations.length + 1];
4+
dp[0] = startFuel;
5+
6+
for (int i = 0; i < stations.length; i++) {
7+
for (int refill = i; refill >= 0 && dp[refill] >= stations[i][0]; refill--) {
8+
dp[refill + 1] = Math.max(dp[refill + 1], dp[refill] + stations[i][1]);
9+
}
10+
}
11+
12+
for (int i = 0; i <= stations.length; i++) {
13+
if (dp[i] >= target) return i;
14+
}
15+
16+
return -1;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [871. Minimum Number of Refueling Stops](https://leetcode.com/problems/minimum-number-of-refueling-stops/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* public class TreeNode {
7+
* int val;
8+
* TreeNode left;
9+
* TreeNode right;
10+
* TreeNode() {}
11+
* TreeNode(int val) { this.val = val; }
12+
* TreeNode(int val, TreeNode left, TreeNode right) {
13+
* this.val = val;
14+
* this.left = left;
15+
* this.right = right;
16+
* }
17+
* }
18+
*/
19+
class LeafSimilarTrees {
20+
private void dfs(TreeNode root, List<Integer> seq) {
21+
if (root.left == null && root.right == null)
22+
seq.add(root.val);
23+
else {
24+
if (root.left != null)
25+
dfs(root.left, seq);
26+
if (root.right != null)
27+
dfs(root.right, seq);
28+
}
29+
}
30+
31+
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
32+
List<Integer> seq1 = new ArrayList<>();
33+
if (root1 != null)
34+
dfs(root1, seq1);
35+
36+
List<Integer> seq2 = new ArrayList<>();
37+
if (root2 != null)
38+
dfs(root2, seq2);
39+
40+
return seq1.equals(seq2);
41+
}
42+
}

0872.leaf-similar-trees/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [872. Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/)
2+
3+
4+
5+
Complexity Analysis:
6+
7+
- Time Complexity: $O(n_1+n_2)$. $n_1$ and $n_2$ are the number of nodes in the two trees respectively.
8+
- Space Complexity: $O(n_1+n_2)$. Space complexity depends on the stack size of the recursion.

0 commit comments

Comments
 (0)