算法问题——(动态规划问题)

一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。问总共有多少条不同的路径?

算法问题——(动态规划问题)_第1张图片

/**
 * Copyright (C), 2018-2020
 * FileName: uniquePaths62
 * Author:   xjl
 * Date:     2020/7/7 8:34
 * Description: 62. 不同路径
 */
package DP;

import org.junit.Test;

public class uniquePaths62 {

    public int uniquePaths(int m, int n) {
        //d[i][j]: start->(i,j) 一共有多少个uniqueunique paths
        //d[i][j]=dp[i-1][j]+d[i][j-1]
        //d[0][0]=1

        int[][] dp = new int[m][n];
        dp[0][0] = 1;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i > 0) {
                    dp[i][j] += dp[i - 1][j];
                }
                if (j > 0) {
                    dp[i][j] += dp[i][j - 1];
                }
            }
        }
        return dp[m - 1][n - 1];
    }

    @Test
    public void test() {
        int i = uniquePaths(3, 4);
        System.out.println(i);
    }

}

 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?

算法问题——(动态规划问题)_第2张图片

/**
 * Copyright (C), 2018-2020
 * FileName: uniquePathsWithObstacles63
 * Author:   xjl
 * Date:     2020/7/7 9:05
 * Description: 63. 不同路径 II
 */
package DP;

public class uniquePathsWithObstacles63 {

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];

        dp[0][0] = 1;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (obstacleGrid[i][j] == 1) {
                    dp[i][j] = 0;
                } else {
                    if (j > 0) {
                        dp[i][j] += dp[i][j - 1];
                    }
                    if (i > 0) {
                        dp[i][j] += dp[i - 1][j];
                    }
                }
            }
        }
        return dp[m - 1][n - 1];
    }
}

198. 打家劫舍

/**
 * Copyright (C), 2018-2020
 * FileName: rob198
 * Author:   xjl
 * Date:     2020/7/7 9:19
 * Description: 198. 打家劫舍
 */
package DP;

import org.junit.Test;

public class rob198 {

    public int subRob(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int frist = 0;
        int second = 0;
        int maxMoney = 0;
        for (int index = 0; index < nums.length; index++) {
            maxMoney = Math.max(nums[index] + frist, second);
            frist = second;
            second = maxMoney;
        }
        return maxMoney;
    }

    public int rob(int[] nums) {
        //输入数据的校验
        if (nums.length < 1) {
            return 0;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }

        //
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        dp[1] = nums[1];
        int max = Math.max(dp[0], dp[1]);

        for (int i = 2; i < nums.length; i++) {
            dp[i] = maxVal(dp, i - 1) + nums[i];
            max = max < dp[i] ? dp[i] : max;
        }
        return max;
    }

    private int maxVal(int[] dp, int i) {
        int max = -1;
        for (int j = 0; j < i; j++) {
            max = max < dp[j] ? dp[j] : max;
        }
        return max;
    }

    @Test
    public void test(){
        int[] array={1,2,3,1};
        int i = subRob(array);
        System.out.println(i);
    }

}

213. 打家劫舍 II

/**
 * Copyright (C), 2018-2020
 * FileName: rob213
 * Author:   xjl
 * Date:     2020/7/7 9:39
 * Description: 213. 打家劫舍 II
 */
package DP;

import org.junit.Test;

public class rob213 {

    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        //判断一个特殊的情况
        if (nums.length == 1) {
            return nums[0];
        }
        return Math.max(subRob(nums, 0, nums.length - 1), subRob(nums, 1, nums.length));
    }

    public int subRob(int[] nums, int start, int end) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int frist = 0;
        int second = 0;
        int maxMoney = 0;
        for (int index = start; index < end; index++) {
            maxMoney = Math.max(nums[index] + frist, second);
            frist = second;
            second = maxMoney;
        }
        return maxMoney;
    }

    @Test
    public void test(){
        int[] array={1,2,3,1};
        int rob = rob(array);
        System.out.println(rob);
    }
}

337. 打家劫舍 III

/**
 * Copyright (C), 2018-2020
 * FileName: rob337
 * Author:   xjl
 * Date:     2020/7/7 9:56
 * Description: 337. 打家劫舍 III
 */
package DP;

public class rob337 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public int rob(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int[] res = postRoot(root);
        return Math.max(res[0],res[1]);
    }

    private int[] postRoot(TreeNode root) {
        if (root == null) return new int[]{0, 0};
        int[] l = postRoot(root.left);
        int[] r = postRoot(root.right);
        int[] res = new int[2];
        res[0] = Math.max(l[0], l[1]) + Math.max(r[0], r[1]);
        res[1] = l[0] + r[0] + root.val;
        return res;
    }

}

你可能感兴趣的:(数据结构与算法)