当前位置:网站首页>[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 .
边栏推荐
- 对话阿里巴巴副总裁贾扬清:追求大模型,并不是一件坏事
- Seven original sins of embedded development
- Manifest of SAP ui5 framework json
- Summary of cross partition scheme
- Is it profitable to host an Olympic Games?
- The difference between break and continue in the for loop -- break completely end the loop & continue terminate this loop
- WEB功能测试说明
- Deployment of external server area and dual machine hot standby of firewall Foundation
- El table table - sortable sorting & disordered sorting when decimal and% appear
- The use method of string is startwith () - start with XX, endswith () - end with XX, trim () - delete spaces at both ends
猜你喜欢
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
None of the strongest kings in the monitoring industry!
Swagger UI教程 API 文档神器
Seven original sins of embedded development
面试官:Redis中有序集合的内部实现方式是什么?
[sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics
After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
Is this the feeling of being spoiled by bytes?
每个程序员必须掌握的常用英语词汇(建议收藏)
2017 8th Blue Bridge Cup group a provincial tournament
随机推荐
技术分享 | 抓包分析 TCP 协议
Math symbols in lists
SDL2来源分析7:演出(SDL_RenderPresent())
Nodejs教程之让我们用 typescript 创建你的第一个 expressjs 应用程序
R language for text mining Part4 text classification
After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
OSPF多区域配置
SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
Ravendb starts -- document metadata
What are RDB and AOF
Absolute primes (C language)
Start the embedded room: system startup with limited resources
3D人脸重建:从基础知识到识别/重建方法!
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
MySQL - 事务(Transaction)详解
js通过数组内容来获取数组下标
Quick news: the flybook players' conference is held online; Wechat payment launched "education and training service toolbox"
js中,字符串和数组互转(一)——字符串转为数组的方法
WEB功能测试说明