Skip to content

Commit bad1653

Browse files
committed
Added more solutions to easy category
1 parent c5c49e7 commit bad1653

File tree

8 files changed

+210
-0
lines changed

8 files changed

+210
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.dp;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class BestTimeToBuyAndSellStock {
11+
public int maxProfit(int[] prices) {
12+
int max_so_far = 0, curr_max = 0;
13+
for (int i = 1; i < prices.length; i++) {
14+
curr_max += prices[i] - prices[i - 1];
15+
curr_max = Math.max(curr_max, 0);
16+
max_so_far = Math.max(max_so_far, curr_max);
17+
}
18+
return max_so_far;
19+
}
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.dp;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class ClimbingStairs {
11+
public int climbStairs(int n) {
12+
int dp[] = new int[n + 1];
13+
return climbStairsUtil(n, dp);
14+
15+
}
16+
17+
public int climbStairsUtil(int n, int[] dp) {
18+
if (n < 0)
19+
return 0;
20+
if (n == 0)
21+
return 1;
22+
if (dp[n] != 0) {
23+
return dp[n];
24+
} else {
25+
return dp[n] = climbStairsUtil(n - 1, dp) + climbStairsUtil(n - 2, dp);
26+
}
27+
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.dp;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class HouseRobber {
11+
public int rob(int[] nums) {
12+
int n = nums.length;
13+
if (n == 0)
14+
return 0;
15+
if (n == 1)
16+
return nums[0];
17+
18+
int dp[] = new int[n + 1];
19+
dp[0] = nums[0];
20+
dp[1] = Math.max(dp[1], dp[0]);
21+
int max = dp[1];
22+
for (int i = 2; i < n; i++) {
23+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
24+
max = Math.max(max, dp[i]);
25+
}
26+
return max;
27+
}
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.dp;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class MaximumProductSubarray {
11+
public int maxProduct(int[] nums) {
12+
int max_so_far = 1, max_ending_here = 1;
13+
for (int i = 0; i < nums.length; i++) {
14+
max_ending_here *= nums[i];
15+
max_ending_here = Math.max(max_ending_here, nums[i]);
16+
max_so_far = Math.max(max_so_far, max_ending_here);
17+
}
18+
return max_so_far;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.dp;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class MaximumSubarray {
11+
public int maxSubArray(int[] nums) {
12+
int max_so_far=Integer.MIN_VALUE,max_ending_here=0;
13+
for(int i=0;i<nums.length;i++){
14+
max_ending_here+=nums[i];
15+
max_ending_here=Math.max(max_ending_here,nums[i]);
16+
max_so_far=Math.max(max_so_far,max_ending_here);
17+
}
18+
return max_so_far;
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.dp;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class MinCostClimbingStairs {
11+
public int minCostClimbingStairs(int[] cost) {
12+
int n=cost.length;
13+
int[] dp=new int[cost.length+1];
14+
dp[0]=cost[0];
15+
dp[1]=cost[1];
16+
for(int i=2;i<cost.length;i++) {
17+
dp[i]=cost[i]+Math.min(dp[i-1], dp[i-2]);
18+
}
19+
return Math.min(dp[n-2], dp[n-1]);
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.dp;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class RangeSumQueryImmutable {
11+
12+
int[] nums;
13+
14+
public RangeSumQueryImmutable(int[] nums) {
15+
this.nums = nums;
16+
for (int k = 1; k < nums.length; k++) {
17+
nums[k] += nums[k - 1];
18+
}
19+
20+
}
21+
22+
public int sumRange(int i, int j) {
23+
24+
if (i > 0)
25+
return (nums[j] - nums[i - 1]);
26+
return (nums[j]);
27+
}
28+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.trees;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class ConvertSortedArrayToBinarySearchTree {
11+
static class TreeNode {
12+
int val;
13+
TreeNode left;
14+
TreeNode right;
15+
16+
TreeNode(int x) {
17+
val = x;
18+
}
19+
}
20+
21+
public TreeNode sortedArrayToBST(int[] nums) {
22+
TreeNode root = null;
23+
root = sortedArrayToBSTUtil(nums, 0, nums.length - 1);
24+
return root;
25+
}
26+
27+
/**
28+
*
29+
* @param nums
30+
* @param start
31+
* @param end
32+
* @return
33+
*/
34+
private TreeNode sortedArrayToBSTUtil(int[] nums, int start, int end) {
35+
if (start > end) {
36+
return null;
37+
}
38+
int mid = (start + end) / 2;
39+
TreeNode node = new TreeNode(nums[mid]);
40+
node.left = sortedArrayToBSTUtil(nums, start, mid - 1);
41+
node.right = sortedArrayToBSTUtil(nums, mid + 1, end);
42+
return node;
43+
}
44+
}

0 commit comments

Comments
 (0)