当前位置:网站首页>【力扣——动态规划】整理题目1:基础题目:509、70、746、62、63、343、96(附链接、题目描述、解题方法及代码)
【力扣——动态规划】整理题目1:基础题目:509、70、746、62、63、343、96(附链接、题目描述、解题方法及代码)
2022-06-28 10:11:00 【-Blue.】
如果对你有帮助的话
为博主点个赞吧
点赞是对博主最大的鼓励
爱心发射~
目录
基础知识

- 动态规划是由前一个状态推导出来的
- 而贪心算法是局部直接选最优的
解题步骤
- 确定dp数组(dp table)以及下标的含义
- 确定递推公式
- dp数组如何初始化
- 确定遍历顺序
- 举例推导dp数组
解题步骤-简洁
- 确定dp数组下标含义
- 递推公式
- 初始化
- 遍历顺序
- 推导结果
509. 斐波那契数


动态规划
class Solution {
public int fib(int n) {
/* 动态规划: 1. 确定dp数组(dp table)以及下标的含义 - 第i个斐波那契数 的值 2. 确定递推公式 - F(n) = F(n - 1) + F(n - 2) 3. dp数组如何初始化 - F(0) = 0,F(1) = 1 4. 确定遍历顺序 - 从前到后 5. 举例推导dp数组 - 0 1 1 2 3 5 8 13 21 34 55 */
if(n < 2) return n;
int a=0, b=1, c=0;
for(int i=1; i<n; ++i){
c = a + b;
a = b;
b = c;
}
return c;
}
}
70. 爬楼梯
动态规划
class Solution {
public int climbStairs(int n) {
/* 动态规划: 1. 确定dp数组(dp table)以及下标的含义 - 爬第n个台阶,有dp[n]种方法 2. 确定递推公式 - dp[n] = dp[n-1] + dp[n-2] 3. dp数组如何初始化 - dp[1]=1 dp[2]=2 4. 确定遍历顺序 - 从前向后 5. 举例推导dp数组 - 1、2、3、5、8 */
if(n<=2) return n;
int a=1, b=2,c=0;
for(int i=2; i<n; ++i){
c = a + b;
a = b;
b = c;
}
return c;
}
}
746. 使用最小花费爬楼梯
动态规划
class Solution {
public int minCostClimbingStairs(int[] cost) {
/* 1. 确定dp数组(dp table)以及下标的含义 - 到第i个台阶需要的最少体力 2. 确定递推公式 - dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]; 3. dp数组如何初始化 - dp[0] = cost[0], dp[1] = cost[1] 4. 确定遍历顺序 - 从前向后 5. 举例推导dp数组 */
if(cost == null || cost.length == 0) return 0;
if(cost.length == 1) return cost[0];
int[] dp = new int[cost.length];
dp[0] = cost[0];
dp[1] = cost[1];
for(int i=2; i<cost.length; ++i){
dp[i] = Math.min(dp[i-1], dp[i-2]) + cost[i];
}
return Math.min(dp[cost.length-1], dp[cost.length -2]);
}
}
62. 不同路径
动态规划
class Solution {
public int uniquePaths(int m, int n) {
/* 1. 确定dp数组下标含义—— dp[i][j] 到每一个坐标可能的路径种类 2. 递推公式—— dp[i][j] = dp[i-1][j]+ dp[i][j-1] 3. 初始化—— dp[i][0]=1 dp[0][i]=1 初始化横竖就可 4. 遍历顺序—— 一行一行遍历 5. 推导结果 */
int[][] dp = new int[m][n];
for(int i=0; i<m; ++i){
dp[i][0] = 1;
}
for(int i=0; i<n; ++i){
dp[0][i] = 1;
}
for(int i=1; i<m; ++i){
for(int j=1; j<n; ++j){
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}
}
63. 不同路径 II



动态规划
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
/* 1. 确定dp数组下标含义—— 到dp[i][j]有几种路径 2. 递推公式 —— dp[i][j] = dp[i][j-1] + dp[i-1][j] 3. 初始化 —— dp[][] 循环初始化,遇见障碍物,跳出,后边都为0 4. 遍历顺序 —— 从前到后,一层一层 5. 推导结果 */
int m = obstacleGrid.length, n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
for(int i=0; i<m && obstacleGrid[i][0]==0; ++i){
dp[i][0] = 1;
}
for(int i=0; i<n && obstacleGrid[0][i]==0; ++i){
dp[0][i] = 1;
}
for(int i=1; i<m; ++i){
for(int j=1; j<n; ++j){
if(obstacleGrid[i][j]==1){
continue;
}
dp[i][j] = dp[i][j-1] + dp[i-1][j];
}
}
return dp[m-1][n-1];
}
}
343. 整数拆分

