DEFKIN -Defense of a Kingdom

Pattern: Candy and assign cookies

medium

A tower guards its whole row and its whole column. The unguarded cells form rectangles between guarded rows and columns, and the biggest one uses the widest gap between neighbouring tower columns and the widest gap between neighbouring tower rows. Sort each list and take the largest gaps.

The problem

A board is w columns by h rows, numbered from 1. Each tower i stands at column xs[i] and row ys[i] and guards its entire row and column. Return the area of the largest rectangle of cells that no tower guards. w = 15, h = 8, towers at (3, 8), (11, 2), (8, 6) gives 12. The SPOJ version reads several test cases from input; here each call is one board.

Think first

  • Which cells are unguarded?
  • Do the rows and columns interact?
  • How do you find the longest run of unguarded columns?

Hints

    Brute force, and why it fails

    Mark the guarded rows and columns in arrays and scan: O(w + h), fine for small boards but not for large ones.

    The key insight

    Free columns and free rows are independent, so the biggest free rectangle is the widest column gap times the widest row gap.

    Dry run

    columns3 8 11
    rows2 6 8

    Board 15 × 8, towers at columns 3, 11, 8 and rows 8, 2, 6.

    Step 1 of 3

    The plan

    gap(list, size): sort; best = max(x - prev - 1) over neighbours, with prev starting at 0; also size - last
    return gap(xs, w) × gap(ys, h)
    

    Complexity

    Time
    O(n log n)
    Space
    O(n)

    Two sorts of the tower coordinates.

    Edge cases

    • No towers: the whole board.
    • Towers on every column: 0.
    • Two towers on the same column.
    I've built my logic, show the code

    Java

    import java.util.*;
    
    class Solution {
        public long largestUnguarded(int w, int h, int[] xs, int[] ys) {
            return (long) widestGap(xs, w) * widestGap(ys, h);   // free columns and free rows are independent
        }
    
        private int widestGap(int[] towers, int size) {          // the longest run of cells 1..size with no tower
            int[] t = towers.clone();
            Arrays.sort(t);
            int prev = 0, best = 0;
            for (int x : t) {
                best = Math.max(best, x - prev - 1);
                prev = x;
            }
            return Math.max(best, size - prev);                  // the run after the last tower
        }
    }

    C++

    class Solution {
    public:
        long long largestUnguarded(int w, int h, vector<int>& xs, vector<int>& ys) {
            return static_cast<long long>(widestGap(xs, w)) * widestGap(ys, h);   // free columns and rows are independent
        }
    
        int widestGap(vector<int> t, int size) {                // the longest run of cells 1..size with no tower
            sort(t.begin(), t.end());
            int prev = 0, best = 0;
            for (int x : t) {
                best = max(best, x - prev - 1);
                prev = x;
            }
            return max(best, size - prev);                      // the run after the last tower
        }
    };

    Python

    class Solution:
        def largestUnguarded(self, w: int, h: int, xs: list[int], ys: list[int]) -> int:
            return self.widest_gap(xs, w) * self.widest_gap(ys, h)   # free columns and free rows are independent
    
        def widest_gap(self, towers: list[int], size: int) -> int:   # the longest run of cells 1..size with no tower
            prev = best = 0
            for x in sorted(towers):
                best = max(best, x - prev - 1)
                prev = x
            return max(best, size - prev)           # the run after the last tower

    Common mistakes

    • Forgetting the gaps at the edges.
    • Counting the towers' own column in the gap (subtract 1).

    Similar problems