当前位置:网站首页>leetcode:面試題 08.13. 堆箱子【自頂而下的dfs + memory or 自底而上的排序 + dp】
leetcode:面試題 08.13. 堆箱子【自頂而下的dfs + memory or 自底而上的排序 + dp】
2022-06-23 16:24:00 【白速龍王的回眸】

分析
這種按要求選取元素的題目往往有兩種思路
分別是自頂而下的dfs + memory 非常優雅
以及自底而上的排序 + dp 也很優雅
dfs + memory
class Solution:
def pileBox(self, box: List[List[int]]) -> int:
# sol(w, d, h) => if we choose(w, d, h), what's our maxHeight
@cache
def dfs(w, d, h):
choices = [(nw, nd, nh) for nw, nd, nh in box if nw < w and nd < d and nh < h]
if not choices: return h # we can not dfs
return h + max([dfs(nw, nd, nh) for nw, nd, nh in choices]) # dfs
# outer entry
return max([dfs(w, d, h) for w, d, h in box])
sort + dp
class Solution:
def pileBox(self, box: List[List[int]]) -> int:
box.sort()
n = len(box)
# dp[i] => box[i] as bottom maxHeight
dp = [box[i][2] for i in range(n)]
# 最長上昇子序列變種
for i in range(n):
for j in range(i):
# check all j before i
if box[j][0] < box[i][0] and box[j][1] < box[i][1] and box[j][2] < box[i][2]:
# if i can add after j
dp[i] = max(dp[i], dp[j] + box[i][2])
#print(i, dp[i])
#print(dp)
return max(dp)
總結
選擇問題
dfs or dp
優雅永不過時
边栏推荐
- stylegan2:analyzing and improving the image quality of stylegan
- Can the hbuilderx light theme be annotated?
- Advanced development - generic entry basic class test
- [tcapulusdb knowledge base] tcapulusdb tmonitor module architecture introduction
- What are the risks of opening a fund account? Is it safe to open an account
- Web容器是怎样给第三方插件做初始化工作的
- stylegan1: a style-based henerator architecture for gemerative adversarial networks
- 机器人方向与高考选专业的一些误区
- XML
- npm install 问题解决(nvm安装与使用)
猜你喜欢

stylegan2:analyzing and improving the image quality of stylegan

leetcode:面试题 08.13. 堆箱子【自顶而下的dfs + memory or 自底而上的排序 + dp】

PageHelper faces the paging problem of complex service data processing

【TcaplusDB知识库】TcaplusDB新增机型介绍

再突破!阿里云进入Gartner云AI开发者服务挑战者象限

万字攻略,详解腾讯面试(T1-T9)核心技术点,面试题整理

Interpreting the 2022 agile coaching industry status report

【解决】npm WARN config global `--global`, `--local` are deprecated. Use `--location=global`

《ThreadLocal》
![[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (II)](/img/bc/35873ebe78db5967b67570cbd9b3ab.png)
[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (II)
随机推荐
2022九峰小学(光谷第二十一小学)生源摸底
Sleuth + Zipkin
Avoid these six difficulties and successfully implement MES system
Image reading: image open(ImgPath)
get_ edges
线上交流丨可信机器学习之机器学习与知识推理相结合(青源Talk第20期 李博)
Tips for accelerating file transfer between windows remote desktop connections
Thinking analysis of binary search method
stylegan2:analyzing and improving the image quality of stylegan
解读2022年度敏捷教练行业现状报告
企业想上MES系统?还得满足这些条件
SaaS cloud tool, a sharp tool for change under the industrial Internet
Use of iscellstr function in MATLAB
腾讯的技术牛人们,是如何完成全面上云这件事儿的?
坚持五件事,带你走出迷茫困境
【TcaplusDB知识库】Tmonitor单机安装指引介绍(一)
Generating binary search balanced tree [using tree recursion]
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)
股票开户如何便宜一些?现在网上开户安全么?
window远程桌面连接互传文件加速小技巧