日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/7/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode 1274. 矩形内船只的数目(分治) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1. 題目
    • 2. 解題

1. 題目

(此題是 交互式問題 )

在用笛卡爾坐標系表示的二維海平面上,有一些船。

每一艘船都在一個整數點上,且每一個整數點最多只有 1 艘船。

有一個函數 Sea.hasShips(topRight, bottomLeft) ,輸入參數為右上角和左下角兩個點的坐標,當且僅當這兩個點所表示的矩形區域(包含邊界)內至少有一艘船時,這個函數才返回 true ,否則返回 false 。

給你矩形的右上角 topRight 和左下角 bottomLeft 的坐標,請你返回此矩形內船只的數目。

題目保證矩形內 至多只有 10 艘船。

調用函數 hasShips 超過400次 的提交將被判為 錯誤答案(Wrong Answer) 。
同時,任何嘗試繞過評測系統的行為都將被取消比賽資格。

示例:

輸入: 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塊。
/*** // 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;} }; # """ # 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 = yclass Solution(object): #py3def __init__(self):self.sum = 0def 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 0xmid = (topRight.x + bottomLeft.x)//2ymid = (topRight.y + bottomLeft.y)//2if topRight.x == bottomLeft.x and topRight.y == bottomLeft.y:self.sum += 1return self.sumself.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

我的CSDN博客地址 https://michael.blog.csdn.net/

長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!

總結

以上是生活随笔為你收集整理的LeetCode 1274. 矩形内船只的数目(分治)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。