Skip to content

Commit 36eba57

Browse files
committed
add
1 parent 86fe0b8 commit 36eba57

File tree

2 files changed

+160
-5
lines changed

2 files changed

+160
-5
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package LeetcodeQuestions;
2+
3+
// https://leetcode.com/problems/house-robber/
4+
// https://leetcode.com/problems/house-robber-ii/
5+
public class HouseRobber {
6+
7+
// linear..
8+
public static int houserob(int nums[]) {
9+
10+
if (nums.length == 0) {
11+
return 0;
12+
}
13+
int[] dp = new int[nums.length + 1];
14+
dp[0] = 0;
15+
dp[1] = nums[0];
16+
for (int i = 2; i < nums.length + 1; i++) {
17+
dp[i] = Math.max(dp[i - 1], nums[i - 1] + dp[i - 2]);
18+
}
19+
return dp[nums.length];
20+
}
21+
22+
23+
// when houses are in circular fashion..
24+
25+
public static int houserobCircular(int nums[]) {
26+
if (nums.length == 0)
27+
return 0;
28+
29+
if (nums.length == 1)
30+
return nums[0];
31+
32+
int maxExcludeFirst = helper(nums, 1, nums.length);
33+
int maxExcludeLast = helper(nums, 0, nums.length - 1);
34+
35+
return Math.max(maxExcludeFirst, maxExcludeLast);
36+
37+
38+
}
39+
40+
public static int helper(int[] nums, int start, int end) {
41+
int prevMax = 0;
42+
int currentMax = 0;
43+
44+
for (int i = start; i < end; i++) {
45+
int newMax = Math.max(currentMax, nums[i] + prevMax);
46+
47+
prevMax = currentMax;
48+
currentMax = newMax;
49+
}
50+
51+
return currentMax;
52+
}
53+
54+
55+
public static void main(String[] args) {
56+
57+
}
58+
}

src/LeetcodeQuestions/uniquePath.java

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,120 @@
66
// https://leetcode.com/problems/unique-paths-ii/
77
public class uniquePath {
88

9-
109

11-
public static boolean uniquePathone() {
10+
//O(M*N)
11+
public static int uniquePathwithObstacles(int arr[][]) {
1212

1313

14+
int r = arr.length;
15+
int c = arr[0].length;
1416

17+
if (arr[0][0] == 1) {
18+
return 0;
19+
}
20+
21+
arr[0][0] = 1;
22+
23+
for (int i = 1; i < r; i++) {
24+
25+
arr[i][0] = (arr[i][0] == 0 && arr[i - 1][0] == 1) ? 1 : 0;
26+
}
27+
28+
29+
for (int i = 1; i < r; i++) {
30+
31+
arr[0][i] = (arr[0][i] == 0 && arr[0][i - 1] == 1) ? 1 : 0;
32+
}
33+
34+
35+
for (int i = 1; i < r; i++) {
36+
37+
for (int j = 1; j < c; j++) {
38+
39+
if (arr[i][j] == 0) {
40+
arr[i][j] = arr[i - 1][j] + arr[i][j - 1];
41+
} else {
42+
arr[i][j] = 0;
43+
}
44+
}
45+
}
46+
47+
48+
return arr[r - 1][c - 1];
1549

1650

1751
}
1852

1953

54+
// Unique paths without any obstacles recursive and dp solution ...
55+
public int uniquePaths(int m, int n) {
56+
57+
return solve(0, 0, m, n);
58+
}
59+
// recursive
60+
public int solve(int i, int j, int m, int n) {
61+
if (i >= m || j >= n)
62+
return 0;
63+
if (i == m - 1 && j == n - 1)
64+
return 1;
65+
return solve(i + 1, j, m, n) + solve(i, j + 1, m, n);
66+
}
67+
68+
69+
int[][] dp = new int[101][101];
70+
// dp ...
71+
public int uniquePathsDP(int m, int n) {
72+
73+
for (int i = 0; i < m; i++)
74+
for (int j = 0; j < n; j++)
75+
dp[i][j] = -1;
76+
77+
return solvedp(0, 0, m, n);
78+
}
79+
80+
public int solvedp(int i, int j, int m, int n) {
81+
if (i >= m || j >= n)
82+
return 0;
83+
if (i == m - 1 && j == n - 1)
84+
return 1;
85+
if (dp[i][j] != -1)
86+
return dp[i][j];
87+
return dp[i][j] = solve(i + 1, j, m, n) + solve(i, j + 1, m, n);
88+
}
89+
90+
91+
// convert into 1d dp method...
92+
static int numberOfPaths(int m, int n) {
93+
// Create a 1D array to store results of subproblems
94+
int[] dp = new int[n];
95+
dp[0] = 1;
96+
97+
for (int i = 0; i < m; i++) {
98+
for (int j = 1; j < n; j++) {
99+
dp[j] += dp[j - 1];
100+
}
101+
}
102+
103+
return dp[n - 1];
104+
}
105+
106+
// using combinatorics..
107+
static int numberOfPaths2(int m, int n) {
108+
// We have to calculate m+n-2 C n-1 here
109+
// which will be (m+n-2)! / (n-1)! (m-1)!
110+
int path = 1;
111+
for (int i = n; i < (m + n - 1); i++) {
112+
path *= i;
113+
path /= (i - n + 1);
114+
}
115+
return path;
116+
}
20117

21118
public static void main(String[] args) {
22119

23-
int arr[][]={{0,0,0},{0,1,0},{0,0,0}};
24-
25-
120+
int arr[][] = {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}};
121+
122+
26123

27124
System.out.println();
28125

0 commit comments

Comments
 (0)