牛客網算法練習題目——(動態規劃問題)

一個機器人位於一個 m x n 網格的左上角 (起始點在下圖中標記爲“Start” )。機器人每次只能向下或者向右移動一步。機器人試圖達到網格的右下角(在下圖中標記爲“Finish”)。問總共有多少條不同的路徑?

/**
 * 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”)。現在考慮網格中有障礙物。那麼從左上角到右下角將會有多少條不同的路徑?

/**
 * 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;
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章