当前位置:网站首页>【力扣——动态规划】整理题目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];
}
}
边栏推荐
- Solve the problem that the value of the action attribute of the form is null when transferring parameters
- Training and recognition of handwritten digits through the lenet-5 network built by pytorch
- 我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
- 为什么 Istio 要使用 SPIRE 做身份认证?
- Numpy array: join, flatten, and add dimensions
- OpenHarmony应用开发之二维码生成器
- Generate token
- 理想中的接口自动化项目
- sqlcmd 连接数据库报错
- Fastposter v2.8.4 release e-commerce poster generator
猜你喜欢

Several methods of using ABAP to operate Excel

Day 6 script and animation system

dotnet 使用 Crossgen2 对 DLL 进行 ReadyToRun 提升启动性能

ruoyi集成积木报表(nice)

纵观jBPM从jBPM3到jBPM5以及Activiti

mysql打不开,闪退

Redis sentinel cluster main database failure data recovery ideas # yyds dry goods inventory #

一种跳板机的实现思路
Ribbon core source code analysis

idea连接sql sever失败
随机推荐
关于FTP的协议了解
读取pdf图片并识别内容
Training and recognition of handwritten digits through the lenet-5 network built by pytorch
读取pdf文字和excel写入操作
广州海关支持保障食品、农产品和中药材等民生物资稳定供港
Resolution: overview of decentralized hosting solution
Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
接口自动化框架脚手架-参数化工具的实现
Ffmpeg audio and video recording
idea连接sql sever失败
Missed the golden three silver four, found a job for 4 months, interviewed 15 companies, and finally got 3 offers, ranking P7+
Read PDF Text and write excel operation
物联网5种无线传输协议特点大汇总
Adapter mode
Dbeaver installation and use tutorial (super detailed installation and use tutorial)
Au revoir! Navigateur ie, cette route Edge continue pour IE
Instant messaging and BS architecture simulation of TCP practical cases
JSON数据与List集合之间的正确转换
Restful style
PyGame game: "Changsha version" millionaire started, dare you ask? (multiple game source codes attached)






