Skip to content

Back tracking #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 79 additions & 25 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/main/java/com/thealgorithm/graph/CollectCoinsInATree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.thealgorithm.graph;

/**
* @author: Subham Santra
*/
public class CollectCoinsInATree {

public int collectTheCoins(int[] coins, int[][] edges) {
return 0;
}

public static void main(String[] args) {

System.out.println(
new CollectCoinsInATree()
.collectTheCoins(
new int[] {1, 0, 0, 0, 0, 1},
new int[][] {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}}));
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/thealgorithm/graph/Edge.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public static <V> Edge<V, V> createSimpleEdgeUnWeighted(V v1, V v2) {
return new Edge<>(Vertex.create(v1), Vertex.create(v2), 0D);
}

public static <V> Edge<V, V> createEdge(V v1, V v2, double weight) {
return new Edge<>(Vertex.create(v1), Vertex.create(v2), weight);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -29,4 +33,9 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(getVertex1(), getVertex2(), getWeight());
}

@Override
public String toString() {
return "<" + vertex1.getKey() + "--" + vertex2.getKey() + "|" + weight + '>';
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/thealgorithm/graph/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ public void addEdge(Edge<K, V> edge) {
}
}
}

void clear() {
vertexSet.clear();
edgeSet.clear();
isDirected = false;
}
}
136 changes: 136 additions & 0 deletions src/main/java/com/thealgorithm/graph/MostStonesRemovedSameRowCol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.thealgorithm.graph;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
* @author: Subham Santra
*/
public class MostStonesRemovedSameRowCol {

static class Solution {
static class Vertex {
int row;
int col;

public Vertex(int row, int col) {
this.row = row;
this.col = col;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Vertex vertex)) return false;
return row == vertex.row && col == vertex.col;
}

@Override
public int hashCode() {
return Objects.hash(row, col);
}

@Override
public String toString() {
return "{" + row + ", " + col + '}';
}
}

/**
* The intuition is -- As all the vertices will be connected - we will use DFS to find out no of
* connected components
*
* @param stones
* @return
*/
public int removeStones(int[][] stones) {
Map<Vertex, Set<Vertex>> graph = new HashMap<>();
Map<Integer, Set<Vertex>> rowMap = new HashMap<>();
Map<Integer, Set<Vertex>> colMap = new HashMap<>();

for (int[] stone : stones) {
Vertex vertex = new Vertex(stone[0], stone[1]);
if (rowMap.containsKey(vertex.row)) {
for (Vertex sameRowVertex : rowMap.get(vertex.row)) {
graph.putIfAbsent(vertex, new HashSet<>());
graph.get(vertex).add(sameRowVertex);

graph.putIfAbsent(sameRowVertex, new HashSet<>());
graph.get(sameRowVertex).add(vertex);
}
}
if (colMap.containsKey(vertex.col)) {
for (Vertex sameColVertex : colMap.get(vertex.col)) {
graph.putIfAbsent(vertex, new HashSet<>());
graph.get(vertex).add(sameColVertex);

graph.putIfAbsent(sameColVertex, new HashSet<>());
graph.get(sameColVertex).add(vertex);
}
}

rowMap.putIfAbsent(vertex.row, new HashSet<>());
colMap.putIfAbsent(vertex.col, new HashSet<>());

rowMap.get(vertex.row).add(vertex);
colMap.get(vertex.col).add(vertex);
}

Set<Vertex> removedVertices = new HashSet<>();
int connectedComponents = 0;
for (int[] stone : stones) {
Vertex vertex = new Vertex(stone[0], stone[1]);
if (!removedVertices.contains(vertex)) {
++connectedComponents;
DFS(graph, removedVertices, vertex);
}
}

return stones.length - connectedComponents;
}

/**
* This method will check each vertex and remove it. This will continue to remove all the
* connected vertices
*
* @param graph
* @param removedVertices
* @param vertex
*/
private void DFS(Map<Vertex, Set<Vertex>> graph, Set<Vertex> removedVertices, Vertex vertex) {
if (removedVertices.contains(vertex)) {
return;
}

if (!graph.containsKey(vertex)) {
return;
}

removedVertices.add(vertex);
for (Vertex nextVertex : graph.get(vertex)) {
DFS(graph, removedVertices, nextVertex);
}
}
}

public static void main(String[] args) {
System.out.println(
new Solution().removeStones(new int[][] {{0, 0}, {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 2}}));

System.out.println(new Solution().removeStones(new int[][] {{0, 1}, {1, 0}, {1, 1}}));

System.out.println(
new Solution().removeStones(new int[][] {{0, 0}, {0, 2}, {1, 1}, {2, 0}, {2, 2}}));

System.out.println(
new Solution()
.removeStones(
new int[][] {
{0, 0}, {0, 1}, {1, 0}, {1, 1}, {2, 1}, {2, 2}, {3, 2}, {3, 3}, {3, 4}, {4, 3},
{4, 4}
}));
}
}
Loading