Most stones removed with same row or column

Graphs Hard Problems II Hard
  • This problem is useful in the domain of game development, computational geometry, and artificial intelligence
  • For instance, in strategy games like Go or Chess where stone (or piece) removal based on certain conditions is part of the game logic
  • The underlying concept can also be applied in spatial data processing, such as Geographic Information Systems (GIS) on tasks where you might want to remove some duplicate points from the same latitudes or longitudes
  • This problem helps program those types of scenarios effectively and efficiently

There are n stones at integer coordinate points on a 2D plane, with at most one stone per coordinate point. Some stones need to be removed.A stone can be removed if it shares the same row or the same column as another stone that has not been removed.


Given an array of stones of length n where stones[i] = [xi, yi] represents the location of the ith stone, return the maximum possible number of stones that can be removed.

Examples:

Input : n=6, stones = [[0, 0],[ 0, 1], [1, 0],[1, 2],[2, 1],[2, 2]]

Output: 5

Explanation: One of the many ways to remove 5 stones is to remove the following stones:

[0,0], [1,0], [0,1], [2,1], [1,2]

Input : n = 6, stones = [[0, 0], [0, 2], [1, 3], [3, 1], [3, 2], [4, 3]]

Output: 4

Explanation: We can remove the following stones: [0,0], [0,2], [1,3], [3,1]

Input: n = 2, stones = [[0, 0], [0, 2]]

Constraints

  •   1 <= n <=1000
  •   0 <= x[i], y[i]<= 104
  •   No two stones are at same position.

Hints

  • "reat each row and column as a node and stones as edges connecting them. Merge stones that share the same row or column using Union-Find. If there are C connected components, at least C stones must remain. The maximum stones that can be removed is n - C."
  • "Build a graph where each stone is a node. Perform DFS/BFS to count connected components. The maximum removals is again n - C."

Company Tags

Shopify IBM Bungie Epic Systems Pinterest MongoDB Ernst & Young Intel Wayfair Goldman Sachs PwC McKinsey & Company AMD Morgan Stanley Broadcom Twilio OYO Rooms Uber Unity Technologies eBay Bloomberg Freshworks Visa Cerner HCL Technologies Google Microsoft Amazon Meta Apple Netflix Adobe