当前位置:网站首页>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
边栏推荐
- 怎样快速的应对变动的生产管理需求?
- js 对象 使用小技巧
- Web篇_01 了解web開發
- Interpreting the 2022 agile coaching industry status report
- 科大讯飞神经影像疾病预测方案!
- 企业想上MES系统?还得满足这些条件
- How can I get the discount for opening a securities account? Is online account opening safe?
- 【TcaplusDB知识库】Tmonitor后台一键安装介绍(一)
- Image saving: torchvision utils. save_ image(img, imgPath)
- Server deployment and instructions
猜你喜欢

科大讯飞神经影像疾病预测方案!

npm install 问题解决(nvm安装与使用)
![[tcapulusdb knowledge base] tcapulusdb tmonitor module architecture introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb tmonitor module architecture introduction

leetcode:30. 串联所有单词的子串【Counter匹配 + 剪枝】

SSRS页面配置Postgresql data source的方法

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

PageHelper在面对复杂service数据处理下的分页问题

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

二分查找法思路分析

Advanced development stage - the thickening of potential suspension wire begins A small step now, a big step next year
随机推荐
Pytorch: saving and exporting models
Ten thousand words introduction, detailed explanation of the core technical points of Tencent interview (t1-t9), and arrangement of interview questions
Sleuth + Zipkin
Amadis发布OLA支付处理标准
再突破!阿里云进入Gartner云AI开发者服务挑战者象限
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)
将vscode打造无敌的IDE(14) tasks.json和launch.json配置详解,随心所欲添加自动化任务
ABP框架之——数据访问基础架构(下)
Improving efficiency or increasing costs, how should developers understand pair programming?
《ThreadLocal》
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(上)
Object
Object
How to configure PostgreSQL data source on SSRs page
进阶开发- 泛型入门基础类测试
Can the hbuilderx light theme be annotated?
R语言ggplot2可视化水平箱图(Horizontal boxplot with coord_flip)、并添加抖动数据点显示分布情况(jittered points)
【TcaplusDB知识库】Tmonitor系统升级介绍
How can I get the discount for opening a securities account? Is online account opening safe?
Apple iPhone, Samsung mobile phone and other electronic products began to enter Russia through parallel import channels