动态规划
class Solution {
public int integerBreak(int n) {
/* 1. 确定dp数组下标含义—— 数字i得到的最大乘积dp[i] 2. 递推公式 —— dp[i] = max({dp[i], (i - j) * j, dp[i - j] * j}); 3. 初始化 —— dp[2]=1 , dp[3]=2 4. 遍历顺序 —— 从前到后 外边i从3开始。就从1 5. 推导结果 */
int[] dp = new int[n+1];
dp[2] = 1;
for(int i=3; i<=n; ++i){
for(int j=1; j<=i-j; ++j){
dp[i] = Math.max(dp[i], Math.max(j*(i-j), j*dp[i-j]));
}
}
return dp[n];
}
}
96. 不同的二叉搜索树
卡特兰数
class Solution {
public int numTrees(int n) {
// 卡特兰数
int[] dp = new int[n+1];
dp[0] = 1;
dp[1] = 1;
for(int i=2; i<=n; ++i){
for(int j=1; j<=i; ++j){
dp[i] += dp[j-1] * dp[i -j];
}
}
return dp[n];
}
}
边栏推荐
- Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
- 请教下, 我在本地idea运行flinkcdc的mysql到mysql全量同步,这个是在我本地ide
- Restful style
- [Unity][ECS]学习笔记(二)
- 标识符的命名规则和规范
- BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比
- Missed the golden three silver four, found a job for 4 months, interviewed 15 companies, and finally got 3 offers, ranking P7+
- 【云驻共创】DWS告警服务DMS详细介绍和集群连接方式简介
- June training (day 28) - Dynamic Planning
- Sqlcmd database connection error
猜你喜欢

Matplotlib属性及注解

How to use dataant to monitor Apache apisex

Dotnet uses crossgen2 to readytorun DLL to improve startup performance

dotnet 使用 Crossgen2 对 DLL 进行 ReadyToRun 提升启动性能
![[Unity]EBUSY: resource busy or locked](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[Unity]EBUSY: resource busy or locked

第六天 脚本与动画系统

To enhance the function of jupyter notebook, here are four tips

Several methods of using ABAP to operate Excel

使用 ABAP 操作 Excel 的几种方法

etf持仓如何影响现货金价?
随机推荐
Unity loads AssetBundle resources from the server and writes them to local memory, and loads the downloaded and saved AB resources from local memory to the scene
广州海关支持保障食品、农产品和中药材等民生物资稳定供港
Install using snap in opencloudos NET 6
Proxy mode (proxy)
[Unity]EBUSY: resource busy or locked
Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
[Unity][ECS]学习笔记(二)
无线通信模块定点传输-点对多点的具体传输应用
Fastposter v2.8.4 release e-commerce poster generator
Interface automation framework scaffolding - Implementation of parametric tools
Looking at jBPM from jbm3 to jbm5 and activiti
Settings of gift giving module and other custom controls in one-to-one video chat system code
满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
Huawei OSPF single region
第三章 栈和队列
接口自动化框架脚手架-利用反射机制实现接口统一发起端
Sword finger offer | Fibonacci sequence
[unity][ecs] learning notes (I)
Generate token
JSON数据与List集合之间的正确转换






