题目如下:

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. C/C++ C++ 11 兰姆达

    { auto layer = Layer::create();    auto event = cocos2d::EventListenerTouchOneByOne::create();    ev ...

  2. spring-boot整合shiro实现权限管理

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...

  3. filebeat配置详解

    从input读取事件源,经过相应解析和处理之后,从output输出到目标存储库(elasticsearch或其他).输入可以从Log.Syslog.Stdin.Redis.UDP.Docker.TCP ...

  4. 用C#编写ActiveX控件,开发浏览器控件,注册ActiveX 控件

    用C#编写ActiveX控件,开发浏览器控件,注册ActiveX 控件用C#编写ActiveX控件 开发浏览器控件这是本控件开发完成后的一个简单应用.我们可以利用它以本地文件夹为单位来批量更新服务器的 ...

  5. window安装nexus和配置

    原文 http://www.cnblogs.com/fxmemory/p/7133672.html 一. 认识mav仓库 1.1 maven仓库的作用   回想之前不用maven的时候,我们用ecli ...

  6. flask-路转换器

    from flask import Flask, render_template from werkzeug.routing import BaseConverter # 配置regex路由转换器 # ...

  7. Windows 08 R2_创建AD DS域服务(图文详解)

    目录 目录 Active Directory概念 创建第一个AD域控制器 搭建DNS服务器 使用Windows窗口程序创建AD域控制器 AD与LDAP的关系 使用Powershell来创建ADDS域控 ...

  8. Java学习之多态---类成员变化

    类成员 一.成员变量 编译时:变量(f)所属类(Fu)中是否有成员变量,有:编译成功,没有:编译失败 运行时:变量(f)所属类(Fu)中是否有成员变量,运行该类(Fu)中的成员变量 class Fu ...

  9. centos6.2 shutdown now关机进入单用户模式

    在centos5.5时当我们输入 shutdown now 系统会进入关机状态.而centos6.2时并非如此,其他版本不清楚,而进入了单用户模式.(进入系统后想维护可做此操作.)会出现如下提示:(注 ...

  10. lua string.byte怎么判断是中文还是别的

    string.byte(char)>127是中文,string.sub(s,i,i+2),占两个字节,中文 string.byte(char)<=127是普通字符,咱一个字节