题目如下:

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: 5

Example 2:

Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
Output: 3

Example 3:

Input: stones = [[0,0]]
Output: 0

Note:

  1. 1 <= stones.length <= 1000
  2. 0 <= stones[i][j] < 10000

解题思路:题目解法本身不难,难点在于包了一个壳,去掉壳后就能看到本质了。本题的壳就是分组,把所有行相同或者列相同的元素分进同一个组,计算出一个有多少个组,删除操作完成后,每个组都会留下一个元素。最后的结果就是stones的个数减去组的个数。至于怎么求组的个数,DFS或者并查集都是可行的方法。我的解法是用DFS,最初用的是visit数组来保存元素是否遍历过,但是python语言会超时,只好改成每遍历完一个元素,都将其从stones中删除。

代码如下:

class Solution(object):
def removeStones(self, stones):
"""
:type stones: List[List[int]]
:rtype: int
"""
group = 0
original_len = len(stones)
while len(stones) > 0:
group += 1
queue = [(stones[0][0],stones[0][1])]
del stones[0]
while len(queue) > 0:
r, c = queue.pop(0)
inx_internal = 0
while inx_internal < len(stones):
if r == stones[inx_internal][0] or c == stones[inx_internal][1]:
queue.append((stones[inx_internal][0], stones[inx_internal][1]))
del stones[inx_internal]
else:
inx_internal += 1
return original_len - group

【leetcode】947. Most Stones Removed with Same Row or Column的更多相关文章

  1. 【LeetCode】947. Most Stones Removed with Same Row or Column 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...

  2. LeetCode 947. Most Stones Removed with Same Row or Column

    原题链接在这里:https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/ 题目: On a 2D plane ...

  3. 【LeetCode】1007. Minimum Domino Rotations For Equal Row 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历一遍 日期 题目地址:https://leetc ...

  4. 【LeetCode】Jewels and Stones(宝石与石头)

    这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...

  5. 【LeetCode】1033. Moving Stones Until Consecutive 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 脑筋急转弯 日期 题目地址:https://leet ...

  6. 【Leetcode】Jewels and Stones

    Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...

  7. 【leetcode】1033. Moving Stones Until Consecutive

    题目如下: Three stones are on a number line at positions a, b, and c. Each turn, you pick up a stone at ...

  8. Leetcode: 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 ...

  9. 【leetcode】Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

随机推荐

  1. MySQL索引优化之双表示例

    select * from tableA a left join tableB b on a.f_id = b.id; 索引建tableB表上面, 因为left join 注定左表全都有,所以应该关心 ...

  2. 【Linux】运维常用命令

    1.查看进程 ps -ef 如果需要查看特定的进程,比如redis的 ps -ef | grep redis 2.强制杀死进程  kill -9 进程id 3.忽略输出后台启动 nohup ./red ...

  3. webbrowser控件显示word文档

    参照某网站上的步骤(http://www.kuqin.com/office/20070909/968.html)首先,在Visual Studio中创建一个C#语言的Windows应用程序,然后在左侧 ...

  4. [CSP-S模拟测试]:v(hash表+期望DP)

    题目背景 $\frac{1}{4}$遇到了一道水题,又完全不会做,于是去请教小$D$.小$D$看了$0.607$眼就切掉了这题,嘲讽了$\frac{1}{4}$一番就离开了.于是,$\frac{1}{ ...

  5. 2018-2019-2 20175213实验四 《Android开发基础》实验报告

    一.实验报告封面 课程:Java程序设计 班级:1752班 姓名:吕正宏 学号:20175213 指导教师:娄嘉鹏 实验日期:2019年5月14日 实验时间:13:45 - 21:00 实验序号:实验 ...

  6. oracle函数自治事务解决不能增改删的语句操作

    CREATE OR REPLACE FUNCTION SEQ3 (v_bname in VARCHAR2) return NUMBER is pragma autonomous_transaction ...

  7. mongodb配置详解

    #启用日志文件,默认启用 journal=true #这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false quiet=false # 日志文件位置 logpath=/usr/loc ...

  8. HBase 中加盐之后的表如何读取:Spark 篇

    在 <HBase 中加盐之后的表如何读取:协处理器篇> 文章中介绍了使用协处理器来查询加盐之后的表,本文将介绍第二种方法来实现相同的功能. 我们知道,HBase 为我们提供了 hbase- ...

  9. python基础实现tcp文件传输

    准备工作,实现文件上传需要那些工具呢? socket(传输).open()(打开文件).os(读取文件信息),当然还有辅助类sys和json,下面我们开始吧 import socket,sys imp ...

  10. 洛谷 P1522 牛的旅行 Cow Tours——暴力枚举+最短路

    先上一波题目  https://www.luogu.org/problem/P1522 这道题其实就是给你几个相互独立的连通图 问找一条新的路把其中的两个连通图连接起来后使得新的图中距离最远的两个点之 ...