Dashboard Temp Share Shortlinks Frames API

HTMLify

separate-squares-i.py
Views: 5 | Author: prakhardoneria
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
    def separateSquares(self, squares: List[List[int]]) -> float:
        total_area = sum(s[2] * s[2] for s in squares)
        
        low = min(s[1] for s in squares)
        high = max(s[1] + s[2] for s in squares)
        
        for _ in range(100):
            mid = (low + high) / 2
            area_below = 0
            
            for x, y, l in squares:
                if mid <= y:
                    continue
                elif mid >= y + l:
                    area_below += l * l
                else:
                    area_below += l * (mid - y)
            
            if area_below < total_area / 2:
                low = mid
            else:
                high = mid
                
        return low