Skip to content

Latest commit

 

History

History
69 lines (51 loc) · 1.5 KB

_3111. Minimum Rectangles to Cover Points.md

File metadata and controls

69 lines (51 loc) · 1.5 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 21, 2024

Last updated : July 21, 2024


Related Topics : Array, Greedy, Sorting

Acceptance Rate : 63.04 %


Solutions

Java

class Solution {
    public int minRectanglesToCoverPoints(int[][] points, int w) {
        int[] xVals = new int[points.length];
        for (int i = 0; i < points.length; i++) {
            xVals[i] = points[i][0];
        }

        Arrays.sort(xVals);

        int prev = w * -1 - 1;
        int counter = 0;

        for (int i : xVals) {
            if (i - prev > w) {
                prev = i;
                counter++;
            }
        }

        return counter;

    }
}

Python

class Solution:
    def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int:
        counter = 0

        prev = -inf
        for x in sorted([x[0] for x in points]) :
            if x - prev <= w :
                continue
            counter += 1
            prev = x

        return counter