【leetcode】1274. Number of Ships in a Rectangle
题目如下:
(This problem is an interactive problem.)
On the sea represented by a cartesian plane, each ship is located at an integer point, and each integer point may contain at most 1 ship.
You have a function
Sea.hasShips(topRight, bottomLeft)which takes two points as arguments and returnstrueif and only if there is at least one ship in the rectangle represented by the two points, including on the boundary.Given two points, which are the top right and bottom left corners of a rectangle, return the number of ships present in that rectangle. It is guaranteed that there are at most 10 ships in that rectangle.
Submissions making more than 400 calls to
hasShipswill be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification.Example :
Input:
ships = [[1,1],[2,2],[3,3],[5,5]], topRight = [4,4], bottomLeft = [0,0]
Output: 3
Explanation: From [0,0] to [4,4] we can count 3 ships within the range.Constraints:
- On the input
shipsis only given to initialize the map internally. You must solve this problem "blindfolded". In other words, you must find the answer using the givenhasShipsAPI, without knowing theshipsposition.0 <= bottomLeft[0] <= topRight[0] <= 10000 <= bottomLeft[1] <= topRight[1] <= 1000
解题思路:四分查找法。每次把矩形分成上下左右四部分,如果哪部分有船,继续对这部分四分处理。
代码如下:
# """
# This is Sea's API interface.
# You should not implement it, or speculate about its implementation
# """
#class Sea(object):
# def hasShips(self, topRight, bottomLeft):
# """
# :type topRight: Point
# :type bottomLeft: Point
# :rtype bool
# """
#
#class Point(object):
# def __init__(self, x, y):
# self.x = x
# self.y = y class Solution(object):
def countShips(self, sea, topRight, bottomLeft):
"""
:type sea: Sea
:type topRight: Point
:type bottomLeft: Point
:rtype: integer
"""
self.res = 0
dic = {}
dic_history = {}
def recursive(top, bottom):
#print top.x,top.y,bottom.x,bottom.y
if (top.x,top.y,bottom.x,bottom.y) in dic_history:return
dic_history[(top.x,top.y,bottom.x,bottom.y)] = 1
if sea.hasShips(top, bottom) == False: return
if top.x == bottom.x and top.y == bottom.y:
if (top.x,top.y,bottom.x,bottom.y) not in dic and sea.hasShips(top, bottom) :
self.res += 1
dic[(top.x,top.y,bottom.x,bottom.y)] = 1
return
if top.x - bottom.x <= 1 and top.y - bottom.y <= 1:
recursive(bottom, bottom)
recursive(top,top)
recursive(Point(bottom.x, top.y), Point(bottom.x, top.y))
recursive(Point(top.x, bottom.y), Point(top.x, bottom.y))
return mid_x = (top.x + bottom.x) / 2
mid_y = (top.y + bottom.y) / 2
if mid_x-1 >= bottom.x and mid_y- 1 >= bottom.y:
recursive(Point(mid_x-1, mid_y-1), bottom)
if mid_y-1 >= bottom.y:
recursive(Point(top.x, mid_y-1), Point(mid_x, bottom.y))
if mid_x - 1 >= bottom.x:
recursive(Point(mid_x-1, top.y),Point(bottom.x, mid_y))
recursive(top,Point(mid_x, mid_y)) recursive(topRight, bottomLeft)
#print sea.count
return self.res
【leetcode】1274. Number of Ships in a Rectangle的更多相关文章
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...
- 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...
- 【LeetCode】447. Number of Boomerangs 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
随机推荐
- 【OpenGL】初识OpenGL4.0
目录(?)[-] 什么是GLSL GLEW 安装GLEW 使用GLEW 其他库 使用GLM库进行数学运算 安装GLM 使用GLM 使用GLM作为OpenGL的输入 使用GLFW进行窗口管理 这篇文章主 ...
- 【DSP开发】C6678的中断控制器
分两层,一层是每个core内部的中断控制器,这个叫interrupt controller,简写intc:一层是整个芯片的,属于芯片级的,在每个core的外面,这个叫chip-level interr ...
- mysql——创建表、修改表、删除表(概念详细讲解)
一.创建一个数据表 create table 表名 ( 列名1 数据类型1 [完整性约束条件], 列名2 数据类型2 [完整性约束条件], 列名3 数据类型3 [完整性约束条件], 列名4 数据类型4 ...
- Hive怎么使用远程连接
HIVE的连接模式== 本地连接模式 直接启动hive命令 HIVE的远程连接 这里要启动HIVE的服务 thirft进行编写 hiveserver2 —- > 前台启动 后台启动 前台启动 h ...
- 设计模式:备忘录模式(Memento)
个人比较喜欢玩单机游戏,什么仙剑.古剑.鬼泣.使命召唤.三国无双等等一系列的游戏我都玩过(现在期待凡人修仙传),对于这些游戏除了剧情好.场面大.爽快之外,还可以随时存档,等到下次想玩了又可以从刚开始的 ...
- 洛谷 P1268 树的重量 题解
题面 目的:求出树的各边长度 条件:每个节点之间最短路.整个图中不存在负边 我们可以每一次把一个点加入树内,求出这个点和已经构建好的树的边的长度: 这个长度抽象理解一下就是(dis[i][j]+dis ...
- 多表表与表关系 增删改查 admin
今日内容 多表表与表关系 增删改查表数据 admin 多表操作 表与表关系 默认指向主键 可能是隐藏主键 djamgo1.1默认级联(models. SET NULL解除级联) 一对一 先建立少的一方 ...
- ion-icon
观察默认的使用 关于Ion-icon 图标的自定义,首先,看一看默认内置的图标的显示,是怎么来的 可以看到默认的路径为:/svg/ios-xxx.svg 自定义实现 那么这种路径是哪里来的,明显是an ...
- O009、KVM 网络虚拟化基础
参考https://www.cnblogs.com/CloudMan6/p/5289590.html 网络虚拟化是虚拟化技术中最复杂的部分,学习难度最大. 但因为网络是虚拟化中非常重要的资源, ...
- 07 Deque的应用案例-回文检查
- 回文检测:设计程序,检测一个字符串是否为回文. - 回文:回文是一个字符串,读取首尾相同的字符,例如,radar toot madam. - 分析:该问题的解决方案将使用 deque 来存储字符串 ...