Untitled

mail@pastecode.io avatar
unknown
java
a month ago
2.0 kB
1
Indexable
Never
class Solution {
    
    record Point(int x, int y, int r, int id) {
        
        @Override
        public int hashCode() {
            return id;
        }
        
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Point point = (Point) o;
            return id == point.id;
        }
    }
    
    private double getDistance(int x1, int y1, int x2, int y2) {
        return ((y2 - y1) * (y2 - y1)) + ((x2 - x1) * (x2 - x1));
    }
    
    private int dfs(Point p, Map<Point, List<Point>> graph, Set<Point> seen) {
        if (seen.contains(p)) {
            return 0;
        }
        
        seen.add(p);
        int value = 1;
        for (int i = 0; i < graph.get(p).size(); i++) {
            value += dfs(graph.get(p).get(i), graph, seen);
        }
        return value;
    }
    
    public int maximumDetonation(int[][] bombs) {
        
        Map<Point, List<Point>> graph = new HashMap<>();
        
        for (int i = 0; i < bombs.length; i++) {
            Point p = new Point(bombs[i][0], bombs[i][1], bombs[i][2], i);
            graph.put(p, new ArrayList<>());
        }
        
        for (var entry1: graph.entrySet()) {
            for (var entry2: graph.entrySet()) {
                if (entry1 == entry2) {
                    continue;
                }
                Point p1 = entry1.getKey();
                Point p2 = entry2.getKey();
                
                double d = getDistance(p1.x, p1.y, p2.x, p2.y);
                if (d <= p1.r * p1.r) {
                    entry2.getValue().add(p1);
                }
            }
        }
        
        int maxTillNow = 0;
        for (Map.Entry<Point, List<Point>> entry: graph.entrySet()) {
            int size = dfs(entry.getKey(), graph, new HashSet<>());
            maxTillNow = Math.max(maxTillNow, size);
        }
        return maxTillNow;
    }
}
Leave a Comment