当前位置:网站首页>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
边栏推荐
- Image saving: torchvision utils. save_ image(img, imgPath)
- 阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一文搞定
- R语言使用colorblinr包模拟色盲视觉、将已有的ggplot2可视化结果图像使用edit_colors函数编辑转化为色盲视觉友好的可视化结果、并自定设置色盲形式、色盲严重级别
- leetcode:30. Concatenate substrings of all words [counter matching + pruning]
- How does the web container initialize third-party plug-ins
- 企业想上MES系统?还得满足这些条件
- golang冒泡排序代码实现
- 总结一下购买阿里云服务器的经验
- Spin lock using CAS
- 多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中
猜你喜欢

Ten thousand words introduction, detailed explanation of the core technical points of Tencent interview (t1-t9), and arrangement of interview questions

XML

If no code is moved, the project access speed drops significantly the next day. Case analysis

线上交流丨可信机器学习之机器学习与知识推理相结合(青源Talk第20期 李博)

聚焦:ZK-SNARK 技术

2022九峰小学(光谷第二十一小学)生源摸底

Golang对JSON文件的写操作

Thinking analysis of binary search method

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

golang二分查找法代码实现
随机推荐
Readimg: read picture to variable variable variable
Code implementation of golang binary search method
Build vscode into an invincible IDE (14) tasks JSON and launch JSON configuration details, add automation tasks at will
规避这六大难点,成功实施MES系统
CA authentication and issuance of revocation certificates
Object
数组自带的方法
R语言使用yardstick包的rmse函数评估回归模型的性能、评估回归模型在每个交叉验证(或者重采样)的每一折fold上的RMSE、以及整体的均值RMSE(其他指标mae、mape等计算方式类似)
TCP protocol notes
ADB 按鍵名、按鍵代碼數字、按鍵說明對照錶
513. Find Bottom Left Tree Value
Amadis publishes Ola payment processing standards
将vscode打造无敌的IDE(14) tasks.json和launch.json配置详解,随心所欲添加自动化任务
Apache commons tool class
How NPM contracts and deletes packages
SaaS cloud tool, a sharp tool for change under the industrial Internet
解读2022年度敏捷教练行业现状报告
怎样快速的应对变动的生产管理需求?
Block, non block, multiplexing, synchronous, asynchronous, bio, NiO, AIO
R语言ggplot2可视化水平箱图(Horizontal boxplot with coord_flip)、并添加抖动数据点显示分布情况(jittered points)