当前位置:网站首页>[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 .
边栏推荐
- js中,字符串和数组互转(一)——字符串转为数组的方法
- R语言可视化两个以上的分类(类别)变量之间的关系、使用vcd包中的Mosaic函数创建马赛克图( Mosaic plots)、分别可视化两个、三个、四个分类变量的关系的马赛克图
- el-table表格——sortable排序 & 出现小数、%时排序错乱
- for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环
- js 根据汉字首字母排序(省份排序) 或 根据英文首字母排序——za排序 & az排序
- js通过数组内容来获取数组下标
- js之遍历数组、字符串
- Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
- el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort
- Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
猜你喜欢
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
The difference between break and continue in the for loop -- break completely end the loop & continue terminate this loop
[in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
Data Lake (VIII): Iceberg data storage format
OneNote in-depth evaluation: using resources, plug-ins, templates
【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
Opencv learning example code 3.2.3 image binarization
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
[MySQL] trigger
【滑动窗口】第九届蓝桥杯省赛B组:日志统计
随机推荐
14年本科毕业,转行软件测试,薪资13.5K
c#使用oracle存储过程获取结果集实例
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
Aike AI frontier promotion (7.6)
爱可可AI前沿推介(7.6)
Is this the feeling of being spoiled by bytes?
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
VIM basic configuration and frequently used commands
@Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)
PG basics -- Logical Structure Management (transaction)
[in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
OSPF多区域配置
ICML 2022 | flowformer: task generic linear complexity transformer
Pat 1078 hashing (25 points) ⼆ times ⽅ exploration method
R language visualizes the relationship between more than two classification (category) variables, uses mosaic function in VCD package to create mosaic plots, and visualizes the relationship between tw
Nodejs tutorial let's create your first expressjs application with typescript
Thinking about agile development
Regular expression collection
2017 8th Blue Bridge Cup group a provincial tournament
Divide candy