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

    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;
        }
    }
  • 相关阅读:
    Accoridion折叠面板
    mui列表系列
    按照中文首字母排序查询表数据
    五分位算法
    springmvc添加拦截器
    springmvc添加定时任务
    通过后台解决跨域调用接口问题
    eclipse搭建ssm框架
    Java 将图片转成base64,传到前台展示
    用mysql存储过程代替递归查询
  • 原文地址:https://www.cnblogs.com/f91og/p/9742274.html
Copyright © 2020-2023  润新知