LeetCode699. Falling Squares

來源:互聯網
上載者:User

標籤:center   output   ret   touch   是什麼   span   --   number   height   

On an infinite number line (x-axis), we drop given squares in the order they are given.

The i-th square dropped (positions[i] = (left, side_length)) is a square with the left-most point being positions[i][0] and sidelength positions[i][1].

The square is dropped with the bottom edge parallel to the number line, and from a higher height than all currently landed squares. We wait for each square to stick before dropping the next.

The squares are infinitely sticky on their bottom edge, and will remain fixed to any positive length surface they touch (either the number line or another square). Squares dropped adjacent to each other will not stick together prematurely.

Return a list ans of heights. Each height ans[i] represents the current highest height of any square we have dropped, after dropping squares represented by positions[0], positions[1], ..., positions[i].

Example 1:

Input: [[1, 2], [2, 3], [6, 1]]Output: [2, 5, 5]Explanation:

After the first drop of positions[0] = [1, 2]: _aa _aa ------- The maximum height of any square is 2.

After the second drop of positions[1] = [2, 3]: __aaa __aaa __aaa _aa__ _aa__ -------------- The maximum height of any square is 5. The larger square stays on top of the smaller square despite where its center of gravity is, because squares are infinitely sticky on their bottom edge.

After the third drop of positions[1] = [6, 1]: __aaa __aaa __aaa _aa _aa___a -------------- The maximum height of any square is still 5. Thus, we return an answer of [2, 5, 5].

Example 2:

Input: [[100, 100], [200, 100]]Output: [100, 100]Explanation: Adjacent squares don‘t get stuck prematurely - only their bottom edge can stick to surfaces.

分析

首先還是來明確一下題目的意思,在x軸上落下方塊,用方塊的最左下的頂點positions[i][0]和方塊的長度positions[i][1]來確定每個方塊的位置,那麼方塊的高是什麼呢?查了一下,原來squre是正方形的意思,那麼每個方塊的高度就知道了。將這些箱子從無限的高空中投擲到x軸,邊有交集的箱子會堆疊(邊界相接不算交集),查詢每個箱子投擲後的最大堆疊高度。

是不是有點像俄羅斯方塊?解法是首先利用之前演算法題遇到的interval來代表這些方塊,初始假設所有的方塊都落到了地上而不會堆疊,對於每個方塊我們去迭代它之前所有的方塊,檢查是否有方塊是應該在當前方塊的下面的,如果當前的interva和之前的interval有交集那麼則意味著當前的方塊應該在這個方塊之上。我們的目標是找到那個最高的square並且將當前方塊cur放在之前的方塊i之上,並且將方塊cur的高度設定為

 

cur.height = cur.height + previousMaxHeight;

 

要明確的是這裡的cur.height始終記錄的是放下當前cur方塊後整個x軸上堆疊的最大高度。previousMaxHeight記錄的是在的當前方塊cur之下的堆疊到的最大高度。

還是有些不是很明白,還是上代碼來具體分析

class Solution {    private class Interval {        int start, end, height;        public Interval(int start, int end, int height) {            this.start = start;            this.end = end;            this.height = height;        }    }    public List<Integer> fallingSquares(int[][] positions) {        List<Interval> intervals = new ArrayList<>();        List<Integer> res = new ArrayList<>();        int h = 0;        for (int[] pos : positions) {            Interval cur = new Interval(pos[0], pos[0] + pos[1] - 1, pos[1]);            h = Math.max(h, getHeight(intervals, cur));            res.add(h);        }        return res;    }    private int getHeight(List<Interval> intervals, Interval cur) {        int preMaxHeight = 0;  // 注意這裡preMaxHeight會初始化為0,這樣能保證後面的preMaxHeight一定是beneath cur的        for (Interval i : intervals) {  // 從intervas取出的interval都是堆疊高度合并之後的            // Interval i does not intersect with cur            if (i.end < cur.start) continue;            if (i.start > cur.end) continue;            // find the max height beneath cur            preMaxHeight = Math.max(preMaxHeight, i.height);        }        cur.height += preMaxHeight;  // 確定cur的高度,並將這個cur加入到intervas中,注意這個高度是合并之後再加入到interva中的        intervals.add(cur);        return cur.height;    }}

 

LeetCode699. Falling Squares

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.