题目如下:

(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. 页面可见性改变事件 : visibilitychange 详解

    1.Page Visibility API标准概述 查看W3C的官方文档时候看到这个属性 标准时间线是这样介绍的: Page Visibility Level 2 W3C Proposed Recom ...

  2. 时间转换:DateTimeExtensions

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. Zebra架构与大数据架构优劣对比

    zebra架构的缺点 文件存储方式的缺点 1. 一个文件如果特别大,这样会造成存储的问题.一台机器的磁盘也不可能存储特别大的文件 2. 该文件在查询.删除.修改的时候都会存在问题 3. 如果文件太多, ...

  4. 02-Zookeeper介绍及安装

    1 Zookeeper介绍 ZooKeeper是为分布式应用所设计的高可用.高性能且一致的开源协调服务,它提供了一项基本服务:分布式锁服务.分布式应用可以基于它实现更高级的服务,实现诸如同步服务.配置 ...

  5. .Net Core Grpc 实现通信

    .Net Core 3.0已经把Grpc作为一个默认的模板引入,所以我认为每一个.Net程序员都有学习Grpc的必要,当然这不是必须的. 我在我的前一篇文章中介绍并创建了一个.Net Core 3.0 ...

  6. SQL之事务

    ●事务的ACID(acid)属性 ➢1.原子性(Atomicity ) 原子性是指事务是-一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生. ➢2. 一致性(Consistency) 事务 ...

  7. Java Web开发技术教程入门-Model1和Model2

    今天我们聊聊JSP开发中的Model1和Model2. Model1采用了JSP+JavaBean技术开发Web应用.其中,JSP实现页面显示,业务逻辑和流程控制,数据处理由JavaBean完成.在J ...

  8. CSP前模板复习

    Tarjan 求强连通分量 展开查看 #include #include #include using namespace std; const int N = 1e4 + 1e3; int n, m ...

  9. JavaSSM框架精选50道面试题

    JavaSSM框架精选50道面试题 2019年02月13日 19:04:43 EerhtSedah 阅读数 7760更多 分类专栏: 面试题   版权声明:本文为博主原创文章,遵循CC 4.0 BY- ...

  10. vim /etc/security/limits.conf中的hard和soft

    转自:https://blog.csdn.net/zxljsbk/article/details/89153690 "soft" 和 "hard" 的区别sof ...