Skip to content

Commit 569ff3a

Browse files
committed
Add new solutions
1 parent 7adf928 commit 569ff3a

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.andrewbayd.BinarySearchTreeLCA;
2+
3+
public class Solution {
4+
5+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
6+
if (p.val > root.val && q.val > root.val) {
7+
return lowestCommonAncestor(root.right, p, q);
8+
} else if (p.val < root.val && q.val < root.val) {
9+
return lowestCommonAncestor(root.left, p, q);
10+
} else {
11+
return root;
12+
}
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.andrewbayd.BinarySearchTreeLCA;
2+
3+
public class TreeNode {
4+
int val;
5+
TreeNode left;
6+
TreeNode right;
7+
8+
TreeNode(int x) {
9+
val = x;
10+
}
11+
}

src/com/andrewbayd/FloodFill.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.andrewbayd;
2+
3+
import java.util.Arrays;
4+
5+
public class FloodFill {
6+
7+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
8+
int currentColor = image[sr][sc];
9+
if (currentColor == newColor) {
10+
return image;
11+
}
12+
fillPixel(image, sr, sc, currentColor, newColor);
13+
return image;
14+
}
15+
16+
private void fillPixel(int[][] image, int row, int col, int currentColor, int newColor) {
17+
if (!isValidPixel(image, row, col) || image[row][col] != currentColor || image[row][col] == newColor) {
18+
return;
19+
}
20+
image[row][col] = newColor;
21+
// Filling all 4-dimensional pixels recursively
22+
fillPixel(image, row-1, col, currentColor, newColor);
23+
fillPixel(image, row, col+1, currentColor, newColor);
24+
fillPixel(image, row+1, col, currentColor, newColor);
25+
fillPixel(image, row, col-1, currentColor, newColor);
26+
}
27+
28+
private boolean isValidPixel(int[][] image, int row, int col) {
29+
int maxRow = image.length - 1;
30+
int maxCol = image[0].length - 1;
31+
return row >= 0 && col >= 0 && row <= maxRow && col <= maxCol;
32+
}
33+
34+
public static void main(String[] args) {
35+
int[][] image1 = {
36+
{1,1,1},
37+
{1,1,0},
38+
{1,0,1}
39+
};
40+
41+
42+
int[][] image2 = {
43+
{0,0,0},
44+
{0,1,1}
45+
};
46+
47+
FloodFill filler = new FloodFill();
48+
System.out.println(Arrays.deepToString(filler.floodFill(image1, 1, 1, 2))); //-> [[2, 2, 2], [2, 2, 0], [2, 0, 1]]
49+
System.out.println(Arrays.deepToString(filler.floodFill(image2, 1, 1, 1))); //-> [[0, 0, 0], [0, 1, 1]]
50+
}
51+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.andrewbayd;
2+
3+
public class MaximumSubarray {
4+
5+
6+
public static int maxSubArray(int[] nums) {
7+
if (nums.length == 1) {
8+
return nums[0];
9+
}
10+
int sum = 0;
11+
int maxSum = nums[0];
12+
for (int num : nums) {
13+
sum += num;
14+
if (sum > maxSum) {
15+
maxSum = sum;
16+
}
17+
if (sum < 0) {
18+
sum = 0;
19+
}
20+
}
21+
return maxSum;
22+
}
23+
24+
public static void main(String[] args) {
25+
System.out.println(maxSubArray(new int[]{1,2,-1,-2,2,1,-2,1,4,-5,4})); //-> 6
26+
System.out.println(maxSubArray(new int[]{1})); //-> 1
27+
System.out.println(maxSubArray(new int[]{-2,-3,-1})); //-> -1
28+
}
29+
}

0 commit comments

Comments
 (0)