题目如下:

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. 使用idea搭建Spring boot+jsp的简单web项目

    大家好: 这是我的第一篇博客文章,简单介绍一下Spring boot + jsp 的搭建流程,希望给跟我一样新接触Spring boot的读者一点儿启发. 开发工具:jdk1.8   idea2017 ...

  2. Jenkins之配置GitHub-Webhook2

    什么是持续集成(Continuous integration) 提出者Martin Fowler本人对持续集成是这样定义的:持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员每 ...

  3. Redis缓存在django中的配置

    django  settings中的配置 # 缓存 CACHES = { "default": { "BACKEND": "django_redis. ...

  4. Java并发与多线程与锁优化

    前言 目前CPU的运算速度已经达到了百亿次每秒,所以为了提高生产率和高效地完成任务,基本上都采用多线程和并发的运作方式. 并发(Concurrency):是指在某个时间段内,多任务交替处理的能力.CP ...

  5. 线程join方法 小demo

    1.第一个示例: package cn.threaddemo; public class T implements Runnable { public static int a = 0; @Overr ...

  6. JS中数据结构之散列表

    散列是一种常用的数据存储技术,散列后的数据可以快速地插入或取用.散列使用的数据 结构叫做散列表.在散列表上插入.删除和取用数据都非常快. 下面的散列表是基于数组进行设计的,数组的长度是预先设定的,如有 ...

  7. 【LeetCode 73】矩阵置零

    题目链接 [题解] 如果a[i][j]==0. 就把第i行的第一个数字置为0 然后把第j列的第一个数字置为0 最后再处理下每行第一个为0的行.每列第一个为0的列. (第一行和第一列都得用同一个位置处理 ...

  8. moment.js 时间库

    一.概念:    https://www.cnblogs.com/Jimc/p/10591580.html    或    http://momentjs.cn/(官网) 1.Moment.js是一个 ...

  9. STM32时钟配置方法详解

      一.在STM32中,有五个时钟源,为HSI.HSE.LSI.LSE.PLL. ①HSI是高速内部时钟,RC振荡器,频率为8MHz. ②HSE是高速外部时钟,可接石英/陶瓷谐振器,或者接外部时钟源, ...

  10. 左手Mongodb右手Redis redis操作

    set key value  设置key的值 get key 取得key的值 decr key 值会减一 incr key 值会加一 decrby key value ,会让key的值减少value. ...