699. Falling Squares

Difficulty:
Related Topics:
Similar Questions:

Problem

There are several squares being dropped onto the X-axis of a 2D plane.

You are given a 2D integer array positions where positions[i] = [lefti, sideLengthi] represents the ith square with a side length of sideLengthi that is dropped with its left edge aligned with X-coordinate lefti.

Each square is dropped one at a time from a height above any landed squares. It then falls downward (negative Y direction) until it either lands on the top side of another square or on the X-axis. A square brushing the left/right side of another square does not count as landing on it. Once it lands, it freezes in place and cannot be moved.

After each square is dropped, you must record the height of the current tallest stack of squares.

Return **an integer array *ans* where ans[i] represents the height described above after dropping the ith square**.

  Example 1:

Input: positions = [[1,2],[2,3],[6,1]]
Output: [2,5,5]
Explanation:
After the first drop, the tallest stack is square 1 with a height of 2.
After the second drop, the tallest stack is squares 1 and 2 with a height of 5.
After the third drop, the tallest stack is still squares 1 and 2 with a height of 5.
Thus, we return an answer of [2, 5, 5].

Example 2:

Input: positions = [[100,100],[200,100]]
Output: [100,100]
Explanation:
After the first drop, the tallest stack is square 1 with a height of 100.
After the second drop, the tallest stack is either square 1 or square 2, both with heights of 100.
Thus, we return an answer of [100, 100].
Note that square 2 only brushes the right side of square 1, which does not count as landing on it.

  Constraints:

Solution (Java)

class Solution {
    class SegTree {

        class Node {
            int l, r, v, lazy;

            public Node(int l, int r) {
                this.l = l;
                this.r = r;
            }
        }

        private Node[] tr;

        public SegTree(int size) {
            tr = new Node[size << 2];
            build(1, 0, size - 1);
        }

        private void pushup(int u) {
            tr[u].v = Math.max(tr[u << 1].v, tr[u << 1 | 1].v);
        }

        private void pushdown(int u) {
            int c = tr[u].lazy;
            if (c != 0) {
                tr[u].lazy = 0;
                tr[u << 1].v = tr[u << 1 | 1].v = c;
                tr[u << 1].lazy = tr[u << 1 | 1].lazy = c;
            }
        }

        public void build(int u, int l, int r) {
            tr[u] = new Node(l, r);
            if (l == r) {
                return;
            }

            int mid = l + r >> 1;
            build(u << 1, l, mid);
            build(u << 1 | 1, mid + 1, r);
        }

        public void update(int u, int l, int r, int c) {
            if (l <= tr[u].l && tr[u].r <= r) {
                tr[u].v = tr[u].lazy = c;
                return;
            }

            pushdown(u);
            int mid = tr[u].l + tr[u].r >> 1;
            if (l <= mid) {
                update(u << 1, l, r, c);
            }
            if (mid + 1 <= r) {
                update(u << 1 | 1, l, r, c);
            }
            pushup(u);
        }

        public int query(int u, int l, int r) {
            if (l <= tr[u].l && tr[u].r <= r) {
                return tr[u].v;
            }

            pushdown(u);
            int res = 0, mid = tr[u].l + tr[u].r >> 1;
            if (l <= mid) {
                res = Math.max(res, query(u << 1, l, r));
            }
            if (mid + 1 <= r) {
                res = Math.max(res, query(u << 1 | 1, l, r));
            }

            return res;
        }

        public int query() {
            return tr[1].v;
        }
    }

    public List<Integer> fallingSquares(int[][] positions) {
        List<Integer> xs = new ArrayList<>();
        for (int[] p : positions) {
            int a = p[0], b = a + p[1];
            xs.add(a * 2);
            xs.add(b * 2);
            xs.add(a + b);
        }
        xs = unique(xs);

        SegTree segTree = new SegTree(xs.size());

        List<Integer> res = new ArrayList<>();
        for (int[] p : positions) {
            int a = p[0], b = a + p[1];
            a = get(a * 2, xs);
            b = get(b * 2, xs);
            int h = segTree.query(1, a + 1, b - 1);
            segTree.update(1, a, b, h + p[1]);
            res.add(segTree.query());
        }

        return res;
    }

    private int get(int x, List<Integer> xs) {
        int l = 0, r = xs.size() - 1;
        while (l < r) {
            int m = l + r >> 1;
            if (xs.get(m) >= x) {
                r = m;
            } else {
                l = m + 1;
            }
        }

        return l;
    }

    private List<Integer> unique(List<Integer> list) {
        list.sort(Integer::compareTo);
        int j = 0;
        for (int i = 0; i < list.size(); i++) {
            if (i == 0 || list.get(j - 1) != list.get(i)) {
                list.set(j++, list.get(i));
            }
        }

        return list.subList(0, j);
    }
}

Explain:

nope.

Complexity: