# LWC 54：699. Falling Squares

## LWC 54：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.

```    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);

max = Math.max(max, h_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);
}

return ans;
}   ```

98 篇文章33 人订阅

0 条评论

## 相关文章

DK1.5 引入了 concurrent package, 提供了更多的concurrent 控制方法。 还提供了一个 ConcurrentHashMap 类...

36412

### Python编程语言入门经典案例

【程序1】 题目：输入一行字符，分别统计出其中英文字母、空格、数字和其它字符的个数。 1#!/usr/bin/python 2#-*- coding:utf-8...

3240

1977

4029

### 正则判断工具类

package com.gulf.utils; import java.text.ParseException; import java.text.Simpl...

23210

1869

2155

1656

### Q101 Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric aroun...

3268

### XStream、JAXB 日期(Date)、数字(Number)格式化输出xml

XStream、Jaxb是java中用于对象xml序列化/反序列化 的经典开源项目，利用它们将对象转换成xml时，经常会遇到日期(Date)、数字按指定格式输出...

2577