题目如下:

(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 returns true if 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 hasShips will 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 ships is only given to initialize the map internally. You must solve this problem "blindfolded". In other words, you must find the answer using the given hasShips API, without knowing the ships position.
  • 0 <= bottomLeft[0] <= topRight[0] <= 1000
  • 0 <= 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的更多相关文章

  1. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  2. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  3. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  4. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  5. 【LeetCode】476. Number Complement (java实现)

    原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...

  6. 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...

  7. 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...

  8. 【LeetCode】447. Number of Boomerangs 解题报告(Java & Python)

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

  9. 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)

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

随机推荐

  1. JS延迟加载的几种方式

    参考链接:https://blog.csdn.net/meijory/article/details/76389762

  2. [c++] SYSTEM_INFO

    SYSTEM_INFO,Win32 API函数GetSystemInfo所使用的结构体. 说明 SYSTEM_INFO结构体包含了当前计算机的信息.这个信息包括计算机的体系结构.中央处理器的类型.系统 ...

  3. [Python3] 041 文件 持久化

    目录 文件 持久化 1. pickle 1.1 例子1 1.2 例子2 1.3 注意 2. shelve 2.1 举例 2.2 特性 2.3 强制写回 2.4 使用 with 管理上下文环境 文件 持 ...

  4. [转帖]How does a CPU work?

    How does a CPU work? https://milapneupane.com.np/2019/07/06/how-does-a-cpu-work/ CPU, also known as ...

  5. java生成0~9个9个不相等的整数

    HashSet<Integer> hs=new HashSet<Integer>(); Integer i=0; while (i<9){ int s=(int) Mat ...

  6. 完全删除MySQL及相关软件

    一.删除mysql服务 个人认为首先删除mysql服务最重要,这个多数人会忘记如何删除 首先是查看自己的mysql服务名,需要用这个服务名进行删除 进入命令行 二.卸载mysql,workbench等 ...

  7. 在字符串中找出第一个只出现一次的字符,Python实现

    要求: 1. 不能依赖库函数直接实现此功能,需使用基础的数据结构实现 2. 时间复杂度 O(n) 思路: 1. 用字典存储每个字符在字符串中出现的次数 2. 列表是有序的,用来存储字符的出现先后 3. ...

  8. Mac 安装 Homebrew

    为什么要在 MAC 上安装 Homebrew 它干什么用的呢?我们知道在 CentOS 和 Ubuntu 上都有自己的包管理工具,但是在 MAC 上却没有这样类似的管理工具. # CentOS $ y ...

  9. TCP三次握手与四次挥手详解(最全面)

    目录 TCP的三次握手与四次挥手 TCP报文段的首部格式 TCP的工作原理 TCP 的流量控制 TCP的拥塞控制 拥塞控制与流量控制的关系 拥塞控制所起的作用 慢开始和拥塞避免 慢开始算法的原理 三次 ...

  10. Dockerfile安装jdk1.8 、部署java项目

    基础指令 FROM 基于哪个镜像MAINTAINER 用来写备注信息,例如作者.日期等.COPY 复制文件进入镜像(只能用相对路径,不能用绝对路径)ADD 复制文件进入镜像(可以用绝对路径,假如是压缩 ...