Skip to content

Commit

Permalink
Added solutions to 3 new problems
Browse files Browse the repository at this point in the history
Added All Paths To Target, Kth Largest XOR, Matrix Block Sum
  • Loading branch information
jayanpahuja20 committed Oct 31, 2022
1 parent 0539f2d commit 982ea0c
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
78 changes: 78 additions & 0 deletions leetcode/AllPathstoTarget.java
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);
}
}
}
}
}
21 changes: 21 additions & 0 deletions leetcode/KthLargestXOR.java
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();
}
}
23 changes: 23 additions & 0 deletions leetcode/MatrixBlockSum.java
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;
}
}

0 comments on commit 982ea0c

Please sign in to comment.