LeetCode——Dynamic Planning (1)

The order and ideas for answering questions come from Code Caprice, website address : https://programmercarl.com 

Table of contents

509. Fibonacci Numbers - LeetCode

70. Climbing stairs – LeetCode

746. Climb stairs with minimum cost - LeetCode

62. Different paths - LeetCode

 63. Different Paths II - LeetCode




509. Fibonacci Numbers - LeetCode

The sequence of Fibonacci numbers  (usually  F(n) represented by ) is called  the Fibonacci Sequence  . The sequence  starts 0 with  sum 1 , and each subsequent number is the sum of the previous two numbers. That is:

F(0) = 0, F(1) = 1 
F(n) = F(n - 1) + F(n - 2), where n > 1

Given  n , please calculate  F(n) .

输入:n = 2
输出:1
解释:F(2) = F(1) + F(0) = 1 + 0 = 1
package com.light.code.leetcode.dp;

import java.util.Scanner;

/**
 * @author light
 * @Description 斐波那契数列
 * F(0) = 0,F(1) = 1
 * F(n) = F(n - 1) + F(n - 2),其中 n > 1,给定 n ,请计算 F(n) 。
 * @create 2023-09-13 9:28
 */
public class FibTest {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		int n=input.nextInt();
		System.out.println(fib(n));
	}
	public int fib(int n) {  //动态规划解法
        if(n<=1){
            return n;
        }
        int dp[]=new int[n+1];  //1、确定dp数组含义:第i个数的斐波那契数值为df[i]
        dp[0]=0;  //3、初始化递推公式
        dp[1]=1;
        for(int i=2;i<=n;i++){  //4、遍历顺序
            dp[i]=dp[i-1]+dp[i-2]; //2、确定递推公式
        }
        return dp[n];
        //5、如果有问题打印dp数组
    }
}

70. Climbing stairs – LeetCode

Suppose you are climbing stairs. You need  n stairs to reach the top of the building.

You can climb  one 1 or  2 two steps at a time. How many different ways can you climb to the top of a building?

输入:n = 2
输出:2
解释:有两种方法可以爬到楼顶。
1. 1 阶 + 1 阶
2. 2 阶

import java.util.Scanner;

/**
 * @author light
 * @Description 爬楼梯
 *
 * @create 2023-09-13 9:36
 */
public class ClimbStairsTest {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		int n=input.nextInt();
		System.out.println(climbStairs(n));
	}
	 public int climbStairs(int n) {
        if(n<=2){
            return n;
        }
        //确定dp数组及下标含义 dp[i]:爬到第i阶楼梯有dp[i]种方法
        int[] dp=new int[n+1];
        //确定递推公式--->dp[i]=dp[i-2]+dp[i-1]
        //初始化dp数组 
        dp[1]=1;
        dp[2]=2;
        for(int i=3;i<=n;i++){
            dp[i]=dp[i-1]+dp[i-2];
        }

        return dp[n];

    }
}

746. Climb stairs with minimum cost - LeetCode

You are given an array of integers  cost , which  is  the cost of climbing up the first step of cost[i] the stairs  . iOnce you pay this fee, you have the option of climbing up one or two steps.

 You can choose to start climbing the stairs from the steps marked with  0 or with  .1

Please calculate and return the minimum cost to reach the top of the stairs.

输入:cost = [10,15,20]
输出:15
解释:你将从下标为 1 的台阶开始。
- 支付 15 ,向上爬两个台阶,到达楼梯顶部。
总花费为 15 。

import java.util.Scanner;

/**
 * @author light
 * @Description 最小花费爬楼梯
 *
 * @create 2023-09-13 10:02
 */
public class MinCostClimbingStairsTest {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		int n=input.nextInt();
		int[] num=new int[n];
		for (int i = 0; i < n; i++) {
			num[i]=input.nextInt();
		}
		System.out.println(minCostClimbingStairs(num));
	}
	 public int minCostClimbingStairs(int[] cost) {
         //1 定义dp数组并确认其含义dp[i] 爬到第i阶台阶的最低花费
        int[] dp=new int[cost.length+1];
        //3 初始化dp数组
        dp[0]=0;
        dp[1]=0;
        //4 确认遍历顺序
        for(int i=2;i<=cost.length;i++){
             //2 确认转移矩阵 
            dp[i]=Math.min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
        }
        return dp[cost.length];
    }
}

62. Different paths - LeetCode

A robot is located in  m x n the upper left corner of a grid (the starting point is marked "Start" in the image below).

The robot can only move one step down or to the right at a time. The robot attempts to reach the lower right corner of the grid (labeled "Finish" in the image below).

How many different paths are there in total?

import java.util.Scanner;

/**
 * @author light
 * @Description 不同路径
 * 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。
 *
 * 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。
 *
 * 问总共有多少条不同的路径?
 *
 * @create 2023-09-13 10:33
 */
public class UniquePathsTest {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		int m=input.nextInt();
		int n=input.nextInt();
		System.out.println(uniquePaths(m, n));

	}
	  public int uniquePaths(int m, int n) {
        //确定dp数组及其含义 do[i][j]:到位置为(i,j)的格子有几条路径
        int[][] dp=new int[m][n];
        //初始化dp数组
        for(int i=0;i<m;i++){
            dp[i][0]=1;//列
        }
        for(int j=0;j<n;j++){
            dp[0][j]=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. Different Paths II - LeetCode

A robot is located in  m x n the upper left corner of a grid (the starting point is marked "Start" in the image below).

The robot can only move one step down or to the right at a time. The robot attempts to reach the lower right corner of the grid (labeled "Finish" in the image below).

Now consider that there are obstacles in the grid. So how many different paths will there be from the upper left corner to the lower right corner?

Obstacles and empty locations in the grid are represented by  1 and  respectively 0 .

class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m=obstacleGrid.length;
        int n=obstacleGrid[0].length;
        //确定dp数组及其含义 
        // dp[i][j] :表示从(0 ,0)出发,到(i, j) 有dp[i][j]条不同的路径。
        int[][] dp=new int[m][n];
        //如果在起点或终点出现了障碍,直接返回0
        if(obstacleGrid[0][0]==1||obstacleGrid[m-1][n-1]==1){
            return 0;
        }
        //初始化dp数组
        for(int i=0;i<n&&obstacleGrid[0][i]==0;i++){
            dp[0][i]=1;
        }
        for(int i=0;i<m&&obstacleGrid[i][0]==0;i++){
            dp[i][0]=1;
        }

        //遍历顺序
        for(int i=1;i<m;i++){
            for(int j=1;j<n;j++){
                //确定状态转移方程
                if(obstacleGrid[i][j]==0){
                    dp[i][j]=dp[i-1][j]+dp[i][j-1];
                }
            }
        }
        return dp[m-1][n-1];
    }
}

 

 

Guess you like

Origin blog.csdn.net/zssxcj/article/details/132845442