题目如下:

(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. 【神经网络与深度学习】【C/C++】C++日志操作开源函数库之Google-glog

    今天想给我的C++项目找一个开源的日志类,用于记录系统日志,结果浪费了半个下午的时间.从网上搜索相关资料,找到以下几个备选方案: 1.log4cplus 下载地址:http://sourceforge ...

  2. 【DSP开发】【并行计算-CUDA开发】TI OpenCL v01.01.xx

    TI OpenCL v01.01.xx TI OpenCL™ Runtime Documentation Contents: Introduction OpenCL 1.1 Reference Mat ...

  3. ipcs查看消息队列命令

    修改消息队列大小: root:用户: /etc/sysctl.conf kernel.msgmnb =4203520 #kernel.msgmnb =3520 kernel.msgmni = 2878 ...

  4. 【数据库】Redis/MongoDB/MySQL/Oracle随笔索引

    数据库体系 [思维导图]数据库体系 密码: a8ni Redis JPA

  5. Redhat7 安装 yum(换成免费版) 安装gcc

    最近上Linux系统基础课程,要在虚拟机上编译运行程序,这时候就需要安装gcc,网上一搜,各种什么在线,离线安装,其中在线安装很方面,一个命令 yum install gcc 即可解决 可我这么输入后 ...

  6. 从零开始,SpreadJS新人学习笔记【第3周】

    表单&函数 阔别多日, SpreadJS新人学习笔记,本周起正式回归!(在断更的这一个月中,我为大家先后录制了14期SpreadJS产品入门系列学习视频,希望帮助那些正在学习和使用 Sprea ...

  7. python-day37(正式学习)

    前景回顾 抢票系统的代码优化,使用了Lock类 from multiprocessing import Process,Lock import os,time,json with open('user ...

  8. Git复习(二)之远程仓库、注册GitHub账号、SSH警告、使用GitHub

    远程仓库 Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上.怎么分布呢?最早,肯定只有一台机器有一个原始版本库,此后,别的机器可以“克隆”这个原始版本库,而且每台机器的版本库其实都 ...

  9. Django框架——基础教程(总)

    1. Django简介 Python下有许多款不同的 Web 框架.Django是重量级选手中最有代表性的一位.许多成功的网站和APP都基于Django. Django是一个开放源代码的Web应用框架 ...

  10. jieba:我虽然结巴,但是我会分词啊

    介绍 jieba目前是一款比较好分词模块 分词 import jieba # 可以使用jieba.cut进行分词 sentence = "失去恋人所带来的苦痛远远超过了他的承受范围" ...