LWC 54:699. Falling Squares

LWC 54:699. Falling Squares

传送门:699. Falling Squares

Problem:

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 positionsi. 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], positions1, …, 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 positions1 = [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 positions1 = [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.

Note:

1 <= positions.length <= 1000.

1 <= positions0 <= 10^8.

1 <= positions1 <= 10^6.

思路: 有点几何题的味道,实际上正方形往上叠总共就5种情况,如下:

前四种情况,可以合并成一种情况处理,只需要搜索蓝色两条边界内是否存在对应的边,如果有,则说明需要叠加。第五种情况需要特殊处理,直接扫描所有正方形,包含蓝色矩形块则更新高度。

代码如下:

    class Edge{
        int x;
        int y;
        int h;

        Edge(int x, int y, int h){
            this.x = x;
            this.y = y;
            this.h = h;
        }
    }

    public List<Integer> fallingSquares(int[][] positions) {
        int n = positions.length;
        List<Integer> ans = new ArrayList<>();

        TreeMap<Double, Integer> map = new TreeMap<>();
        List<Edge> heights = new ArrayList<Edge>();
        int max = 0;

        for (int i = 0; i < n; ++i) {
            int x = positions[i][0];
            int h = positions[i][1];
            int y = h + x - 1;


            int h_max = 0;
            for (Double e : map.subMap(x - 0.1, y + 0.1).keySet()) {
                h_max = Math.max(h_max, map.get(e));
            }

            for (Edge edge : heights) {
                if (edge.x <= x && edge.y >= y) {
                    h_max = Math.max(h_max, edge.h);
                }
            }

            h_max += h;

            map.put(x * 1.0, h_max);
            map.put(y * 1.0, h_max);
            heights.add(new Edge(x, y, h_max));

            max = Math.max(max, h_max);
            ans.add(max);
        }

        return ans;
    }       

其实吧,上述五种情况也能合并在一块,比较取巧,先把对应的所有边都枚举出来,之后不断在其基础上累加高度即可。造房子的感觉!

代码如下:

        public List<Integer> fallingSquares(int[][] positions) {
            int n = positions.length;
            List<Integer> ans = new ArrayList<>();

            TreeMap<Double, Integer> map = new TreeMap<>();
            for (int[] pos : positions) {
                int x = pos[0];
                int h = pos[1];
                int y = x + h - 1;
                map.put(x * 1.0, 0);
                map.put(y * 1.0, 0);
            }
            int max = 0;

            for (int i = 0; i < n; ++i) {
                int x = positions[i][0];
                int h = positions[i][1];
                int y = h + x - 1;


                int h_max = 0;
                for (Double e : map.subMap(x - 0.1, y + 0.1).keySet()) {
                    h_max = Math.max(h_max, map.get(e));
                }

                h_max += h;

                for (Double e : map.subMap(x - 0.1, y + 0.1).keySet()) {
                    map.put(e, h_max);
                }

                max = Math.max(max, h_max);
                ans.add(max);
            }

            return ans;
        }   

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏计算机视觉与深度学习基础

Leetcode 45 Jump Game II

Given an array of non-negative integers, you are initially positioned at the fi...

1907
来自专栏计算机视觉与深度学习基础

Leetcode 284. Peeking Iterator

Given an Iterator class interface with methods: next() and hasNext(), design a...

2783
来自专栏大闲人柴毛毛

贪心算法(五)——迪杰斯特拉算法

问题描述 给一个有向无环带权图,并给一个起点,求出该原点到所有顶点的最短路径。 ? 数据结构 dis: Map<String,Integer> dis; ...

3489
来自专栏聊聊技术

原 数据结构-散列表(Hash Table

3349
来自专栏算法修养

HOJ 2985 Wavio Sequence(最长递增子序列以及其O(n*logn)算法)

Wavio Sequence My Tags (Edit) Source : UVA Time limit : 1 sec Memor...

2496
来自专栏技术小黑屋

Google IO:Android内存管理主题演讲记录

翻出了3年前的Google IO大会的主题演讲 Google IO 2011 Memory management for Android Apps,该演讲介绍...

711
来自专栏ml

uva----11729 Commando war (突击战争)

G Commando War Input: Standard Input Output: Standard Output “Wa...

2767
来自专栏尾尾部落

[剑指offer] 从上往下打印二叉树

就是二叉树的层序遍历。借助一个队列就可以实现。 使用两个队列一个存放节点,一个存放值。先将根节点加入到队列中,然后遍历队列中的元素,遍历过程中,访问该元素的左...

561
来自专栏Android知识点总结

05-图解数据结构之队列--Queue

844
来自专栏Jack-Cui

225. Implement Stack using Queues(Stack-Easy)

Implement the following operations of a stack using queues. push(x) – Push eleme...

18810

扫码关注云+社区