当前位置:网站首页>Leetcode: interview question 08.13 Stacking bin [top-down DFS + memory or bottom-up sorting + DP]
Leetcode: interview question 08.13 Stacking bin [top-down DFS + memory or bottom-up sorting + DP]
2022-06-23 16:24:00 【Review of the white speed Dragon King】

analysis
There are often two ways to choose elements according to requirements
From the top down dfs + memory Very elegant
And bottom-up sorting + dp Also is very elegant
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)]
# Longest ascending subsequence variant
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)
summary
Choice problem
dfs or dp
Elegance never goes out of style
边栏推荐
- npm 如何发包 删包
- Block, non block, multiplexing, synchronous, asynchronous, bio, NiO, AIO
- Apache commons tool class
- Pytorch: saving and exporting models
- 企业想上MES系统?还得满足这些条件
- SaaS 云工具,产业互联网下的变革利器
- 多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中
- Matlab: how to know from some data which data are added to get a known number
- 如何让销售管理更高效?
- golang二分查找法代码实现
猜你喜欢
Drag the child file to the upper level

Block, non block, multiplexing, synchronous, asynchronous, bio, NiO, AIO
![[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)](/img/6d/8b1ac734cd95fb29e576aa3eee1b33.png)
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)

【TcaplusDB知识库】TcaplusDB Tmonitor模块架构介绍

Interpreting the 2022 agile coaching industry status report

Matlab: how to know from some data which data are added to get a known number

SSRS页面配置Postgresql data source的方法
![Generating binary search balanced tree [using tree recursion]](/img/b3/f8edf45bdfdced7c3698088dbf7d84.png)
Generating binary search balanced tree [using tree recursion]

uniapp对接腾讯即时通讯TIM 发图片消息问题

Uniapp sends picture messages to Tencent instant messaging Tim
随机推荐
Spin lock using CAS
VGg download (.Net file and imagenet-vgg-verydeep-19)
《ThreadLocal》
Solution: in the verification phase, the first batch does not report errors, and the second batch reports CUDA exceeded errors
Now I want to buy stocks. How do I open an account? Is it safe to open a mobile account?
Image saving: torchvision utils. save_ image(img, imgPath)
Advanced development - generic entry basic class test
进阶开发阶段-势若悬丝的加粗开始. 现在的一小步,明年的一大步
Image reading: image open(ImgPath)
创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋
js中 if 直接判断 数据类型 结果举例
提高效率 Or 增加成本,开发人员应如何理解结对编程?
【解决】npm WARN config global `--global`, `--local` are deprecated. Use `--location=global`
How can I get the discount for opening a securities account? Is online account opening safe?
Openresty Foundation
机器人方向与高考选专业的一些误区
《ThreadLocal》
Avoid these six difficulties and successfully implement MES system
How does the web container initialize third-party plug-ins
Uniapp sends picture messages to Tencent instant messaging Tim