LeetCode699. Falling Squares

Source: Internet
Author: User

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

The i -th Square dropped ( positions[i] = (left, side_length) ) is a square with the left-most point being and positions[i][0] 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 is infinitely sticky on their bottom edge, and would remain fixed to any positive length surface they touch (E Ither the number line or another square). Squares dropped adjacent to all other would not stick together prematurely.

Return a list of ans heights. Each height ans[i] represents the current highest height of any square we had 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 the maximum, the height of any positions[0] = [1, 2]: _aa _aa ------- square is 2.

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

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

Example 2:

Input: [[+], [+], [100]]output: [+], 100]explanation:adjacent squares don ' t get stuck prematurely-only their bo Ttom edge can stick to surfaces.

Analysis

First of all, to clarify the meaning of the topic, on the x-axis down the box, with the top left of the box of the vertex positions[i][0] and the length of the block positions[i][1] to determine the location of each block, then the height of the block is what? Check, the original squre is square meaning, then the height of each block will know. The chests are thrown from an infinite height to the x-axis, and chests with overlapping edges are stacked (the boundary does not intersect), and the maximum stacking heights after each box throw are queried.

Isn't it a bit like Tetris? The solution is to use the previous algorithm encountered by the interval to represent these blocks, the initial assumption that all the blocks fell to the ground and not stacked, for each block we go to iterate all the squares before it, check whether there is a block should be under the current block, If the current Interva has a intersection with the previous interval then it means that the current block should be above the block. Our goal is to find the highest square and place the current block cur above the previous block I, and set the height of the block cur to

cur.height = cur.height + previousMaxHeight;

It is important to be clear that the cur.height here always records the maximum height of the stack on the entire x-axis after dropping the current cur block.previousMaxHeight记录的是在的当前方块cur之下的堆叠到的最大高度。

Still some not very clear, or the code to specific analysis

classSolution {Private classInterval {intstart, end, height;  PublicInterval (intStartintEndintheight) {             This. Start =start;  This. end =end;  This. Height =height; }    }     PublicList<integer> Fallingsquares (int[] positions) {List<Interval> intervals =NewArraylist<>(); List<Integer> res =NewArraylist<>(); intH = 0;  for(int[] pos:positions) {Interval cur=NewInterval (Pos[0], pos[0] + pos[1]-1, pos[1]); H=Math.max (H, getheight (intervals, cur));        Res.add (h); }        returnRes; }    Private intGetHeight (list<interval>intervals, Interval cur) {        intPremaxheight = 0; //Note here Premaxheight will be initialized to 0, this will ensure that the back of the premaxheight must be beneath cur  for(Interval i:intervals) { //Interval removed from Intervas is a combination of Stack heights //Interval I does not intersect with cur            if(I.end < Cur.start)Continue; if(I.start > Cur.end)Continue; //find the max height beneath curPremaxheight =Math.max (Premaxheight, i.height); } cur.height+=Premaxheight;        //Determine the height of the cur and add the cur to the Intervas, noting that the height is the Intervals.add (cur) that was added to Interva after merging ; returnCur.height; }}

LeetCode699. Falling Squares

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.