Skip to content

Commit e814ec1

Browse files
Merge pull request #598 from unfixedbug/main
DP and Graph easy implementation JAVA
2 parents c44c3c2 + 5f4015d commit e814ec1

5 files changed

+331
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*https://leetcode.com/problems/minimum-falling-path-sum/submissions/
2+
*
3+
*/
4+
// minimum hai bhai
5+
private class recursion {
6+
7+
public int minFallingPathSum(int[][] matrix) {
8+
int n = matrix.length;
9+
int m = matrix[0].length;
10+
11+
int ans = Integer.MAX_VALUE;
12+
13+
for (int j = 0; j < m; j++) {
14+
ans = Math.min(ans, recur(n - 1, j, matrix));
15+
}
16+
return ans;
17+
}
18+
19+
private int recur(int i, int j, int[][] matrix) {
20+
// out of bound errors
21+
if (j < 0 || j >= matrix[0].length) return Integer.MAX_VALUE;
22+
// if reawched the first row, then only
23+
if (i == 0) return matrix[0][j];
24+
25+
int up = recur(i - 1, j, matrix);
26+
int leftdiagonal = recur(i - 1, j - 1, matrix);
27+
int rightdiagonal = recur(i - 1, j + 1, matrix);
28+
return matrix[i][j] + Math.min(up, Math.min(leftdiagonal, rightdiagonal));
29+
}
30+
}
31+
32+
private class memoisation {
33+
int dp[][];
34+
35+
public int minFallingPathSum(int[][] matrix) {
36+
int n = matrix.length;
37+
int m = matrix[0].length;
38+
dp = new int[n][m];
39+
int ans = Integer.MAX_VALUE;
40+
// fill dp with min value
41+
for (int a[] : dp) {
42+
Arrays.fill(a, Integer.MAX_VALUE);
43+
}
44+
45+
for (int j = 0; j < m; j++) {
46+
ans = Math.min(ans, recur(n - 1, j, matrix));
47+
}
48+
return ans;
49+
}
50+
51+
private int recur(int i, int j, int[][] matrix) {
52+
// out of bound errors
53+
if (j < 0 || j >= matrix[0].length) return Integer.MAX_VALUE;
54+
// if reawched the first row, then only
55+
if (i == 0) return matrix[0][j];
56+
if (dp[i][j] != Integer.MAX_VALUE) return dp[i][j];
57+
58+
int up = recur(i - 1, j, matrix);
59+
int leftdiagonal = recur(i - 1, j - 1, matrix);
60+
int rightdiagonal = recur(i - 1, j + 1, matrix);
61+
return (
62+
dp[i][j] =
63+
matrix[i][j] + Math.min(up, Math.min(leftdiagonal, rightdiagonal))
64+
);
65+
}
66+
}
67+
68+
private class tabulation {
69+
int dp[][];
70+
71+
public int minFallingPathSum(int[][] matrix) {
72+
int n = matrix.length;
73+
int m = matrix[0].length;
74+
dp = new int[n][m];
75+
int ans = Integer.MAX_VALUE;
76+
// fill dp with min value
77+
for (int a[] : dp) {
78+
Arrays.fill(a, Integer.MAX_VALUE);
79+
}
80+
81+
// filling the first array
82+
for (int i = 0; i < m; i++) {
83+
dp[0][i] = matrix[0][i];
84+
}
85+
86+
for (int i = 1; i < n; i++) {
87+
for (int col = 0; col < m; col++) {
88+
// check out of boound and go
89+
dp[i][col] = matrix[i][col];
90+
91+
int up = dp[i - 1][col];
92+
int leftdiag = dp[i - 1][Math.max(0, col - 1)];
93+
int rightdiag = dp[i - 1][Math.min(m - 1, col + 1)];
94+
95+
// minimum from the rest of moves
96+
dp[i][col] += Math.min(up, Math.min(leftdiag, rightdiag));
97+
}
98+
}
99+
100+
// return the minimum value from the end
101+
for (int j = 0; j < m; j++) {
102+
ans = Math.min(ans, dp[n - 1][j]);
103+
}
104+
return ans;
105+
}
106+
107+
private int recur(int i, int j, int[][] matrix) {
108+
// out of bound errors
109+
if (j < 0 || j >= matrix[0].length) return Integer.MAX_VALUE;
110+
// if reawched the first row, then only
111+
if (i == 0) return matrix[0][j];
112+
if (dp[i][j] != Integer.MAX_VALUE) return dp[i][j];
113+
114+
int up = recur(i - 1, j, matrix);
115+
int leftdiagonal = recur(i - 1, j - 1, matrix);
116+
int rightdiagonal = recur(i - 1, j + 1, matrix);
117+
return (
118+
dp[i][j] =
119+
matrix[i][j] + Math.min(up, Math.min(leftdiagonal, rightdiagonal))
120+
);
121+
}
122+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*https://leetcode.com/problems/minimum-path-sum/
2+
* dp easy, striver op
3+
*
4+
*/
5+
6+
private class Recursion {
7+
8+
public int minPathSum(int[][] grid) {
9+
int n = grid.length;
10+
int m = grid[0].length;
11+
return recursion(n - 1, m - 1, grid);
12+
}
13+
14+
private int recursion(int i, int j, int[][] grid) {
15+
if (i == 0 && j == 0) return grid[0][0];
16+
if (i < 0 || j < 0) return Integer.MAX_VALUE;
17+
int up = recursion(i - 1, j, grid); // travelling from upside
18+
int left = recursion(i, j - 1, grid); // travelling from righside
19+
return grid[i][j] + Math.min(up, left);
20+
}
21+
}
22+
23+
private class memoization {
24+
int dp[][];
25+
26+
public int minPathSum(int[][] grid) {
27+
int n = grid.length;
28+
int m = grid[0].length;
29+
dp = new int[n][m];
30+
for (int a[] : dp) {
31+
Arrays.fill(a, -1);
32+
}
33+
return recursion(n - 1, m - 1, grid);
34+
}
35+
36+
private int recursion(int i, int j, int[][] grid) {
37+
if (i == 0 && j == 0) return grid[0][0];
38+
if (i < 0 || j < 0) return Integer.MAX_VALUE;
39+
if (dp[i][j] != -1) return dp[i][j];
40+
int up = recursion(i - 1, j, grid); // travelling from upside
41+
int left = recursion(i, j - 1, grid); // travelling from righside
42+
return dp[i][j] = grid[i][j] + Math.min(up, left);
43+
}
44+
}
45+
46+
private class tabulation {
47+
int dp[][];
48+
49+
public int minPathSum(int[][] grid) {
50+
int n = grid.length;
51+
int m = grid[0].length;
52+
dp = new int[n][m];
53+
for (int a[] : dp) {
54+
Arrays.fill(a, -1);
55+
}
56+
for (int i = 0; i < n; i++) {
57+
for (int j = 0; j < m; j++) {
58+
if (i == 0 && j == 0) dp[i][j] = grid[i][j];
59+
if (dp[i][j] != -1) continue; else if (i == 0 && j != 0) dp[i][j] =
60+
grid[i][j] + dp[i][j - 1]; else if (i != 0 && j == 0) dp[i][j] =
61+
grid[i][j] + dp[i - 1][j]; else {
62+
dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]);
63+
}
64+
}
65+
}
66+
67+
return dp[n - 1][m - 1];
68+
}
69+
}

