前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 1274. 矩形内船只的数目(分治)

LeetCode 1274. 矩形内船只的数目(分治)

作者头像
Michael阿明
发布2021-02-19 10:48:42
5760
发布2021-02-19 10:48:42
举报
文章被收录于专栏:Michael阿明学习之路

文章目录

1. 题目

(此题是 交互式问题 )

在用笛卡尔坐标系表示的二维海平面上,有一些船。

每一艘船都在一个整数点上,且每一个整数点最多只有 1 艘船。

有一个函数 Sea.hasShips(topRight, bottomLeft) ,输入参数为右上角和左下角两个点的坐标,当且仅当这两个点所表示的矩形区域(包含边界)内至少有一艘船时,这个函数才返回 true ,否则返回 false 。

给你矩形的右上角 topRight 和左下角 bottomLeft 的坐标,请你返回此矩形内船只的数目。

题目保证矩形内 至多只有 10 艘船。

调用函数 hasShips 超过400次 的提交将被判为 错误答案(Wrong Answer) 。 同时,任何尝试绕过评测系统的行为都将被取消比赛资格。

示例:

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
输入:
ships = [[1,1],[2,2],[3,3],[5,5]], 
topRight = [4,4], bottomLeft = [0,0]
输出:3
解释:在 [0,0] 到 [4,4] 的范围内总共有 3 艘船。
 
提示:
ships 数组只用于评测系统内部初始化。
你无法得知 ships 的信息,所以只能通过调用 hasShips 接口来求解。
0 <= bottomLeft[0] <= topRight[0] <= 1000
0 <= bottomLeft[1] <= topRight[1] <= 1000

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/number-of-ships-in-a-rectangle 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 计算横纵坐标的中点,将矩形分成4块。
代码语言:javascript
复制
/**
 * // This is Sea's API interface.
 * // You should not implement it, or speculate about its implementation
 * class Sea {
 *   public:
 *     bool hasShips(vector<int> topRight, vector<int> bottomLeft);
 * };
 */

class Solution { //C++
	int sum = 0;
public:
    int countShips(Sea sea, vector<int> topRight, vector<int> bottomLeft) {
    	if(topRight[0] < bottomLeft[0] || topRight[1] < bottomLeft[1]
                || !sea.hasShips(topRight, bottomLeft))
    		return 0;
        if(topRight == bottomLeft)
    		return ++sum;
        int xmid = (topRight[0] + bottomLeft[0])/2;
        int ymid = (topRight[1] + bottomLeft[1])/2;
        countShips(sea, {xmid, ymid}, bottomLeft);
        countShips(sea,  {topRight[0], ymid}, {xmid+1, bottomLeft[1]});
        countShips(sea, {xmid, topRight[1]}, {bottomLeft[0], ymid+1});
        countShips(sea, topRight, {xmid+1, ymid+1});
        return sum;
    }
};
代码语言:javascript
复制
# """
# This is Sea's API interface.
# You should not implement it, or speculate about its implementation
# """
#class Sea(object):
#    def hasShips(self, topRight: 'Point', bottomLeft: 'Point') -> bool:
#
#class Point(object):
#   def __init__(self, x: int, y: int):
#       self.x = x
#       self.y = y

class Solution(object): #py3
    def __init__(self):
        self.sum = 0
    def countShips(self, sea: 'Sea', topRight: 'Point', bottomLeft: 'Point') -> int:
        if topRight.x < bottomLeft.x or topRight.y < bottomLeft.y or not sea.hasShips(topRight, bottomLeft):
            return 0
        xmid = (topRight.x + bottomLeft.x)//2
        ymid = (topRight.y + bottomLeft.y)//2
        if topRight.x == bottomLeft.x and topRight.y == bottomLeft.y:
            self.sum += 1
            return self.sum
        self.countShips(sea, Point(xmid, ymid), bottomLeft)
        self.countShips(sea, Point(topRight.x, ymid), Point(xmid+1, bottomLeft.y))
        self.countShips(sea, Point(xmid, topRight.y), Point(bottomLeft.x, ymid+1))
        self.countShips(sea, topRight, Point(xmid+1, ymid+1))
        return self.sum
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/08/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1. 题目
  • 2. 解题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档