Skip to content

Commit 4e2d8f3

Browse files
committed
Added 4 solutions
1 parent ec95347 commit 4e2d8f3

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

Easy/Diameter of Binary Tree.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public int diameterOfBinaryTree(TreeNode root) {
12+
if (root == null) return 0;
13+
14+
int rootDiam = depth(root.left) + depth(root.right);
15+
int leftDia = diameterOfBinaryTree(root.left);
16+
int rightDia = diameterOfBinaryTree(root.right);
17+
18+
return Math.max(rootDiam, Math.max(leftDia, rightDia));
19+
}
20+
21+
public int depth(TreeNode root) {
22+
if (root == null) {
23+
return 0;
24+
}
25+
26+
return 1 + Math.max(depth(root.left), depth(root.right));
27+
}
28+
}

Easy/Find Pivot Index.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int pivotIndex(int[] nums) {
3+
int left = 0;
4+
int sum = 0;
5+
6+
for (int i=0;i<nums.length;i++) {
7+
sum += nums[i];
8+
}
9+
10+
for (int i=0;i<nums.length;i++) {
11+
if (i != 0) {
12+
left += nums[i-1];
13+
}
14+
if (sum - left - nums[i] == left) {
15+
return i;
16+
}
17+
}
18+
19+
return -1;
20+
}
21+
}

Easy/Image Smoother.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
public int[][] imageSmoother(int[][] M) {
3+
4+
int[][] res = new int[M.length][M[0].length];
5+
for (int i=0;i<M.length;i++) {
6+
for (int j=0;j<M[0].length;j++) {
7+
int count = 1;
8+
int sum = M[i][j];
9+
10+
if(j-1 >= 0) {
11+
count++;
12+
sum += M[i][j-1];
13+
}
14+
if(j+1 < M[0].length) {
15+
count++;
16+
sum += M[i][j+1];
17+
}
18+
if(i-1 >= 0) {
19+
count++;
20+
sum += M[i-1][j];
21+
}
22+
if(i+1 < M.length) {
23+
count++;
24+
sum += M[i+1][j];
25+
}
26+
if(i+1 < M.length && j+1 < M[0].length) {
27+
count++;
28+
sum += M[i+1][j+1];
29+
}
30+
if(i+1 < M.length && j-1 >= 0) {
31+
count++;
32+
sum += M[i+1][j-1];
33+
}
34+
if(i-1 >= 0 && j-1 >= 0) {
35+
count++;
36+
sum += M[i-1][j-1];
37+
}
38+
if(i-1 >= 0 && j+1 < M[0].length) {
39+
count++;
40+
sum += M[i-1][j+1];
41+
}
42+
43+
res[i][j] = (int)Math.floor(sum/count);
44+
}
45+
}
46+
47+
return res;
48+
}
49+
}

Easy/Number of Boomerangs.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int numberOfBoomerangs(int[][] points) {
3+
4+
int res = 0;
5+
Map<Integer, Integer> map = new HashMap<>();
6+
7+
for (int i=0;i<points.length;i++) {
8+
for (int j=0;j<points.length;j++) {
9+
if (i == j) {
10+
continue;
11+
}
12+
int d = getDistance(points[i], points[j]);
13+
14+
map.put(d, map.getOrDefault(d,0) + 1);
15+
}
16+
17+
for (int val: map.values()) {
18+
res += val*(val-1);
19+
}
20+
map.clear();
21+
}
22+
23+
return res;
24+
}
25+
26+
public int getDistance(int[] a, int[] b) {
27+
int dx = a[0] - b[0];
28+
int dy = a[1] - b[1];
29+
return dx*dx + dy*dy;
30+
}
31+
}

0 commit comments

Comments
 (0)