Untitled

mail@pastecode.io avatar
unknown
python
2 months ago
679 B
1
Indexable
Never
def find_densest_rectangle(points):
    max_density = -1.0
    
    for i in range(len(points)):
        for j in range(i + 1, len(points)):
            x1, y1 = points[i]
            x2, y2 = points[j]
            width = abs(x2 - x1)
            height = abs(y2 - y1)
            area = width * height
            
            if area == 0:
                continue
            
            count = 0
            for x, y in points:
                if x1 <= x <= x2 and y1 <= y <= y2:
                    count += 1
            
            density = count / area
            max_density = max(max_density, density)
    
    return max_density if max_density != -1.0 else -1.0
Leave a Comment