1. 解题思路
这一题在这次比赛的4道题当中算是比较简单的,基本就只需要将所有的点排序之后然后使用贪婪算法来cover住所有的点即可。
2. 代码实现
给出python代码实现如下:
class Solution:
def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int:
points = sorted(points)
cnt, rb = 0, -1
idx, n = 0, len(points)
while idx < n:
while idx < n and points[idx][0] <= rb:
idx += 1
if idx < n:
cnt += 1
rb = points[idx][0] + w
return cnt
提交代码评测得到:耗时1010ms,占用内存62.2MB。