Skip to content

Commit c76355a

Browse files
authored
Added tasks 687, 688, 701, 703.
1 parent f05d46b commit c76355a

File tree

12 files changed

+356
-0
lines changed

12 files changed

+356
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g0601_0700.s0687_longest_univalue_path;
2+
3+
// #Medium #Depth_First_Search #Tree #Binary_Tree
4+
5+
import com_github_leetcode.TreeNode;
6+
7+
public class Solution {
8+
public int longestUnivaluePath(TreeNode root) {
9+
if (root == null) {
10+
return 0;
11+
}
12+
int[] res = new int[1];
13+
preorderLongestSinglePathLen(root, res);
14+
return res[0];
15+
}
16+
17+
private int preorderLongestSinglePathLen(TreeNode root, int[] res) {
18+
if (root == null) {
19+
return -1;
20+
}
21+
int left = preorderLongestSinglePathLen(root.left, res);
22+
int right = preorderLongestSinglePathLen(root.right, res);
23+
24+
if (root.left == null || root.val == root.left.val) {
25+
left = left + 1;
26+
} else {
27+
left = 0;
28+
}
29+
30+
if (root.right == null || root.val == root.right.val) {
31+
right = right + 1;
32+
} else {
33+
right = 0;
34+
}
35+
int longestPathLenPassingThroughRoot = left + right;
36+
res[0] = Math.max(res[0], longestPathLenPassingThroughRoot);
37+
return Math.max(left, right);
38+
}
39+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
687\. Longest Univalue Path
2+
3+
Medium
4+
5+
Given the `root` of a binary tree, return _the length of the longest path, where each node in the path has the same value_. This path may or may not pass through the root.
6+
7+
**The length of the path** between two nodes is represented by the number of edges between them.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/13/ex1.jpg)
12+
13+
**Input:** root = [5,4,5,1,1,5]
14+
15+
**Output:** 2
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2020/10/13/ex2.jpg)
20+
21+
**Input:** root = [1,4,5,4,4,5]
22+
23+
**Output:** 2
24+
25+
**Constraints:**
26+
27+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
28+
* `-1000 <= Node.val <= 1000`
29+
* The depth of the tree will not exceed `1000`.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0601_0700.s0688_knight_probability_in_chessboard;
2+
3+
// #Medium #Dynamic_Programming
4+
5+
public class Solution {
6+
private final int[][] directions =
7+
new int[][] {{-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, -1}, {2, 1}, {1, -2}, {-1, -2}};
8+
private double[][][] probabilityGiven;
9+
10+
public double knightProbability(int n, int k, int row, int column) {
11+
probabilityGiven = new double[n][n][k + 1];
12+
return probability(row, column, k, n);
13+
}
14+
15+
private double probability(int row, int column, int k, int n) {
16+
if (k == 0) {
17+
return 1.0;
18+
} else if (probabilityGiven[row][column][k] != 0) {
19+
return probabilityGiven[row][column][k];
20+
} else {
21+
double p = 0d;
22+
for (int[] dir : directions) {
23+
if (isValid(row + dir[0], column + dir[1], n)) {
24+
p += probability(row + dir[0], column + dir[1], k - 1, n);
25+
}
26+
}
27+
probabilityGiven[row][column][k] = p / 8.0;
28+
return probabilityGiven[row][column][k];
29+
}
30+
}
31+
32+
private boolean isValid(int row, int column, int n) {
33+
return (row >= 0 && row < n && column >= 0 && column < n);
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
688\. Knight Probability in Chessboard
2+
3+
Medium
4+
5+
On an `n x n` chessboard, a knight starts at the cell `(row, column)` and attempts to make exactly `k` moves. The rows and columns are **0-indexed**, so the top-left cell is `(0, 0)`, and the bottom-right cell is `(n - 1, n - 1)`.
6+
7+
A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.
8+
9+
![](https://assets.leetcode.com/uploads/2018/10/12/knight.png)
10+
11+
Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.
12+
13+
The knight continues moving until it has made exactly `k` moves or has moved off the chessboard.
14+
15+
Return _the probability that the knight remains on the board after it has stopped moving_.
16+
17+
**Example 1:**
18+
19+
**Input:** n = 3, k = 2, row = 0, column = 0
20+
21+
**Output:** 0.06250
22+
23+
**Explanation:** There are two moves (to (1,2), (2,1)) that will keep the knight on the board. From each of those positions, there are also two moves that will keep the knight on the board. The total probability the knight stays on the board is 0.0625.
24+
25+
**Example 2:**
26+
27+
**Input:** n = 1, k = 0, row = 0, column = 0
28+
29+
**Output:** 1.00000
30+
31+
**Constraints:**
32+
33+
* `1 <= n <= 25`
34+
* `0 <= k <= 100`
35+
* `0 <= row, column <= n`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0701_0800.s0701_insert_into_a_binary_search_tree;
2+
3+
// #Medium #Tree #Binary_Tree #Binary_Search_Tree
4+
5+
import com_github_leetcode.TreeNode;
6+
7+
public class Solution {
8+
public TreeNode insertIntoBST(TreeNode root, int val) {
9+
if (root == null) {
10+
return new TreeNode(val);
11+
}
12+
if (root.val < val) {
13+
root.right = insertIntoBST(root.right, val);
14+
} else {
15+
root.left = insertIntoBST(root.left, val);
16+
}
17+
return root;
18+
}
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
701\. Insert into a Binary Search Tree
2+
3+
Medium
4+
5+
You are given the `root` node of a binary search tree (BST) and a `value` to insert into the tree. Return _the root node of the BST after the insertion_. It is **guaranteed** that the new value does not exist in the original BST.
6+
7+
**Notice** that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return **any of them**.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/05/insertbst.jpg)
12+
13+
**Input:** root = [4,2,7,1,3], val = 5
14+
15+
**Output:** [4,2,7,1,3,5]
16+
17+
**Explanation:** Another accepted tree is: ![](https://assets.leetcode.com/uploads/2020/10/05/bst.jpg)
18+
19+
**Example 2:**
20+
21+
**Input:** root = [40,20,60,10,30,50,70], val = 25
22+
23+
**Output:** [40,20,60,10,30,50,70,null,null,25]
24+
25+
**Example 3:**
26+
27+
**Input:** root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
28+
29+
**Output:** [4,2,7,1,3,5]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.
34+
* <code>-10<sup>8</sup> <= Node.val <= 10<sup>8</sup></code>
35+
* All the values `Node.val` are **unique**.
36+
* <code>-10<sup>8</sup> <= val <= 10<sup>8</sup></code>
37+
* It's **guaranteed** that `val` does not exist in the original BST.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0701_0800.s0703_kth_largest_element_in_a_stream;
2+
3+
// #Easy #Tree #Binary_Tree #Design #Heap_Priority_Queue #Binary_Search_Tree #Data_Stream
4+
5+
import java.util.PriorityQueue;
6+
7+
public class KthLargest {
8+
9+
private final int maxSize;
10+
private final PriorityQueue<Integer> heap;
11+
12+
public KthLargest(int k, int[] nums) {
13+
this.maxSize = k;
14+
this.heap = new PriorityQueue<>();
15+
for (int num : nums) {
16+
this.add(num);
17+
}
18+
}
19+
20+
public int add(int val) {
21+
if (heap.size() < maxSize) {
22+
heap.add(val);
23+
} else if (heap.peek() < val) {
24+
heap.add(val);
25+
heap.poll();
26+
}
27+
return heap.peek();
28+
}
29+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
703\. Kth Largest Element in a Stream
2+
3+
Easy
4+
5+
Design a class to find the <code>k<sup>th</sup></code> largest element in a stream. Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.
6+
7+
Implement `KthLargest` class:
8+
9+
* `KthLargest(int k, int[] nums)` Initializes the object with the integer `k` and the stream of integers `nums`.
10+
* `int add(int val)` Appends the integer `val` to the stream and returns the element representing the <code>k<sup>th</sup></code> largest element in the stream.
11+
12+
**Example 1:**
13+
14+
**Input**
15+
16+
["KthLargest", "add", "add", "add", "add", "add"]
17+
18+
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
19+
20+
**Output:** [null, 4, 5, 5, 8, 8]
21+
22+
**Explanation:**
23+
24+
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
25+
kthLargest.add(3); // return 4
26+
kthLargest.add(5); // return 5
27+
kthLargest.add(10); // return 5
28+
kthLargest.add(9); // return 8
29+
kthLargest.add(4); // return 8
30+
31+
**Constraints:**
32+
33+
* <code>1 <= k <= 10<sup>4</sup></code>
34+
* <code>0 <= nums.length <= 10<sup>4</sup></code>
35+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
36+
* <code>-10<sup>4</sup> <= val <= 10<sup>4</sup></code>
37+
* At most <code>10<sup>4</sup></code> calls will be made to `add`.
38+
* It is guaranteed that there will be at least `k` elements in the array when you search for the <code>k<sup>th</sup></code> element.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0601_0700.s0687_longest_univalue_path;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.TreeNode;
7+
import java.util.Arrays;
8+
import org.junit.jupiter.api.Test;
9+
10+
class SolutionTest {
11+
@Test
12+
void longestUnivaluePath() {
13+
TreeNode treeNode = TreeNode.create(Arrays.asList(5, 4, 5, 1, 1, 5));
14+
assertThat(new Solution().longestUnivaluePath(treeNode), equalTo(2));
15+
}
16+
17+
@Test
18+
void longestUnivaluePath2() {
19+
TreeNode treeNode = TreeNode.create(Arrays.asList(1, 4, 5, 4, 4, 5));
20+
assertThat(new Solution().longestUnivaluePath(treeNode), equalTo(2));
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0601_0700.s0688_knight_probability_in_chessboard;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void knightProbability() {
11+
assertThat(new Solution().knightProbability(3, 2, 0, 0), equalTo(0.06250));
12+
}
13+
14+
@Test
15+
void knightProbability2() {
16+
assertThat(new Solution().knightProbability(1, 0, 0, 0), equalTo(1.00));
17+
}
18+
}

0 commit comments

Comments
 (0)