当前位置:网站首页>[Li Kou brushing questions] one dimensional dynamic planning record (53 change exchanges, 300 longest increasing subsequence, 53 largest subarray and)
[Li Kou brushing questions] one dimensional dynamic planning record (53 change exchanges, 300 longest increasing subsequence, 53 largest subarray and)
2022-07-06 21:20:00 【Li xiaoenzi】
Catalog
Two 、300. The longest increasing subsequence
3、 ... and 、53. Maximum subarray and
One 、322. Change for
subject
Give you an array of integers coins , Coins of different denominations ; And an integer amount , Represents the total amount .
Calculate and return the amount needed to make up the total amount The minimum number of coins . If no combination of coins can make up the total amount , return -1 . You can think of the number of coins of each kind as infinite .

Their thinking
First of all, make it clear that dp Definition of array : Enter a target amount n, Returns the minimum number of coins to round up the target amount .
【base case】:amount = 0, return 0;
【 state 】: The only target amount ;
【 choice 】: Each coin chosen , It's equivalent to reducing the target amount .
【 State transition equation 】:

Code
The top-down : Search down with memory , Reduce the number of solving subproblems
class Solution {
public int coinChange(int[] coins, int amount) {
// A one-dimensional
// Memory search
//dp The array represents the input target amount amount Return the number of coins with the smallest target amount
int[] dp = new int[amount + 1];
for(int i = 0;i <= amount;i++){
dp[i] = -99;// Initialize a special value casually , Used to determine
}
return coin(coins,dp,amount);
}
public int coin(int[] coins,int[] dp,int amount){
if(amount == 0) return 0;
if(amount < 0) return -1;
// Don't double count , prune
if(dp[amount] != -99) return dp[amount];
int res = Integer.MAX_VALUE;
for(int coin = 0; coin < coins.length;coin++){
// Sub problem
int next = coin(coins,dp,amount-coins[coin]);
// There is no solution to the subproblem
if(next == -1) continue;
// Choose the optimal solution among all subproblems ,+1
res = Math.min(res,next + 1);
}
// Records of the results , Reduce double counting
dp[amount] = (res == Integer.MAX_VALUE) ? -1:res;
return dp[amount];
}
}Bottom up :dp Iterative solution of array
class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
//amount+1 It's equivalent to infinity , Because the number of coins is the largest, that is amount( All for 1 The condition of a coin with pieces )
for(int i = 0;i < amount + 1 ; i++){
dp[i] = amount + 1;
}
dp[0] = 0;
for(int i = 0; i < amount + 1;i++){
// Find the optimal solution of all subproblems +1
for(int coin = 0; coin < coins.length; coin++){
if(i - coins[coin] < 0) continue;
dp[i] = Math.min(dp[i],dp[i - coins[coin]] + 1);
}
}
return (dp[amount] == amount + 1) ? -1 : dp[amount];
}
}Two 、300. The longest increasing subsequence
subject
Give you an array of integers nums , Find the length of the longest strictly increasing subsequence .
Subsequence Is a sequence derived from an array , Delete ( Or do not delete ) Elements in an array without changing the order of the rest . for example ,[3,6,2,7] It's an array [0,3,1,6,2,2,7] The subsequence .

Their thinking
Notice that you need to find subsequences , It's not a subarray , Subarray is continuous , Subsequence is not , You don't have to be continuous . As the example above 1 The subsequence 【2,3,7,101】.
dp Definition of array :dp[i] Said to nums[i] The longest increasing subsequence at the end of this number .
【base case】:dp[i] All values are initialized to 1, Because in order to nums[i] The longest increasing subsequence at the end at least includes itself
【 choice 】: For each of these dp[i], Just find all the smaller elements in front of it dp[j], Again +1, Find the maximum value, that is dp[i].
【 result 】: The maximum of subsequences , yes dp The maximum value in the array .
Code
class Solution {
public int lengthOfLIS(int[] nums) {
// Dynamic programming
// Definition dp[i] Represents the element nums[i] The longest increasing subsequence at the end
// For each of these dp[i], Just find all the smaller elements in front of it dp[j], Again +1, Find the maximum value, that is dp[i]
int[] dp = new int[nums.length];
for(int i = 0; i < nums.length;i++){
dp[i] = 1;// initialization , All elements are 1
}
int res = 1;
for(int i = 0; i < nums.length;i++){
for(int j = 0; j < i; j++){
if(nums[j] < nums[i]){
dp[i] = Math.max(dp[i],dp[j] + 1);
}
}
res = Math.max(res,dp[i]);
}
return res;
}
}3、 ... and 、53. Maximum subarray and
subject
Give you an array of integers nums , Please find a continuous subarray with the largest sum ( A subarray contains at least one element ), Return to its maximum and . Subarray Is a continuous part of the array .

