Skip to content

Commit e7f26c0

Browse files
add 1530
1 parent bd90747 commit e7f26c0

File tree

3 files changed

+115
-3
lines changed
  • paginated_contents/algorithms/2nd_thousand
  • src

3 files changed

+115
-3
lines changed

Diff for: paginated_contents/algorithms/2nd_thousand/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
|------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------|----------------------------------|----------------------------------------------------------------------
33
| 1996 | [The Number of Weak Characters in the Game](https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1996.java) || Medium ||
44
| 1995 | [Count Special Quadruplets](https://leetcode.com/problems/count-special-quadruplets/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1995.java) || Easy ||
5-
| 1993 | [Operations on Tree](https://leetcode.com/problems/operations-on-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1993.java) || Medium |HashTable, DFS, Design, Tree
5+
| 1993 | [Operations on Tree](https://leetcode.com/problems/operations-on-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1993.java) || Medium | HashTable, DFS, Design, Tree
66
| 1992 | [Find All Groups of Farmland](https://leetcode.com/problems/find-all-groups-of-farmland/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1992.java) || Medium ||
77
| 1991 | [Find the Middle Index in Array](https://leetcode.com/problems/find-the-middle-index-in-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1991.java) || Easy ||
88
| 1985 | [Find the Kth Largest Integer in the Array](https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1985.java) || Medium ||
@@ -198,6 +198,7 @@
198198
| 1539 | [Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1539.java) | [:tv:](https://youtu.be/p0P1JNHAB-c) | Easy | Array, HashTable |
199199
| 1535 | [Find the Winner of an Array Game](https://leetcode.com/problems/find-the-winner-of-an-array-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1535.java) | [:tv:](https://youtu.be/v6On1TQfMTU) | Medium | Array |
200200
| 1534 | [Count Good Triplets](https://leetcode.com/problems/count-good-triplets/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1534.java) | | Easy | Array |
201+
| 1530 | [Number of Good Leaf Nodes Pairs](https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1530.java) | | Medium | Tree, DFS |
201202
| 1528 | [Shuffle String](https://leetcode.com/problems/shuffle-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1528.java) | | Easy | Sort |
202203
| 1526 | [Minimum Number of Increments on Subarrays to Form a Target Array](https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1526.java) | | Hard | Segment Tree |
203204
| 1525 | [Number of Good Ways to Split a String](https://leetcode.com/problems/number-of-good-ways-to-split-a-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1525.java) | [:tv:](https://youtu.be/lRVpVUC5mQ4) | Medium | String, Bit Manipulation |
@@ -389,7 +390,7 @@
389390
| 1151 | [Minimum Swaps to Group All 1's Together](https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1151.java) || Medium | Array, Sliding Window |
390391
| 1150 | [Check If a Number Is Majority Element in a Sorted Array](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1150.java) | [:tv:](https://youtu.be/-t2cdVs9cKk) | Easy ||
391392
| 1146 | [Snapshot Array](https://leetcode.com/problems/snapshot-array/) | [Javascript](https://github.com/fishercoder1534/Leetcode/blob/master/javascript/_1146.js) [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1146.java) | | Medium ||
392-
| 1143 | [Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1143.java) | | Medium |String, DP
393+
| 1143 | [Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1143.java) | | Medium | String, DP
393394
| 1138 | [Alphabet Board Path](https://leetcode.com/problems/alphabet-board-path/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1138.java) | [:tv:](https://youtu.be/rk-aB4rEOyU) | Medium | HashTable, String |
394395
| 1137 | [N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1137.java) | | Easy ||
395396
| 1136 | [Parallel Courses](https://leetcode.com/problems/parallel-courses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1136.java) | | Medium ||
@@ -414,7 +415,7 @@
414415
| 1087 | [Brace Expansion](https://leetcode.com/problems/brace-expansion/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1087.java) | | Medium | Backtracking |
415416
| 1086 | [High Five](https://leetcode.com/problems/high-five/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1086.java) | [:tv:](https://www.youtube.com/watch?v=3iqC5J4l0Cc) | Easy ||
416417
| 1085 | [Sum of Digits in the Minimum Number](https://leetcode.com/problems/sum-of-digits-in-the-minimum-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1085.java) | [:tv:](https://www.youtube.com/watch?v=GKYmPuHZpQg) | Easy ||
417-
| 1080 | [Insufficient Nodes in Root to Leaf Paths](https://leetcode.com/problems/insufficient-nodes-in-root-to-leaf-paths/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1080.java) | | Medium |Tree, DFS
418+
| 1080 | [Insufficient Nodes in Root to Leaf Paths](https://leetcode.com/problems/insufficient-nodes-in-root-to-leaf-paths/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1080.java) | | Medium | Tree, DFS
418419
| 1079 | [Letter Tile Possibilities](https://leetcode.com/problems/letter-tile-possibilities/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1079.java) | | Medium ||
419420
| 1078 | [Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1078.java) | | Easy ||
420421
| 1071 | [Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1071.java) | | Easy ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.fishercoder.solutions.secondthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Queue;
12+
import java.util.Set;
13+
14+
public class _1530 {
15+
public static class Solution1 {
16+
public int countPairs(TreeNode root, int distance) {
17+
Map<TreeNode, TreeNode> childToParentMap = new HashMap<>();
18+
List<TreeNode> leafNodes = findAllLeaves(root, childToParentMap, new ArrayList<>());
19+
int pairs = 0;
20+
for (TreeNode leaf : leafNodes) {
21+
pairs += bfsToPossibleLeaves(leaf, distance, childToParentMap);
22+
}
23+
return pairs / 2;
24+
}
25+
26+
private int bfsToPossibleLeaves(TreeNode leaf, int distance, Map<TreeNode, TreeNode> childToParentMap) {
27+
Queue<TreeNode> q = new LinkedList<>();
28+
q.offer(leaf);
29+
Set<TreeNode> visited = new HashSet<>();
30+
visited.add(leaf);
31+
int count = 0;
32+
while (!q.isEmpty() && distance >= 0) {
33+
int size = q.size();
34+
for (int i = 0; i < size; i++) {
35+
TreeNode curr = q.poll();
36+
if (leaf != curr && curr.left == null && curr.right == null) {
37+
count++;
38+
}
39+
if (curr.left != null && visited.add(curr.left)) {
40+
q.offer(curr.left);
41+
}
42+
if (curr.right != null && visited.add(curr.right)) {
43+
q.offer(curr.right);
44+
}
45+
if (childToParentMap.containsKey(curr) && visited.add(childToParentMap.get(curr))) {
46+
q.offer(childToParentMap.get(curr));
47+
}
48+
}
49+
distance--;
50+
}
51+
return count;
52+
}
53+
54+
private List<TreeNode> findAllLeaves(TreeNode node, Map<TreeNode, TreeNode> childToParentMap, List<TreeNode> leafNodes) {
55+
if (node == null) {
56+
return leafNodes;
57+
}
58+
if (node.left == null && node.right == null) {
59+
leafNodes.add(node);
60+
}
61+
if (node.left != null) {
62+
childToParentMap.put(node.left, node);
63+
findAllLeaves(node.left, childToParentMap, leafNodes);
64+
}
65+
if (node.right != null) {
66+
childToParentMap.put(node.right, node);
67+
findAllLeaves(node.right, childToParentMap, leafNodes);
68+
}
69+
return leafNodes;
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)