方法1:暴力
class Solution:
def fallingSquares(self, positions: List[List[int]]) -> List[int]:
n = len(positions); ans = [0] * n
for i, (left0, widen0) in enumerate(positions):
height = widen0
right0 = left0 + widen0
for j in range(i):
left, widen = positions[j]
right = left + widen
if right0 > left and left0 < right:
height = max(height, ans[j] + widen0)
ans[i] = height
for i in range(1, n):
ans[i] = max(ans[i - 1], ans[i])
return ans
- ArrayList 添加add() / 获取get() / 修改set()
class Solution {
public List<Integer> fallingSquares(int[][] positions) {
int n = positions.length;
List<Integer> heights = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
int left0 = positions[i][0];
int right0 = positions[i][0] + positions[i][1];
int height = positions[i][1];
for (int j = 0; j < i; j++) {
int left = positions[j][0];
int right = positions[j][0] + positions[j][1];
if (right0 > left && left0 < right)
height = Math.max(height, heights.get(j) + positions[i][1]);
}
heights.add(height);
}
for (int i = 1; i < n; i++) {
heights.set(i, Math.max(heights.get(i - 1), heights.get(i)));
}
return heights;
}
}
方法2:线段树
class DynamicSegmentTree:
"""动态开点线段树模板 关键在于使用 defaultdict 来存储左右儿子"""
def __init__(self):
self.val = defaultdict(int)
self.add = defaultdict(int)
def update(self, o: int, lson: int, rson: int, L: int, R: int, v: int) -> None:
if L <= lson and rson <= R:
self.do(o, v)
return
self.pushdown(o)
mid = (lson + rson) >> 1
if mid >= L:
self.update(o << 1, lson, mid, L, R, v)
if mid < R:
self.update(o << 1 | 1, mid + 1, rson, L, R, v)
self.pushup(o)
def pushup(self, o: int) -> None:
self.val[o] = max(self.val[o << 1], self.val[o << 1 | 1])
def pushdown(self, o: int) -> None:
v = self.add[o]
if v == 0:
return
self.do(o << 1, v)
self.do(o << 1 | 1, v)
self.add[o] = 0
def do(self, o: int, v: int) -> None:
self.val[o] = max(self.val[o], v)
self.add[o] = max(self.add[o], v)
def query(self, o: int, lson: int, rson: int, L: int, R: int) -> int:
if L <= lson and rson <= R:
return self.val[o]
self.pushdown(o)
res = 0; mid = (lson + rson) >> 1
if mid >= L:
res = self.query(o << 1, lson, mid, L, R)
if mid < R:
res = max(res, self.query(o << 1 | 1, mid + 1, rson, L, R))
return res
class Solution:
def fallingSquares(self, positions: List[List[int]]) -> List[int]:
ans = list(); N = 10 ** 9 + 1; Max = 0
st = DynamicSegmentTree()
for left, widen in positions:
curH = st.query(1, 1, N, left, left + widen - 1)
Max = max(Max, curH + widen); ans.append(Max)
st.update(1, 1, N, left, left + widen - 1, curH + widen)
return ans
class Solution {
class Node {
int val, add;
int lson, rson;
}
int N = 1000_000_000 + 1;
int cnt = 0;
Node[] tree = new Node[1000010];
void update(int o, int lson, int rson, int L, int R, int v) {
if (L <= lson && rson <= R) {
work(o, v);
return ;
}
pushdown(o);
int mid = (lson + rson) >> 1;
if (mid >= L)
update(tree[o].lson, lson, mid, L, R, v);
if (mid < R)
update(tree[o].rson, mid + 1, rson, L, R, v);
pushup(o);
}
void pushup(int o) {
tree[o].val = Math.max(tree[tree[o].lson].val, tree[tree[o].rson].val);
}
void work(int o, int v) {
tree[o].val = Math.max(tree[o].val, v);
tree[o].add = Math.max(tree[o].add, v);
}
void pushdown(int o) {
if (tree[o] == null) tree[o] = new Node();
if (tree[o].lson == 0) {
tree[o].lson = ++cnt;
tree[tree[o].lson] = new Node();
}
if (tree[o].rson == 0) {
tree[o].rson = ++cnt;
tree[tree[o].rson] = new Node();
}
int v = tree[o].add;
if (v == 0) return ;
work(tree[o].lson, v);
work(tree[o].rson, v);
tree[o].add = 0;
}
int query(int o, int lson, int rson, int L, int R) {
if (L <= lson && rson <= R)
return tree[o].val;
pushdown(o);
int mid = (lson + rson) >> 1, res = 0;
if (mid >= L)
res = query(tree[o].lson, lson, mid, L, R);
if (mid < R)
res = Math.max(res, query(tree[o].rson, mid + 1, rson, L, R));
return res;
}
public List<Integer> fallingSquares(int[][] positions) {
List<Integer> ans = new ArrayList<>();
tree[1] = new Node();
for (int[] info : positions) {
int left = info[0], widen = info[1];
int cur = query(1, 1, N, left, left + widen - 1);
update(1, 1, N, left, left + widen - 1, cur + widen);
ans.add(tree[1].val);
}
return ans;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)