Their thinking
And above 【 The longest increasing subsequence 】 similar , Definition dp Array meaning :dp[i] Said to nums[i] The largest subarray at the end and .
【 Be careful 】: If you define dp[i] The array is nums[0...i] The largest subarray in and , Then you can't find it , because Can't push it out dp[i+1], Subarray must be contiguous , But there is no guarantee dp[i] To dp[i+1] When :nums[0...i] The largest subarray in and nums[i+1] It's adjacent .
【base case】:dp[0] = nums[0]
【 choice 】: Judge the size and make a choice ,① Connect with the previous adjacent elements to form a larger sub array ;② Self as a subarray
【 result 】: The maximum sum of subarrays , yes dp The maximum value in the array .
Code
class Solution {
public int maxSubArray(int[] nums) {
// Dynamic programming
// Definition dp[i] Said to nums[i] The largest subarray at the end and
// There are two options for dynamic transfer , The first is combined with the previous element to form a larger sum , The second is to work in a group of your own
int[] dp = new int[nums.length];
dp[0] = nums[0];
for(int i = 1;i < nums.length;i++){
dp[i] = Math.max(nums[i],dp[i - 1] + nums[i]);
}
int res = Integer.MIN_VALUE;
for(int i = 0 ; i < nums.length;i++){
res = Math.max(res,dp[i]);
}
return res;
}
}In fact, the first thing I think about when I see this topic is sliding window , Because sliding window is most suitable for solving continuous substrings 、 Subarray and other problems , But there are negative numbers in this array element , You can't do it with a sliding window , because The conditions for changing the left and right pointers of the window are not clear .
Topic two 、 Topic 3 belongs to the same kind of dynamic programming problem .
边栏推荐
- Thinking about agile development
- R3live notes: image processing section
- PHP saves session data to MySQL database
- 3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
- 快过年了,心也懒了
- 通过数字电视通过宽带网络取代互联网电视机顶盒应用
- Comprehensive evaluation and recommendation of the most comprehensive knowledge base management tools in the whole network: flowus, baklib, jiandaoyun, ones wiki, pingcode, seed, mebox, Yifang cloud,
- Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
- After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
- R language for text mining Part4 text classification
猜你喜欢

Quick news: the flybook players' conference is held online; Wechat payment launched "education and training service toolbox"

Aiko ai Frontier promotion (7.6)
Why does MySQL index fail? When do I use indexes?

面试官:Redis中有序集合的内部实现方式是什么?

KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning

Swagger UI tutorial API document artifact

LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件

The biggest pain point of traffic management - the resource utilization rate cannot go up

Absolute primes (C language)

MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.
随机推荐
Math symbols in lists
Mtcnn face detection
MySQL - 事务(Transaction)详解
El table table - sortable sorting & disordered sorting when decimal and% appear
js 根据汉字首字母排序(省份排序) 或 根据英文首字母排序——za排序 & az排序
Reinforcement learning - learning notes 5 | alphago
ICML 2022 | flowformer: task generic linear complexity transformer
JS operation DOM element (I) -- six ways to obtain DOM nodes
Vim 基本配置和经常使用的命令
KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
OAI 5g nr+usrp b210 installation and construction
ACdreamoj1110(多重背包)
MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.
Nodejs tutorial let's create your first expressjs application with typescript
Reflection operation exercise
[MySQL] trigger
Aike AI frontier promotion (7.6)
FZU 1686 龙之谜 重复覆盖
3D人脸重建:从基础知识到识别/重建方法!
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性