Dynamic Programming/triangle.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*https://leetcode.com/problems/triangle/
2+
* triangle
3+
* strivers dp series
4+
*/
5+
private class recursion {
6+
7+
public int minimumTotal(List<List<Integer>> tri) {
8+
return recur(tri, 0, 0);
9+
}
10+
11+
private static int recur(List<List<Integer>> tri, int i, int j) {
12+
if (i == tri.size() - 1) {
13+
return tri.get(i).get(j);
14+
}
15+
int ans = tri.get(i).get(j);
16+
int down = recur(tri, i + 1, j); //tri.get(i+1).get(j);
17+
int diagonal = recur(tri, i + 1, j + 1); //tri.get(i+1).get(j+1);
18+
return ans + Math.min(down, diagonal);
19+
}
20+
}
21+
22+
private class memoization {
23+
int dp[][];
24+
25+
public int minimumTotal(List<List<Integer>> tri) {
26+
dp = new int[tri.size()][tri.size()];
27+
for (int a[] : dp) {
28+
Arrays.fill(a, -1);
29+
}
30+
return recur(tri, 0, 0);
31+
}
32+
33+
private int recur(List<List<Integer>> tri, int i, int j) {
34+
if (i == tri.size() - 1) {
35+
return tri.get(i).get(j);
36+
}
37+
if (dp[i][j] != -1) return dp[i][j];
38+
int ans = tri.get(i).get(j);
39+
int down = recur(tri, i + 1, j); //tri.get(i+1).get(j);
40+
int diagonal = recur(tri, i + 1, j + 1); //tri.get(i+1).get(j+1);
41+
return dp[i][j] = ans + Math.min(down, diagonal);
42+
}
43+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.ArrayList;
2+
3+
4+
// check whether the graph is DAG, directed acyclic(without cycles) graph
5+
public class cycleDetectionInDirectedGraph {
6+
private boolean checkCycle(int node, ArrayList<ArrayList<Integer>> adj, int[] visited, int dfsVisited[]) {
7+
visited[node] = 1;
8+
dfsVisited[node] = 1;
9+
for (Integer child : adj.get(node)) {
10+
if (visited[child] == 0) {
11+
if (checkCycle(child, adj, visited, dfsVisited))
12+
return true;
13+
} else if (dfsVisited[child] == 1)
14+
return true;
15+
}
16+
return false;
17+
}
18+
19+
public boolean isCyclic(int N, ArrayList<ArrayList<Integer>> adjlist, int n) {
20+
int visited[] = new int[N];
21+
int dfsVisited[] = new int[N];
22+
for (int i = 0; i < n; i++) {
23+
if (visited[i] == 0) {
24+
if (checkCycle(i, adjlist, visited, dfsVisited) == true) {
25+
return true;
26+
}
27+
}
28+
}
29+
return false;
30+
}
31+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
// CORE: if children is already visited and
7+
// is not the parent of the node then there isa cycle
8+
public class cycledetectionInUndirectedGraph {
9+
private boolean isCycle(int V, ArrayList<ArrayList<Integer>> adj) {
10+
boolean[] visited = new boolean[V + 1];
11+
Arrays.fill(visited, false);
12+
for (int i = 1; i <= V; i++) {
13+
if (!visited[i]) {
14+
if (checkForCycle(adj, i, visited))
15+
return true;
16+
}
17+
}
18+
return false;
19+
}
20+
21+
static boolean checkForCycle(ArrayList<ArrayList<Integer>> adj, int s, boolean visited[]) {
22+
Queue<Node> q = new LinkedList<>(); // bfs queue
23+
q.add(new Node(s, -1)); // add source node parent of start Node is -1 <-- 0
24+
visited[s] = true;
25+
while (!q.isEmpty()) {
26+
int node = q.peek().self;
27+
int parent = q.peek().Parent;
28+
q.remove();
29+
for (Integer child : adj.get(node)) {
30+
if (!visited[child]) {
31+
q.add(new Node(child, node));
32+
visited[child] = true;
33+
} else if (child != parent) { // child is visited and is not parent, then cycle
34+
return true;
35+
}
36+
}
37+
}
38+
return false;
39+
40+
}
41+
42+
// driver code
43+
public static void main(String[] args) {
44+
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
45+
adj.add(new ArrayList<>(Arrays.asList(1, 2)));
46+
adj.add(new ArrayList<>(Arrays.asList(2, 3)));
47+
adj.add(new ArrayList<>(Arrays.asList(3, 4)));
48+
// adj.add(new ArrayList<>(Arrays.asList(4, 5)));
49+
cycledetectionInUndirectedGraph obj = new cycledetectionInUndirectedGraph();
50+
System.out.println(obj.isCycle(4, adj));
51+
}
52+
}
53+
54+
class Node {
55+
int self;
56+
int Parent;
57+
58+
public Node(int self, int Parent) {
59+
this.self = self;
60+
this.Parent = Parent;
61+
}
62+
}
63+
64+
class cycleDetectionUsingDSU{
65+
66+
}

0 commit comments

Comments
 (0)