Most Stones Removed with Same Row or Column
On a 2D plane, we place stones at some integer coordinate points. Each coordinate point may have at most one stone.
Now, a_move_consists of removing a stone that shares a column or row with another stone on the grid.
What is the largest possible number of moves we can make?
Example 1:
Input:
stones =
[[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
Output:
5Example 2:
Input:
stones =
[[0,0],[0,2],[1,1],[2,0],[2,2]]
Output:
3Example 3:
Input:
stones =
[[0,0]]
Output:
0Note:
分析
每个石头把行列连起来变成一个岛,答案就是移到每个岛只剩一个石头。
这里DFS就是loop每个点,把行列连起来。 优化是把行列范围打散,行在1-N,j+10000列在N-2N,区分行列。
简单把i,j当做数字相连就好,就能理解打散区间的做法
参考https://www.jianshu.com/p/30d2058db7f7
DFS
Union Find
UNION FIND (i,~j)也是,等于把j映射到j+10000。~j是补码,就是二进制code倒过来。
Last updated
Was this helpful?