【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 ...
随机推荐
- 解决pip安装第三方包编码错误:UnicodeDecodeError: 'ascii' codec can't decode byte....
.../python27/Lib/mimetypes.py 在 import之后添加下列内容 if sys.getdefaultencoding() != 'gbk': reload(sys) sys ...
- 解决网页ICON图标无法显示的问题
第一步:检查下“<link rel="shortcut icon" href="http://www.bhcode.net/favicon.ico" ty ...
- 关于Java中excel表格导出的总结(Java程序导出模板和Java根据模板导出表格两种实现方式)
导出excel通用模板(程序定义模板导出) 转载原文:https://www.jianshu.com/p/5c7b359a159c 如下代码,本方法主要用于程序定义模板格式,并导出文件.该方法将定义和 ...
- 吉首大学2019年程序设计竞赛(重现赛)-J(树形DP)
题目链接:https://ac.nowcoder.com/acm/contest/992/J 题意:题意很清晰,就是求任意两点距离的和,结果对1e9+7取模. 思路:裸的树形DP题,一条边的贡献值=这 ...
- ZooKeeper常用命令行操作
ZooKeeper常用命令行操作 通过./zkCli.sh 打开zk的客户端进入命令行后台 ls/ls2 列出当前节点下的子节点 ls2还会列出当前节点的状态 [zk: localhost:2181( ...
- Java基础开篇
我是一个2019毕业的非计算机的毕业生,从大二开始喜欢上Java直到现在一直都在学习,Brid从小就对计算机感兴趣,可惜高中的时候不懂事,没有规划未来,考上了一所专科学院,然后大一并不能转专业,现在毕 ...
- STL vector常见用法详解
<算法笔记>中摘取 vector常见用法详解 1. vector的定义 vector<typename> name; //typename可以是任何基本类型,例如int, do ...
- Feign声明式服务调用
Feign是一种声明式.模板化的HTTP客户端(仅在Application Client中使用).声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求. Spring Clo ...
- @Value中冒号的作用
先说明冒号的作用 :可以设置默认值 @Value中可以使用 @Value("${hello:defaultValue}") private String hello; 若找不到属性 ...
- 关于redis的几件小事(二)redis线程模型
1.memcached和redis有什么区别? (1)Redis支持服务器端的数据操作 redis和memcached相比,redis拥有更多的 数据结构并且支持更丰富的数据操作 ,通常在memcac ...