Skip to content

Commit 8670f6f

Browse files
committed
Solution is Added By anjha
1 parent fec9950 commit 8670f6f

File tree

24 files changed

+396
-0
lines changed

24 files changed

+396
-0
lines changed

0766.toeplitz-matrix/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [766. Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/)
2+
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class ToeplitzMatrix {
2+
private boolean check(int[][] matrix, int expectValue, int row, int col) {
3+
if (row >= matrix.length || col >= matrix[0].length) {
4+
return true;
5+
}
6+
if (matrix[row][col] != expectValue) {
7+
return false;
8+
}
9+
return check(matrix, expectValue, row + 1, col + 1);
10+
}
11+
12+
public boolean isToeplitzMatrix(int[][] matrix) {
13+
for (int i = 0; i < matrix[0].length; i++) {
14+
if (!check(matrix, matrix[0][i], 0, i)) {
15+
return false;
16+
}
17+
}
18+
for (int i = 1; i < matrix.length; i++) {
19+
if (!check(matrix, matrix[i][0], i, 0)) {
20+
return false;
21+
}
22+
}
23+
return true;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class MaxChunksToMakeSorted {
2+
public int maxChunksToSorted(int[] arr) {
3+
if (arr == null) return 0;
4+
int ret = 0;
5+
int right = arr[0];
6+
for (int i = 0; i < arr.length; i++) {
7+
right = Math.max(right, arr[i]);
8+
if (i == right) ret++;
9+
}
10+
return ret;
11+
}
12+
}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [769. Max Chunks To Make Sorted](https://leetcode.com/problems/max-chunks-to-make-sorted/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class MinimumDistanceBetweenBstNodes {
17+
int ans, pre;
18+
19+
public int minDiffInBST(TreeNode root) {
20+
ans = Integer.MAX_VALUE;
21+
pre = -1;
22+
dfs(root);
23+
return ans;
24+
}
25+
26+
private void dfs(TreeNode root) {
27+
if (root == null)
28+
return;
29+
30+
dfs(root.left);
31+
if (pre != -1) {
32+
ans = Math.min(ans, root.val - pre);
33+
}
34+
pre = root.val;
35+
dfs(root.right);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [783. Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)
2+
3+
4+
5+
Complexity Analysis:
6+
7+
- Time Complexity: $O(n)$. $n$ is the nodes of tree.
8+
- Space Complexity: $O(n)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
class LetterCasePermutation {
5+
private void backtrack(char[] str, int pos, List<String> res) {
6+
if (pos == str.length) {
7+
res.add(new String(str));
8+
return;
9+
}
10+
11+
if (Character.isLetter(str[pos])) {
12+
if (Character.isUpperCase(str[pos])) {
13+
str[pos] = Character.toLowerCase(str[pos]);
14+
backtrack(str, pos + 1, res);
15+
str[pos] = Character.toUpperCase(str[pos]);
16+
} else {
17+
str[pos] = Character.toUpperCase(str[pos]);
18+
backtrack(str, pos + 1, res);
19+
str[pos] = Character.toLowerCase(str[pos]);
20+
}
21+
}
22+
backtrack(str, pos + 1, res);
23+
}
24+
25+
public List<String> letterCasePermutation(String s) {
26+
List<String> res = new ArrayList<>();
27+
backtrack(s.toCharArray(), 0, res);
28+
return res;
29+
}
30+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [784. Letter Case Permutation](https://leetcode.com/problems/letter-case-permutation/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.Arrays;
2+
3+
class IsGraphBipartite {
4+
private boolean isBipartite(int curNode, int curColor, int[] colors, int[][] graph) {
5+
if (colors[curNode] != -1) {
6+
return colors[curNode] == curColor;
7+
}
8+
colors[curNode] = curColor;
9+
for (int nextNode : graph[curNode]) {
10+
if (!isBipartite(nextNode, 1 - curColor, colors, graph)) {
11+
return false;
12+
}
13+
}
14+
return true;
15+
}
16+
17+
public boolean isBipartite(int[][] graph) {
18+
int[] colors = new int[graph.length];
19+
Arrays.fill(colors, -1);
20+
for (int i = 0; i < graph.length; i++) {
21+
if (colors[i] == -1 && !isBipartite(i, 0, colors, graph)) {
22+
return false;
23+
}
24+
}
25+
return true;
26+
}
27+
}

0785.is-graph-bipartite/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [785. Is Graph Bipartite?](https://leetcode.com/problems/is-graph-bipartite/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.Arrays;
2+
3+
class CheapestFlightsWithinKStops {
4+
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
5+
final int inf = 10000 * 101 + 1;
6+
int[][] f = new int[k + 2][n];
7+
for (int i = 0; i < k + 2; i++)
8+
Arrays.fill(f[i], inf);
9+
f[0][src] = 0;
10+
for (int t = 1; t <= k + 1; t++) {
11+
for (int[] flight : flights) {
12+
int j = flight[0], i = flight[1], cost = flight[2];
13+
f[t][i] = Math.min(f[t][i], f[t - 1][j] + cost);
14+
}
15+
}
16+
int ans = inf;
17+
for (int t = 1; t <= k + 1; t++)
18+
ans = Math.min(ans, f[t][dst]);
19+
20+
return ans == inf ? -1 : ans;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [787. Cheapest Flights Within K Stops](https://leetcode.com/problems/cheapest-flights-within-k-stops/)
2+
3+
4+
5+
Complexity Analysis:
6+
7+
- Time Complexity: $O((m+n)\times k)$. $m$ is the length of `flights`. We first use $O(nk)$ to generate the `f` array, and use $O(mk)$ to iterate all `flights`. Thus, the total time complexity is $O(m+n)\times k)$.
8+
- Space Complexity: $O(nk)$. The size of `f` is $O(nk)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class DominoAndTrominoTiling {
2+
static final int modulo = 1000000007;
3+
4+
public int numTilings(int n) {
5+
int[][] dp = new int[n + 1][4];
6+
dp[0][3] = 1;
7+
for (int i = 1; i <= n; i++) {
8+
dp[i][0] = dp[i - 1][3];
9+
dp[i][1] = (dp[i - 1][0] + dp[i - 1][2]) % modulo;
10+
dp[i][2] = (dp[i - 1][0] + dp[i - 1][1]) % modulo;
11+
dp[i][3] = (((dp[i - 1][0] + dp[i - 1][1]) % modulo + dp[i - 1][2]) % modulo + dp[i - 1][3]) % modulo;
12+
}
13+
return dp[n][3];
14+
}
15+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [790. Domino and Tromino Tiling](https://leetcode.com/problems/domino-and-tromino-tiling/)
2+
3+
4+
5+
Complexity Analysis:
6+
7+
- Time Complexity: $O(n)$. $n$ is the total columns.
8+
- Space Complexity: $O(n)$. We use $4n$ to save DP array, so the space complexity is $O(4n)=O(n)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.HashMap;
2+
import java.util.LinkedList;
3+
import java.util.Map;
4+
import java.util.Queue;
5+
6+
class NumberOfMatchingSubsequences {
7+
public int numMatchingSubseq(String s, String[] words) {
8+
Map<Character, Queue<String>> map = new HashMap<>();
9+
int ans = 0;
10+
11+
for (int i = 0; i < s.length(); i++) {
12+
map.putIfAbsent(s.charAt(i), new LinkedList<>());
13+
}
14+
15+
for (String word : words) {
16+
char startChar = word.charAt(0);
17+
if (map.containsKey(startChar)) {
18+
map.get(startChar).offer(word);
19+
}
20+
}
21+
22+
for (int i = 0; i < s.length(); i++) {
23+
char startChar = s.charAt(i);
24+
Queue<String> q = map.get(startChar);
25+
int size = q.size();
26+
for (int j = 0; j < size; j++) {
27+
String str = q.poll();
28+
if (str != null) {
29+
if (str.substring(1).length() == 0) ans++;
30+
else {
31+
if (map.containsKey(str.charAt(1))) {
32+
map.get(str.charAt(1)).add(str.substring(1));
33+
}
34+
}
35+
}
36+
}
37+
}
38+
return ans;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [792. Number of Matching Subsequences](https://leetcode.com/problems/number-of-matching-subsequences/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.ArrayDeque;
2+
import java.util.ArrayList;
3+
import java.util.Deque;
4+
import java.util.List;
5+
6+
class AllPathsFromSourceToTarget {
7+
List<List<Integer>> ans = new ArrayList<>();
8+
Deque<Integer> stack = new ArrayDeque<>();
9+
10+
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
11+
stack.offerLast(0);
12+
dfs(graph, 0, graph.length - 1);
13+
return ans;
14+
}
15+
16+
private void dfs(int[][] graph, int x, int n) {
17+
if (x == n) {
18+
ans.add(new ArrayList<>(stack));
19+
return;
20+
}
21+
for (int y : graph[x]) {
22+
stack.offerLast(y);
23+
dfs(graph, y, n);
24+
stack.pollLast();
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [797. All Paths From Source to Target](https://leetcode.com/problems/all-paths-from-source-to-target/)
2+
3+
4+
5+
Complexity Analysis:
6+
7+
- Time Complexity: $O(n\times 2^n)$. $n$ is the number of nodes. The worst case scenario is every nodes can connect to all the larger nodes. The number of path is $O(2^n)$ and the path is $O(n)$. Thus, the total time complexity is $O(n\times 2^n)$.
8+
- Space Complexity: $O(n)$. The stack costs $O(n)$.
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [804. Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class UniqueMorseCodeWords {
5+
public int uniqueMorseRepresentations(String[] words) {
6+
Set<String> codeSet = new HashSet<>();
7+
String[] codes = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
8+
int res = 0;
9+
for (String word : words) {
10+
String morseCode = "";
11+
for (char c : word.toCharArray()) {
12+
morseCode = morseCode.concat(codes[c - 'a']);
13+
}
14+
if (!codeSet.contains(morseCode)) {
15+
codeSet.add(morseCode);
16+
res++;
17+
}
18+
}
19+
return res;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class BinaryTreePruning {
17+
private boolean containsOne(TreeNode node) {
18+
if (node == null) return false;
19+
20+
boolean leftContainsOne = containsOne(node.left);
21+
boolean rightContainsOne = containsOne(node.right);
22+
23+
if (!leftContainsOne) node.left = null;
24+
if (!rightContainsOne) node.right = null;
25+
26+
return node.val == 1 || leftContainsOne || rightContainsOne;
27+
}
28+
29+
public TreeNode pruneTree(TreeNode root) {
30+
return containsOne(root) ? root : null;
31+
}
32+
}

0814.binary-tree-pruning/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [814. Binary Tree Pruning](https://leetcode.com/problems/binary-tree-pruning/)

0 commit comments

Comments
 (0)