当前位置:网站首页>2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
2022-07-03 10:06:00 【Ma Nong San Shao_ V】
zero title : Algorithm (leetcode, With mind map + All solutions )300 Title (2312) Selling wood blocks
One Title Description


Two Solution overview ( Mind mapping )

3、 ... and All solutions
interviewer : You're almost ready , Let's start the interview .
Madman Zhang San :okk~
interviewer : If the title is almost read , Say what you think 、 Thinking ha ~
Madman Zhang San : Because in the title , contain “ most ” The word , So I think we should give priority to using “ Dynamic programming ” .
interviewer : What do you think are the conditions for using dynamic programming ?
Madman Zhang San : I personally think , You should have 2 Conditions :
1) Optimal substructure
2) No aftereffect
interviewer : very good , Do you know the essence of dynamic programming and the steps of problem solving ?
Madman Zhang San :
1) The essence : A technology that trades space for time
2) The problem solving steps : branch 3 Step . State definition : Decisions for each state , Store variables for each state ; State transition equation : The transition relationship between the current state and the previous state ; The initial state : Initial state or boundary conditions .
interviewer : Young man , All right . I think you've almost warmed up , Then use the above knowledge to solve this problem ~
Aside : After that 5-10 minute , Zhang San is slow to write the code .
interviewer :( A dignified face 、 confused ) Do you only recite relevant concepts , Haven't you coded relevant topics ?
Madman Zhang San :( Zhang Sanmian leaks timid color ) forehead ....
interviewer : such , You think of the wood block as a big watermelon , Writing code will also be cool and refreshing ~
Then the topic becomes —— Do you have 1 Two dimensional ( The length is w、 Width is h) Big watermelon , You can choose to sell it directly ( If someone has to buy it at this time, the length is w、 Width is h), Otherwise, the big watermelon at this time can only be obtained 0 element
Madman Zhang San : Right , Then we can choose not to sell big watermelons at this time , Horizontal 、 Cut melons vertically , Cut the big watermelon into different small ones constantly , Finally, from these melon cutting schemes, the maximum selling price of the current large watermelon is calculated .
interviewer : Yes , According to what you said before , write down State definition and State transition equation ~
Madman Zhang San : well .
I understand the definition of state —— dp[i][j], The length is i、 Width is j when , The maximum amount of money you can get .
State transition equation —— When cutting melons horizontally :dp[i][j] = max(dp[i][j], dp[k][j] + dp[i - k][j]),k For the range of [1, i - 1].
When cutting melons vertically :dp[i][j] = max(dp[i][j], dp[i][k] + dp[i][j - k]),k For the range of [1, j - 1].
interviewer : What about the initialization of the state ?
Madman Zhang San : According to the array prices , To initialize —— When i、j Exist in prices Inside 0、1 When subscript position ,dp[i][j] = prices[ The corresponding element subscript ][2].
interviewer : very good , Now that the idea has been clarified , Then start your performance , Ah No 、 Start writing your code ~
side : Zhang San was instantly opened up like Ren Du's two veins , Three, five, two , Less than 10 The code was knocked out in minutes ~
1 programme 1
1) Code :
// programme 1 “ Dynamic programming - Normal version ”.
// “ skill : The question stem contains most The word , Give priority to dynamic programming ( The essence : Space for time technology ).”
// Reference resources :
// 1)https://leetcode.cn/problems/selling-pieces-of-wood/solution/mai-mu-tou-kuai-by-leetcode-solution-gflg/
// 2)https://leetcode.cn/problems/selling-pieces-of-wood/solution/by-endlesscheng-mrmd/
// idea ( Think of the wood block here as a big watermelon , Writing code will also be cool and refreshing ~):
// 1) State definition :dp[i][j], The length is i、 Width is j when , The maximum amount of money you can get .
// 2) State shift :
// 2.1) Transverse cut :dp[i][j] = max(dp[i][j], dp[k][j] + dp[i - k][j]),k For the range of [1, i - 1].
// 2.2) Longitudinal cutting :dp[i][j] = max(dp[i][j], dp[i][k] + dp[i][j - k]),k For the range of [1, j - 1].
// Ideas :
// 1.1) State initialization :l = prices.length;
// dp = new Array(m + 1).fill(1).map(v => new Array(n + 1).fill(0));
// reflection : Why do every element of two dimensions go first Default fill 0 ?
// 1.2) State initialization : Traverse Array prices , Further initialization Array dp .
// 2) The core : State shift .
// 2.1) Transverse cut :dp[i][j] = max(dp[i][j], dp[k][j] + dp[i - k][j]),k For the range of [1, i - 1].
// 2.2) Longitudinal cutting :dp[i][j] = max(dp[i][j], dp[i][k] + dp[i][j - k]),k For the range of [1, j - 1].
// 3) Return results dp[m][n] .
var sellingWood = function(m, n, prices) {
// 1.1) State initialization :l = prices.length;
// dp = new Array(m + 1).fill(1).map(v => new Array(n + 1).fill(0));
// reflection : Why do every element of two dimensions go first Default fill 0 ?
const l = prices.length;
let dp = new Array(m + 1).fill(1).map(v => new Array(n + 1).fill(0));
// 1.2) State initialization : Traverse Array prices , Further initialization Array dp .
for (let i = 0; i < l; i++) {
const [width, height, price] = prices[i];
dp[width][height] = price;
}
// 2) The core : State shift .
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
// 2.1) Transverse cut :dp[i][j] = max(dp[i][j], dp[k][j] + dp[i - k][j]),k For the range of [1, i - 1].
for (let k = 1; k < i; k++) {
dp[i][j] = Math.max(dp[i][j], dp[k][j] + dp[i - k][j]);
}
// 2.2) Longitudinal cutting :dp[i][j] = max(dp[i][j], dp[i][k] + dp[i][j - k]),k For the range of [1, j - 1].
for (let k = 1; k < j; k++) {
dp[i][j] = Math.max(dp[i][j], dp[i][k] + dp[i][j - k]);
}
}
}
// 3) Return results dp[m][n] .
return dp[m][n];
};
2 programme 2
interviewer :Good. The code structure is very hierarchical , Notes are also placed in a very appropriate position ~
Madman Zhang San : After all, this “ Two dimensional big watermelon ” It's cooked , I'm sure the algorithm here must be optimal , It can guarantee the highest selling price of our big watermelons .
interviewer : Are you sure you “ Big watermelon cutting algorithm ” Is it cooked ? I don't think so ?
Madman Zhang San : I am a 1 A serious legal person , I can also write for you “ Raw melon algorithm ” no ?
interviewer : I ask you , this “ Big watermelon cutting algorithm ” Is it cooked ?
Madman Zhang San : Just say whether I can pass this interview ~
interviewer :
Madman Zhang San : Then I'm looking at 、 Think about optimizing
Aside : I saw Zhang San write the code running process on the paper .
…
dp[5][5] = max(dp[5][5], dp[1][5] + dp[4][5], dp[2][5] + dp[3][5], dp[3][5] + dp[2][5], dp[2][5] + dp[1][5])
…
Madman Zhang San : It seems that there are some optimization points —— There are a lot of redundant calculations , We subscript k Just enumerate to half the position —— namely k For the range of [1, i / 2( Rounding down )] .
1) Code :
// programme 2 “ Dynamic programming - Optimized version ”.
// “ skill : The question stem contains most The word , Give priority to dynamic programming ( The essence : Space for time technology ).”
// Reference resources :
// 1)https://leetcode.cn/problems/selling-pieces-of-wood/solution/mai-mu-tou-kuai-by-leetcode-solution-gflg/
// 2)https://leetcode.cn/problems/selling-pieces-of-wood/solution/by-endlesscheng-mrmd/
// idea ( Think of the wood block here as a big watermelon , Writing code will also be cool and refreshing ~):
// 1) State definition :dp[i][j], The length is i、 Width is j when , The maximum amount of money you can get .
// 2) State shift :
// 2.1) Transverse cut :dp[i][j] = max(dp[i][j], dp[k][j] + dp[i - k][j]),k For the range of [1, i - 1].
// 2.2) Longitudinal cutting :dp[i][j] = max(dp[i][j], dp[i][k] + dp[i][j - k]),k For the range of [1, j - 1].
// Ideas :
// 1.1) State initialization :l = prices.length;
// dp = new Array(m + 1).fill(1).map(v => new Array(n + 1).fill(0));
// reflection : Why do every element of two dimensions go first Default fill 0 ?
// 1.2) State initialization : Traverse Array prices , Further initialization Array dp .
// 2) The core : State shift ( There is optimization , There is symmetry ,k Enumerate to i、j Of 1 Half position is enough ).
// 2.1) Transverse cut :dp[i][j] = max(dp[i][j], dp[k][j] + dp[i - k][j]),k For the range of [1, i / 2( Rounding down )].
// 2.2) Longitudinal cutting :dp[i][j] = max(dp[i][j], dp[i][k] + dp[i][j - k]),k For the range of [1, j / 2( Rounding down )].
// 3) Return results dp[m][n] .
var sellingWood = function(m, n, prices) {
// 1.1) State initialization :l = prices.length;
// dp = new Array(m + 1).fill(1).map(v => new Array(n + 1).fill(0));
// reflection : Why do every element of two dimensions go first Default fill 0 ?
const l = prices.length;
let dp = new Array(m + 1).fill(1).map(v => new Array(n + 1).fill(0));
// 1.2) State initialization : Traverse Array prices , Further initialization Array dp .
for (let i = 0; i < l; i++) {
const [width, height, price] = prices[i];
dp[width][height] = price;
}
// 2) The core : State shift .
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
// 2.1) Transverse cut :dp[i][j] = max(dp[i][j], dp[k][j] + dp[i - k][j]),k For the range of [1, i / 2( Rounding down )].
for (let k = 1; k <= Math.floor(i / 2); k++) {
dp[i][j] = Math.max(dp[i][j], dp[k][j] + dp[i - k][j]);
}
// 2.2) Longitudinal cutting :dp[i][j] = max(dp[i][j], dp[i][k] + dp[i][j - k]),k For the range of [1, j / 2( Rounding down )].
for (let k = 1; k <= Math.floor(j / 2); k++) {
dp[i][j] = Math.max(dp[i][j], dp[i][k] + dp[i][j - k]);
}
}
}
// 3) Return results dp[m][n] .
return dp[m][n];
};
Aside : Zhang San finished the above code , Hurriedly ask the interviewer .
Madman Zhang San : Passed the interview ?
interviewer :
Madman Zhang San :
also 1 individual offer, Then I will take the post immediately CEO 了 , I should go to eat in the evening Shaxian snacks Well ? still Lanzhou hand-pulled noodles Well ? Ah , Too many choices are also a worry !
Four Resource sharing & more
1 Article history - The overview

