LeetCode699. Falling Squares

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].ide

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.this

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.spa

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].code

Example 1:blog

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.rem

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.get

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].it

Example 2:io

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; } }
相關文章
相關標籤/搜索