LeetCode Entry

2975. Maximum Square Area by Removing Fences From a Field

16.01.2026 medium 2026 kotlin rust

Max square after remove vertical or horizontal bars

2975. Maximum Square Area by Removing Fences From a Field medium blog post substack youtube

85f3cd69-628c-42a4-9194-065e536e30a8 (1).webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1239

Problem TLDR

Max square after remove vertical or horizontal bars #medium

Intuition

    // possible horizontal widths
    // intersect
    // possible vertical heights
    //
    // 1D problem: collect possible gaps
    //
    // |*****|**|*|
    //    5    2 1
    // 1,2,5, 1+2, 2+5, 1+2+5
    //             |+1 -- all prev + x
    // 1+1,(1+2)+1,(1+2+5)+1
    // max size is 600, so O(n^2) should be acceptable

Solve 1D problem. Find all possible gaps. Intersect. Pick max.

Approach

  • to find all gaps just use 2d for loop

Complexity

  • Time complexity: \(O(n^2)\)

  • Space complexity: \(O(n^2)\)

Code

// 648ms
    fun maximizeSquareArea(m: Int, n: Int, h: IntArray, v: IntArray) = listOf(h+1+m,v+1+n)
        .map { buildSet { for (a in it) for (b in it) if (a>b) add(a-b) }}
        .let {(a,b)->a.intersect(b).maxOrNull()}?.let {1L*it*it%1000000007} ?: -1
// 355ms
    pub fn maximize_square_area(m: i32, n: i32, mut h: Vec<i32>, mut v: Vec<i32>) -> i32 {
        let [a,b] = [(h,m),(v,n)].map(|(mut x,y)| {
            let mut s = HashSet::new(); x.extend([1,y]);
            for a in &x { for b in &x { if a > b { s.insert(a-b); }}}; s });
        a.intersection(&b).max().map_or(-1, |&x| ((x as i64*x as i64)%1000000007) as i32)
    }