-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added All Paths To Target, Kth Largest XOR, Matrix Block Sum
- Loading branch information
1 parent
0539f2d
commit 982ea0c
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
class AllPathsToTarget { | ||
public List<List<Integer>> allPathsSourceTarget(int[][] graph) { | ||
Graph G = new Graph(graph.length); | ||
for (int i=0;i<graph.length;i++) | ||
{ | ||
for (int j=0;j<graph[i].length;j++) | ||
{ | ||
G.addEdge(i,graph[i][j]); | ||
} | ||
} | ||
List<List<Integer>> ans = new ArrayList<>(); | ||
int source = 0; | ||
ArrayList<Integer> temp = new ArrayList<>(); | ||
temp.add(0); | ||
dfs(source,graph,ans,temp); | ||
return ans; | ||
|
||
} | ||
public void dfs(int start, int[][] graph, List<List<Integer>> l, ArrayList<Integer> path) | ||
{ | ||
if (start==graph.length-1) | ||
{ | ||
l.add(new ArrayList<>(path)); | ||
return ; | ||
} | ||
|
||
for (int i=0;i<graph[start].length;i++) | ||
{ | ||
path.add(graph[start][i]); | ||
dfs(graph[start][i],graph,l,path); | ||
path.remove(path.size()-1); | ||
} | ||
} | ||
} | ||
class Graph | ||
{ | ||
public int V; | ||
public boolean[] visited; | ||
public ArrayList<ArrayList<Integer>> adj; | ||
|
||
// Constructor | ||
public Graph(int v) | ||
{ | ||
V = v; | ||
visited = new boolean[V]; | ||
adj = new ArrayList<>(); | ||
for (int i=0; i<v; ++i) | ||
adj.add(new ArrayList<>()); | ||
} | ||
|
||
// Function to add an edge into the graph | ||
public void addEdge(int v,int w) | ||
{ | ||
adj.get(v).add(w); | ||
} | ||
|
||
public void BFS(int s) | ||
{ | ||
LinkedList<Integer> queue = new LinkedList<Integer>(); | ||
visited[s]=true; | ||
queue.add(s); | ||
|
||
while (queue.size() != 0) | ||
{ | ||
s = queue.poll(); | ||
Iterator<Integer> i = adj.get(s).listIterator(); | ||
while (i.hasNext()) | ||
{ | ||
int n = i.next(); | ||
if (!visited[n]) | ||
{ | ||
visited[n] = true; | ||
queue.add(n); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class KthLargestXOR { | ||
public int kthLargestValue(int[][] matrix, int k) { | ||
int[][] dp = new int[matrix.length][matrix[0].length]; | ||
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); | ||
for (int i=0;i<matrix.length;i++) | ||
{ | ||
for (int j=0;j<matrix[0].length;j++) | ||
{ | ||
int a = (i-1 < 0 ? 0 : dp[i-1][j]); | ||
int b = (j-1 < 0 ? 0 : dp[i][j-1]); | ||
int c = ((i-1 < 0 || j-1 < 0) ? 0 : dp[i-1][j-1]); | ||
|
||
dp[i][j] = a^b^c^matrix[i][j]; | ||
pq.add(dp[i][j]); | ||
} | ||
} | ||
k--; | ||
while (k-- > 0) pq.remove(pq.peek()); | ||
return pq.peek(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class MatrixBlockSum { | ||
public int[][] matrixBlockSum(int[][] mat, int k) { | ||
int[][] sums = new int[mat.length+1][mat[0].length+1]; | ||
for (int i=0;i<mat.length;i++) | ||
{ | ||
for (int j=0;j<mat[0].length;j++) | ||
{ | ||
sums[i+1][j+1] = sums[i+1][j] + sums[i][j+1] - sums[i][j] + mat[i][j]; | ||
} | ||
} | ||
//System.out.println(cols[0] + " " + cols[1]); | ||
int[][] ans = new int[mat.length][mat[0].length]; | ||
for (int i=0;i<mat.length;i++) | ||
{ | ||
for (int j=0;j<mat[0].length;j++) | ||
{ | ||
int r1 = Math.max(0,i-k), r2 = Math.min(i+k+1,mat.length), c1 = Math.max(0,j-k), c2 = Math.min(j+k+1, mat[0].length); | ||
ans[i][j] = sums[r2][c2] + sums[r1][c1] - sums[r1][c2] - sums[r2][c1]; | ||
} | ||
} | ||
return ans; | ||
} | ||
} |