Skip to content

Commit e19433f

Browse files
Accepted
1 parent 7908474 commit e19433f

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Created by gouthamvidyapradhan on 03/04/2017.
3+
* Accepted
4+
*/
5+
public class BinaryTreeMaximumPathSum
6+
{
7+
public static class TreeNode {
8+
int val;
9+
TreeNode left;
10+
TreeNode right;
11+
TreeNode(int x) { val = x; }
12+
}
13+
14+
private int max = Integer.MIN_VALUE;
15+
/**
16+
* Main method
17+
* @param args
18+
* @throws Exception
19+
*/
20+
public static void main(String[] args) throws Exception
21+
{
22+
TreeNode root = new TreeNode(5);
23+
root.left = new TreeNode(4);
24+
root.left.left = new TreeNode(3);
25+
root.left.left.left = new TreeNode(-1);
26+
root.right = new TreeNode(7);
27+
root.right.left = new TreeNode(2);
28+
root.right.left.left = new TreeNode(9);
29+
System.out.println(new BinaryTreeMaximumPathSum().maxPathSum(root));
30+
}
31+
public int maxPathSum(TreeNode root)
32+
{
33+
if(root == null) return 0;
34+
dfs(root);
35+
return max;
36+
}
37+
38+
private int dfs(TreeNode root)
39+
{
40+
if(root == null) return 0;
41+
int left = dfs(root.left);
42+
int right = dfs(root.right);
43+
max = Math.max(max, root.val);
44+
max = Math.max(max, root.val + left);
45+
max = Math.max(max, root.val + right);
46+
max = Math.max(max, root.val + left + right);
47+
int leftVal = Math.max(root.val, root.val + left);
48+
int rightVal = Math.max(root.val, root.val + right);
49+
return Math.max(leftVal, rightVal);
50+
}
51+
}

problems/src/JumpGameII.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Created by gouthamvidyapradhan on 02/04/2017.
3+
* Accepted
4+
*/
5+
public class JumpGameII
6+
{
7+
/**
8+
* Main method
9+
* @param args
10+
* @throws Exception
11+
*/
12+
public static void main(String[] args) throws Exception
13+
{
14+
15+
}
16+
17+
public int jump(int[] nums)
18+
{
19+
int step = 0;
20+
int e = 0, max = 0;
21+
for(int i = 0; i < nums.length - 1; i++)
22+
{
23+
max = Math.max(max, i + nums[i]);
24+
if(i == e)
25+
{
26+
step++;
27+
e = max;
28+
}
29+
}
30+
return step;
31+
}
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Created by gouthamvidyapradhan on 02/04/2017.
3+
* Accepted
4+
*/
5+
public class LongestIncreasingSubsequence
6+
{
7+
/**
8+
* Main method
9+
* @param args
10+
* @throws Exception
11+
*/
12+
public static void main(String[] args) throws Exception
13+
{
14+
int[] nums = {9, 8, 7, 6};
15+
System.out.println(new LongestIncreasingSubsequence().lengthOfLIS(nums));
16+
}
17+
18+
public int lengthOfLIS(int[] nums)
19+
{
20+
if(nums.length == 0) return 0;
21+
int[] A = new int[nums.length];
22+
int max = Integer.MIN_VALUE;
23+
for(int i = 0, l = nums.length; i < l; i ++)
24+
{
25+
int lis = 1;
26+
for(int j = 0; j < i; j ++)
27+
{
28+
if(nums[i] > nums[j])
29+
lis = Math.max(lis, A[j] + 1);
30+
}
31+
A[i] = lis;
32+
max = Math.max(max, A[i]);
33+
}
34+
return max;
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Created by gouthamvidyapradhan on 02/04/2017.
3+
* Accepted
4+
*/
5+
public class MaximumProductSubarray
6+
{
7+
/**
8+
* Main method
9+
* @param args
10+
* @throws Exception
11+
*/
12+
public static void main(String[] args) throws Exception
13+
{
14+
int[] A = {2,3,-2,4};
15+
System.out.println(new MaximumProductSubarray().maxProduct(A));
16+
}
17+
18+
public int maxProduct(int[] nums)
19+
{
20+
if(nums.length == 1) return nums[0];
21+
int min = nums[0];
22+
int max = nums[0];
23+
int result = max;
24+
for(int i = 1; i < nums.length; i ++)
25+
{
26+
int prevMin = min, prevMax = max;
27+
min = Math.min(nums[i], Math.min(nums[i] * prevMin, nums[i] * prevMax));
28+
max = Math.max(nums[i], Math.max(nums[i] * prevMin, nums[i] * prevMax));
29+
result = Math.max(result, max);
30+
}
31+
return result;
32+
}
33+
}

0 commit comments

Comments
 (0)