2 About bloggers
Ma Nong San Shao , One dedicated to writing The minimalist 、 But complete solutions ( Algorithm ) Bloggers .
Focus on Solve more than one problem 、 Structured thinking , Welcome to brush it together LeetCode ~
边栏推荐
- 2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
- El table X-axis direction (horizontal) scroll bar slides to the right by default
- LeetCode - 919. 完全二叉树插入器 (数组)
- [combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
- Window maximum and minimum settings
- (1) 什么是Lambda表达式
- 2021-11-11 standard thread library
- The underlying principle of vector
- Simulate mouse click
- (1) What is a lambda expression
猜你喜欢

ADS simulation design of class AB RF power amplifier
![[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer](/img/90/4de927e797ec9c2bb70e507392bed0.jpg)
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer

Not many people can finally bring their interests to college graduation

QT self drawing button with bubbles

Leetcode bit operation

Crash工具基本使用及实战分享

Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction

LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
![[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)](/img/19/5dc152b3fadeb56de50768561ad659.jpg)
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)

Swing transformer details-2
随机推荐
Modelcheckpoint auto save model
Emballage automatique et déballage compris? Quel est le principe?
Pymssql controls SQL for Chinese queries
Working mode of 80C51 Serial Port
(1) What is a lambda expression
Screen display of charging pile design -- led driver ta6932
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Opencv histogram equalization
Which language should I choose to program for single chip microcomputer
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
LeetCode - 919. 完全二叉树插入器 (数组)
Tensorflow2.0 save model
4G module designed by charging pile obtains signal strength and quality
03 FastJson 解决循环引用
LeetCode - 706 设计哈希映射(设计) *
Dictionary tree prefix tree trie
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn