Skip to content

Commit ea5e539

Browse files
Accepted solutions
1 parent 4ab690a commit ea5e539

3 files changed

Lines changed: 154 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ My accepted leetcode solutions to some of the common interview problems.
3636
- [Next Greater Element I](problems/src/array/NextGreaterElementI.java) (Easy)
3737
- [Largest Number At Least Twice of Others](problems/src/array/LargestNumberAtLeastTwice.java) (Easy)
3838
- [Minimum Moves to Equal Array Elements II](problems/src/array/MinimumMovesToEqualArray.java) (Median)
39+
- [Image Smoother](problems/src/array/ImageSmoother.java) (Easy)
3940

4041
#### [Backtracking](problems/src/backtracking)
4142

@@ -290,6 +291,7 @@ My accepted leetcode solutions to some of the common interview problems.
290291
- [Same Tree](problems/src/tree/SameTree.java) (Easy)
291292
- [Binary Tree Longest Consecutive Sequence II](problems/src/tree/BinaryTreeLongestConsecutiveSequenceII.java) (Medium)
292293
- [Minimum Absolute Difference in BST](problems/src/tree/MinimumAbsoluteDifferenceInBST.java) (Medium)
294+
- [Equal Tree Partition](problems/src/tree/EqualTreePartition.java) (Medium)
293295

294296
#### [Two Pointers](problems/src/two_pointers)
295297

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package array;
2+
/**
3+
* Created by gouthamvidyapradhan on 17/02/2018.
4+
* * Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the
5+
* gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself.
6+
* If a cell has less than 8 surrounding cells, then use as many as you can.
7+
8+
Example 1:
9+
Input:
10+
[[1,1,1],
11+
[1,0,1],
12+
[1,1,1]]
13+
Output:
14+
[[0, 0, 0],
15+
[0, 0, 0],
16+
[0, 0, 0]]
17+
Explanation:
18+
For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
19+
For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
20+
For the point (1,1): floor(8/9) = floor(0.88888889) = 0
21+
Note:
22+
The value in the given matrix is in the range of [0, 255].
23+
The length and width of the given matrix are in the range of [1, 150].
24+
*/
25+
26+
public class ImageSmoother {
27+
28+
int[] R = {1, -1, 0, 0, 1, -1, 1, -1};
29+
int[] C = {0, 0, -1, 1, 1, 1, -1, -1};
30+
public static void main(String[] args) throws Exception{
31+
32+
}
33+
34+
public int[][] imageSmoother(int[][] M) {
35+
int[][] result = new int[M.length][M[0].length];
36+
for(int i = 0; i < M.length; i ++){
37+
for(int j = 0; j < M[0].length; j ++){
38+
int numCount = 0;
39+
int totalCount = 1;
40+
for(int k = 0; k < 8; k++){
41+
int newR = i + R[k];
42+
int newC = j + C[k];
43+
if(newR >= 0 && newC >= 0 && newR < M.length && newC < M[0].length){
44+
if(M[newR][newC] > 0){
45+
numCount += M[newR][newC];
46+
}
47+
totalCount++;
48+
}
49+
}
50+
if(M[i][j] == 1) numCount++;
51+
result[i][j] = numCount / totalCount;
52+
}
53+
}
54+
return result;
55+
}
56+
57+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package tree;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 17/02/2018.
5+
* Given a binary tree with n nodes, your task is to check if it's possible to partition the tree to two trees which
6+
* have the equal sum of values after removing exactly one edge on the original tree.
7+
8+
Example 1:
9+
Input:
10+
5
11+
/ \
12+
10 10
13+
/ \
14+
2 3
15+
16+
Output: True
17+
Explanation:
18+
5
19+
/
20+
10
21+
22+
Sum: 15
23+
24+
10
25+
/ \
26+
2 3
27+
28+
Sum: 15
29+
Example 2:
30+
Input:
31+
1
32+
/ \
33+
2 10
34+
/ \
35+
2 20
36+
37+
Output: False
38+
Explanation: You can't split the tree into two trees with equal sum after removing exactly one edge on the tree.
39+
Note:
40+
The range of tree node value is in the range of [-100000, 100000].
41+
1 <= n <= 10000
42+
*/
43+
44+
public class EqualTreePartition {
45+
public class TreeNode {
46+
int val;
47+
TreeNode left;
48+
TreeNode right;
49+
TreeNode(int x) { val = x; }
50+
}
51+
private long sum;
52+
private boolean possible = false;
53+
54+
public static void main(String[] args) throws Exception{
55+
56+
}
57+
58+
public boolean checkEqualTree(TreeNode root) {
59+
sum = 0L;
60+
getSum(root);
61+
getDiff(root);
62+
return possible;
63+
}
64+
65+
private void getSum(TreeNode node){
66+
if(node != null){
67+
sum += node.val;
68+
getSum(node.left);
69+
getSum(node.right);
70+
}
71+
}
72+
73+
private Long getDiff(TreeNode node){
74+
if(node == null) return null;
75+
Long left = getDiff(node.left);
76+
Long right = getDiff(node.right);
77+
if(left != null){
78+
if((sum - left) == left){
79+
possible = true;
80+
}
81+
}if(right != null){
82+
if((sum - right) == right){
83+
possible = true;
84+
}
85+
}
86+
Long curr = (long)node.val;
87+
if(left != null){
88+
curr += left;
89+
} if(right != null){
90+
curr += right;
91+
}
92+
return curr;
93+
}
94+
95+
}

0 commit comments

Comments
 (0)