Most Stones Removed with Same Row or Column
Medium
Topics
On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may have at most one stone.
A stone can be removed if it shares either the same row or the same column as another stone that is still on the plane.
Given an array stones of length n where stones[i] = [x, y] represents the location of the i-th stone, return the largest possible number of stones that can be removed. On this judge the array is passed as a grid with two columns.
Example 1
Input: grid = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]] Output: 5 Explanation: All six stones are connected, so all but one can be removed.
Example 2
Input: grid = [[0,0],[0,2],[1,1],[2,0],[2,2]] Output: 3
Example 3
Input: grid = [[0,0]] Output: 0 Explanation: A lone stone shares no row or column, so it cannot be removed.
Constraints
- 1 <= stones.length <= 1000
- 0 <= x, y <= 10^4
- No two stones are at the same coordinate point
Run ⌘' · Submit ⌘⏎