Skip to content

Commit b6a84d9

Browse files
Accepted solutions
1 parent c882233 commit b6a84d9

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ My accepted leetcode solutions to some of the common interview problems.
156156
- [Largest Plus Sign](problems/src/dynamic_programming/LargestPlusSign.java) (Medium)
157157
- [Palindrome Pairs](problems/src/dynamic_programming/PalindromePairs.java) (Hard)
158158
- [Cherry Pickup](problems/src/dynamic_programming/CherryPickup.java) (Hard)
159+
- [Knight Probability in Chessboard](problems/src/dynamic_programming/KnightProbabilityInChessboard.java) (Medium)
159160

160161
#### [Greedy](problems/src/greedy)
161162

@@ -280,6 +281,7 @@ My accepted leetcode solutions to some of the common interview problems.
280281
- [Average of Levels in Binary Tree](problems/src/tree/AverageOfLevelsInBinaryTree.java) (Easy)
281282
- [Convert Binary Search Tree to Sorted Doubly Linked List](problems/src/tree/BSTtoDoublyLinkedList.java) (Easy)
282283
- [Same Tree](problems/src/tree/SameTree.java) (Easy)
284+
- [Binary Tree Longest Consecutive Sequence II](problems/src/tree/BinaryTreeLongestConsecutiveSequenceII.java) (Medium)
283285

284286
#### [Two Pointers](problems/src/two_pointers)
285287

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package dynamic_programming;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 13/02/2018.
7+
* On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K moves.
8+
* The rows and columns are 0 indexed, so the top-left square is (0, 0), and the bottom-right square is (N-1, N-1).
9+
10+
A chess knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal
11+
direction, then one square in an orthogonal direction.
12+
13+
14+
Each time the knight is to move, it chooses one of eight possible moves uniformly at random
15+
(even if the piece would go off the chessboard) and moves there.
16+
17+
The knight continues moving until it has made exactly K moves or has moved off the chessboard.
18+
Return the probability that the knight remains on the board after it has stopped moving.
19+
20+
Example:
21+
Input: 3, 2, 0, 0
22+
Output: 0.0625
23+
Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.
24+
From each of those positions, there are also two moves that will keep the knight on the board.
25+
The total probability the knight stays on the board is 0.0625.
26+
Note:
27+
N will be between 1 and 25.
28+
K will be between 0 and 100.
29+
The knight always initially starts on the board.
30+
31+
Solution: Solution O(N ^ 2 x K) DP top down memoization for each different states.
32+
*/
33+
public class KnightProbabilityInChessboard {
34+
35+
int[] R = {1, -1, 2, -2, -1, -2, 2, 1};
36+
int[] C = {2, 2, 1, 1, -2, -1, -1, -2};
37+
38+
double[][][] dp;
39+
/**
40+
* Main method
41+
* @param args
42+
* @throws Exception
43+
*/
44+
public static void main(String[] args) throws Exception{
45+
System.out.println(new KnightProbabilityInChessboard().knightProbability(3, 2, 0, 0));
46+
}
47+
48+
public double knightProbability(int N, int K, int r, int c) {
49+
dp = new double[N][N][K + 1];
50+
for(int i = 0; i < N; i ++){
51+
for(int j = 0; j < N; j ++){
52+
Arrays.fill(dp[i][j], -1.0D);
53+
}
54+
}
55+
for(int i = 0; i < N; i ++){
56+
for(int j = 0; j < N; j ++){
57+
dp[i][j][0] = 1.0D;
58+
}
59+
}
60+
return dp(dp, r, c, K, N);
61+
}
62+
63+
private double dp(double[][][] dp, int r, int c, int K, int N){
64+
if(r < 0 || r >= N || c < 0 || c >= N) return 0.0D;
65+
if(dp[r][c][K] != -1.0D) return dp[r][c][K];
66+
double sum = 0.0D;
67+
for(int i = 0; i < 8; i ++){
68+
int newR = r + R[i];
69+
int newC = c + C[i];
70+
if(newR >= 0 && newR < N && newC >= 0 && newC < N){
71+
sum += dp(dp, newR, newC, K - 1, N);
72+
}
73+
}
74+
dp[r][c][K] = (sum / 8.0);
75+
return dp[r][c][K];
76+
}
77+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package tree;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 11/02/2018.
5+
* Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree.
6+
7+
Especially, this path can be either increasing or decreasing. For example, [1,2,3,4] and [4,3,2,1] are both
8+
considered valid, but the path [1,2,4,3] is not valid. On the other hand, the path can be in the child-Parent-child
9+
order, where not necessarily be parent-child order.
10+
11+
Example 1:
12+
Input:
13+
1
14+
/ \
15+
2 3
16+
Output: 2
17+
Explanation: The longest consecutive path is [1, 2] or [2, 1].
18+
Example 2:
19+
Input:
20+
2
21+
/ \
22+
1 3
23+
Output: 3
24+
Explanation: The longest consecutive path is [1, 2, 3] or [3, 2, 1].
25+
Note: All the values of tree nodes are in the range of [-1e7, 1e7].
26+
*/
27+
public class BinaryTreeLongestConsecutiveSequenceII {
28+
29+
public static class TreeNode {
30+
int val;
31+
TreeNode left;
32+
TreeNode right;
33+
TreeNode(int x) { val = x; }
34+
}
35+
36+
private class Node{
37+
int i, d;
38+
int val;
39+
Node(int i, int d, int val){
40+
this.i = i;
41+
this.d = d;
42+
this.val = val;
43+
}
44+
}
45+
private int max = Integer.MIN_VALUE;
46+
public static void main(String[] args) throws Exception{
47+
TreeNode root = new TreeNode(2);
48+
root.left = new TreeNode(1);
49+
//root.left.left = new TreeNode(4);
50+
root.right = new TreeNode(3);
51+
System.out.println(new BinaryTreeLongestConsecutiveSequenceII().longestConsecutive(root));
52+
}
53+
54+
55+
public int longestConsecutive(TreeNode root) {
56+
Node n = preorder(root);
57+
if(n != null){
58+
max = Math.max(max, n.d);
59+
max = Math.max(max, n.i);
60+
if(n.i > 1 && n.d > 1){
61+
max = Math.max(max, n.d + n.i - 1);
62+
}
63+
}
64+
if(max == Integer.MIN_VALUE) return 0;
65+
return max;
66+
}
67+
68+
69+
private Node preorder(TreeNode node){
70+
if(node == null) return null;
71+
Node left = preorder(node.left);
72+
Node curr = new Node(1, 1, node.val);
73+
if(left != null){
74+
max = Math.max(max, left.d);
75+
max = Math.max(max, left.i);
76+
if(left.val == node.val + 1){
77+
curr.d = left.d + 1;
78+
curr.i = 1;
79+
} else if(left.val == node.val - 1){
80+
curr.i = left.i + 1;
81+
curr.d = 1;
82+
}
83+
}
84+
Node right = preorder(node.right);
85+
if(right != null){
86+
max = Math.max(max, right.d);
87+
max = Math.max(max, right.i);
88+
if(right.val == node.val + 1){
89+
if(right.d + 1 > curr.d){
90+
curr.d = right.d + 1;
91+
}
92+
} else if(right.val == node.val - 1){
93+
if(right.i + 1 > curr.i){
94+
curr.i = right.i + 1;
95+
}
96+
}
97+
}
98+
if(curr.i > 1 && curr.d > 1){
99+
max = Math.max(max, curr.d + curr.i - 1);
100+
}
101+
return curr;
102+
}
103+
104+
}

0 commit comments

Comments
 (0)