LeetCode 3453: Separate Squares I Binary Search Solution

Day 472 of my #500DaysOfCode challenge 🚀 ✅ LeetCode 3453: Separate Squares I (Medium) 📌 Problem Summary: We are given multiple squares on a 2D plane, each defined by bottom-left coordinate (x, y) and side length (l). The task is to find the minimum y-coordinate of a horizontal line such that: ✅ Total area above the line = Total area below the line ⚠️ Important note: Squares may overlap, and overlapping area should be counted multiple times (each square contributes independently). 💡 Key Insight: For any horizontal line y = mid, each square contributes to the area below based on how much of its height lies below the line: If the line is below the square → contributes 0 If the line is above the square → contributes full area l² If the line cuts the square → contributes l * (mid - y) Since the area below increases monotonically as the line moves upward, we can apply: ✅ Binary Search on the y-coordinate 🎯 Goal: Find smallest y where areaBelow(y) ≥ totalArea / 2. ⚡ Complexity: ✅ Time: O(n log range) (binary search iterations × n squares) ✅ Space: O(1) This was a great problem to reinforce how binary search can be used beyond arrays, especially when the function is monotonic 📈. #leetcode #dsa #java #binarysearch #programming #problemsolving #500daysofcode #consistency #learning

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories