在無限長的數軸(即 x 軸)上,咱們根據給定的順序放置對應的正方形方塊。java
第 i 個掉落的方塊(positions[i] = (left, side_length))是正方形,其中 left 表示該方塊最左邊的點位置(positions[i][0]),side_length 表示該方塊的邊長(positions[i][1])。ide
每一個方塊的底部邊緣平行於數軸(即 x 軸),而且從一個比目前全部的落地方塊更高的高度掉落而下。在上一個方塊結束掉落,並保持靜止後,纔開始掉落新方塊。this
方塊的底邊具備很是大的粘性,並將保持固定在它們所接觸的任何長度表面上(不管是數軸仍是其餘方塊)。鄰接掉落的邊不會過早地粘合在一塊兒,由於只有底邊才具備粘性。spa
返回一個堆疊高度列表 ans 。每個堆疊高度 ans[i] 表示在經過 positions[0], positions[1], …, positions[i] 表示的方塊掉落結束後,目前全部已經落穩的方塊堆疊的最高高度。code
示例 1:it
輸入: [[1, 2], [2, 3], [6, 1]] 輸出: [2, 5, 5] 解釋: 第一個方塊 positions[0] = [1, 2] 掉落: _aa _aa ------- 方塊最大高度爲 2 。 第二個方塊 positions[1] = [2, 3] 掉落: __aaa __aaa __aaa _aa__ _aa__ -------------- 方塊最大高度爲5。 大的方塊保持在較小的方塊的頂部,不論它的重心在哪裏,由於方塊的底部邊緣有很是大的粘性。 第三個方塊 positions[1] = [6, 1] 掉落: __aaa __aaa __aaa _aa _aa___a -------------- 方塊最大高度爲5。 所以,咱們返回結果[2, 5, 5]。
示例 2:io
輸入: [[100, 100], [200, 100]] 輸出: [100, 100] 解釋: 相鄰的方塊不會過早地卡住,只有它們的底部邊緣才能粘在表面上。
注意:class
1 <= positions.length <= 1000.
1 <= positions[i][0] <= 10^8.
1 <= positions[i][1] <= 10^6.import
PS:
按照左端點放進樹List
import java.util.*; class Solution { // 描述方塊以及高度 private class Node { int l, r, h, maxR; Node left, right; public Node(int l, int r, int h, int maxR) { this.l = l; this.r = r; this.h = h; this.maxR = maxR; this.left = null; this.right = null; } } // public List<Integer> fallingSquares(int[][] positions) { // 建立返回值 List<Integer> res = new ArrayList<>(); // 根節點,默認爲零 Node root = null; // 目前最高的高度 int maxH = 0; for (int[] position : positions) { int l = position[0]; // 左橫座標 int r = position[0] + position[1]; // 右橫座標 int e = position[1]; // 邊長 int curH = query(root, l, r); // 目前區間的最高的高度 root = insert(root, l, r, curH + e); maxH = Math.max(maxH, curH + e); res.add(maxH); } return res; } private Node insert(Node root, int l, int r, int h) { if (root == null) return new Node(l, r, h, r); if (l <= root.l) root.left = insert(root.left, l, r, h); else root.right = insert(root.right, l, r, h); // 最終目標是僅僅須要根節點更新 maxR root.maxR = Math.max(r, root.maxR); return root; // 返回根節點 } private int query(Node root, int l, int r) { // 新節點的左邊界大於等於目前的maxR的話,直接獲得0,不須要遍歷了 if (root == null || l >= root.maxR) return 0; // 高度 int curH = 0; if (!(r <= root.l || root.r <= l)) // 是否跟這個節點相交 curH = root.h; // 剪枝 curH = Math.max(curH, query(root.left, l, r)); if (r > root.l) curH = Math.max(curH, query(root.right, l, r)); return curH; } }
在無限長的數軸(即 x 軸)上,咱們根據給定的順序放置對應的正方形方塊。
第 i 個掉落的方塊(positions[i] = (left, side_length))是正方形,其中 left 表示該方塊最左邊的點位置(positions[i][0]),side_length 表示該方塊的邊長(positions[i][1])。
每一個方塊的底部邊緣平行於數軸(即 x 軸),而且從一個比目前全部的落地方塊更高的高度掉落而下。在上一個方塊結束掉落,並保持靜止後,纔開始掉落新方塊。
方塊的底邊具備很是大的粘性,並將保持固定在它們所接觸的任何長度表面上(不管是數軸仍是其餘方塊)。鄰接掉落的邊不會過早地粘合在一塊兒,由於只有底邊才具備粘性。
返回一個堆疊高度列表 ans 。每個堆疊高度 ans[i] 表示在經過 positions[0], positions[1], …, positions[i] 表示的方塊掉落結束後,目前全部已經落穩的方塊堆疊的最高高度。
示例 1:
輸入: [[1, 2], [2, 3], [6, 1]] 輸出: [2, 5, 5] 解釋: 第一個方塊 positions[0] = [1, 2] 掉落: _aa _aa ------- 方塊最大高度爲 2 。 第二個方塊 positions[1] = [2, 3] 掉落: __aaa __aaa __aaa _aa__ _aa__ -------------- 方塊最大高度爲5。 大的方塊保持在較小的方塊的頂部,不論它的重心在哪裏,由於方塊的底部邊緣有很是大的粘性。 第三個方塊 positions[1] = [6, 1] 掉落: __aaa __aaa __aaa _aa _aa___a -------------- 方塊最大高度爲5。 所以,咱們返回結果[2, 5, 5]。
示例 2:
輸入: [[100, 100], [200, 100]] 輸出: [100, 100] 解釋: 相鄰的方塊不會過早地卡住,只有它們的底部邊緣才能粘在表面上。
注意:
1 <= positions.length <= 1000.
1 <= positions[i][0] <= 10^8.
1 <= positions[i][1] <= 10^6.
PS:
按照左端點放進樹
import java.util.*; class Solution { // 描述方塊以及高度 private class Node { int l, r, h, maxR; Node left, right; public Node(int l, int r, int h, int maxR) { this.l = l; this.r = r; this.h = h; this.maxR = maxR; this.left = null; this.right = null; } } // public List<Integer> fallingSquares(int[][] positions) { // 建立返回值 List<Integer> res = new ArrayList<>(); // 根節點,默認爲零 Node root = null; // 目前最高的高度 int maxH = 0; for (int[] position : positions) { int l = position[0]; // 左橫座標 int r = position[0] + position[1]; // 右橫座標 int e = position[1]; // 邊長 int curH = query(root, l, r); // 目前區間的最高的高度 root = insert(root, l, r, curH + e); maxH = Math.max(maxH, curH + e); res.add(maxH); } return res; } private Node insert(Node root, int l, int r, int h) { if (root == null) return new Node(l, r, h, r); if (l <= root.l) root.left = insert(root.left, l, r, h); else root.right = insert(root.right, l, r, h); // 最終目標是僅僅須要根節點更新 maxR root.maxR = Math.max(r, root.maxR); return root; // 返回根節點 } private int query(Node root, int l, int r) { // 新節點的左邊界大於等於目前的maxR的話,直接獲得0,不須要遍歷了 if (root == null || l >= root.maxR) return 0; // 高度 int curH = 0; if (!(r <= root.l || root.r <= l)) // 是否跟這個節點相交 curH = root.h; // 剪枝 curH = Math.max(curH, query(root.left, l, r)); if (r > root.l) curH = Math.max(curH, query(root.right, l, r)); return curH; } }