动态规划(四)

115 阅读3分钟

走路最小距离

给定一个二维数组matrix,一个人必须从左上角出发,最后到达右下角

沿途只可以向下或者向右走,沿途的数字都累加就是距离累加和

返回最小距离累加和

public static int minPathSum1(int[][] m) {
   if (m == null || m.length == 0 || m[0] == null || m[0].length == 0) {
      return 0;
   }
   int row = m.length;
   int col = m[0].length;
   int[][] dp = new int[row][col];
   dp[0][0] = m[0][0];
   for (int i = 1; i < row; i++) {
      dp[i][0] = dp[i - 1][0] + m[i][0];
   }
   for (int j = 1; j < col; j++) {
      dp[0][j] = dp[0][j - 1] + m[0][j];
   }
   for (int i = 1; i < row; i++) {
      for (int j = 1; j < col; j++) {
         dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + m[i][j];
      }
   }
   return dp[row - 1][col - 1];
}

// 把二维压缩成一维数组,节省空间
public static int minPathSum2(int[][] m) {
   if (m == null || m.length == 0 || m[0] == null || m[0].length == 0) {
      return 0;
   }
   int row = m.length;
   int col = m[0].length;
   int[] dp = new int[col];
   dp[0] = m[0][0];
   for (int j = 1; j < col; j++) {
      dp[j] = dp[j - 1] + m[0][j];
   }
   for (int i = 1; i < row; i++) {
      dp[0] += m[i][0];
      for (int j = 1; j < col; j++) {
         dp[j] = Math.min(dp[j - 1], dp[j]) + m[i][j];
      }
   }
   return dp[col - 1];
}

凑钱1

arr是货币数组,其中的值都是正数。再给定一个正数aim。

每个值都认为是一张货币,

即便是值相同的货币也认为每一张都是不同的,

返回组成aim的方法数

例如:arr = {1,1,1},aim = 2

第0个和第1个能组成2,第1个和第2个能组成2,第0个和第2个能组成2

一共就3种方法,所以返回3

public static int coinWays(int[] arr, int aim) {
   return process(arr, 0, aim);
}

// arr[index....] 组成正好rest这么多的钱,有几种方法
public static int process(int[] arr, int index, int rest) {
   if(index == arr.length) {
      return rest == 0 ? 1 : 0;
   }
   if(rest < 0) {
      return 0;
   }
   int p1 = process(arr,index + 1,rest);
   int p2 = process(arr,index + 1,rest - arr[index]);
   return p1 + p2;
}

public static int dp(int[] arr, int aim) {
   int N = arr.length;
   int[][] dp = new int[N + 1][aim + 1];
   dp[N][0] = 1;
   for (int index = N - 1; index >=0;index--) {
      for(int rest = 0; rest < aim + 1; rest++) {
         int p1 = dp[index + 1][rest];
         int p2 = rest - arr[index] >= 0 ? dp[index + 1][rest - arr[index]] : 0;
         dp[index][rest] =  p1 + p2;
      }
   }
   return dp[0][aim];
}

凑钱2

arr是面值数组,其中的值都是正数且没有重复。再给定一个正数aim。

每个值都认为是一种面值,且认为张数是无限的。

返回组成aim的方法数

例如:arr = {1,2},aim = 4

方法如下:1+1+1+1、1+1+2、2+2

一共就3种方法,所以返回3

public static int coinsWay(int[] arr, int aim) {
   if (arr == null || arr.length == 0 || aim < 0) {
      return 0;
   }
   return process(arr, 0, aim);
}

// arr[index....] 所有的面值,每一个面值都可以任意选择张数,组成正好rest这么多钱,方法数多少?
public static int process(int[] arr, int index, int rest) {
   if (index == arr.length) { // 没钱了
      return rest == 0 ? 1 : 0;
   }
   int ways = 0;
   for (int zhang = 0; zhang * arr[index] <= rest; zhang++) {
      ways += process(arr, index + 1, rest - (zhang * arr[index]));
   }
   return ways;
}

// 斜率优化
public static int dp2(int[] arr, int aim) {
   if (arr == null || arr.length == 0 || aim < 0) {
      return 0;
   }
   int N = arr.length;
   int[][] dp = new int[N + 1][aim + 1];
   dp[N][0] = 1;
   for (int index = N - 1; index >= 0; index--) {
      for (int rest = 0; rest <= aim; rest++) {
         dp[index][rest] = dp[index + 1][rest];
         if (rest - arr[index] >= 0) {
            dp[index][rest] += dp[index][rest - arr[index]];
         }
      }
   }
   return dp[0][aim];
}

凑钱3

arr是货币数组,其中的值都是正数。再给定一个正数aim。

每个值都认为是一张货币,

认为值相同的货币没有任何不同,

返回组成aim的方法数

例如:arr = {1,2,1,1,2,1,2},aim = 4

方法:1+1+1+1、1+1+2、2+2

一共就3种方法,所以返回3

public static class Info {
   public int[] coins;
   public int[] zhangs;

   public Info(int[] c, int[] z) {
      coins = c;
      zhangs = z;
   }
}

public static Info getInfo(int[] arr) { // 需要遍历原始数组,重新统计,每种面值货币以及对应的张数
   HashMap<Integer, Integer> counts = new HashMap<>();
   for (int value : arr) {
      if (!counts.containsKey(value)) {
         counts.put(value, 1);
      } else {
         counts.put(value, counts.get(value) + 1);
      }
   }
   int N = counts.size();
   int[] coins = new int[N];
   int[] zhangs = new int[N];
   int index = 0;
   for (Entry<Integer, Integer> entry : counts.entrySet()) {
      coins[index] = entry.getKey();
      zhangs[index++] = entry.getValue();
   }
   return new Info(coins, zhangs);
}

public static int coinsWay(int[] arr, int aim) {
   if (arr == null || arr.length == 0 || aim < 0) {
      return 0;
   }
   Info info = getInfo(arr);
   return process(info.coins, info.zhangs, 0, aim);
}

// coins 面值数组,正数且去重
// zhangs 每种面值对应的张数
public static int process(int[] coins, int[] zhangs, int index, int rest) {
   if (index == coins.length) {
      return rest == 0 ? 1 : 0;
   }
   int ways = 0;
   for (int zhang = 0; zhang * coins[index] <= rest && zhang <= zhangs[index]; zhang++) {
      ways += process(coins, zhangs, index + 1, rest - (zhang * coins[index]));
   }
   return ways;
}

// 斜率优化
public static int dp2(int[] arr, int aim) {
   if (arr == null || arr.length == 0 || aim < 0) {
      return 0;
   }
   Info info = getInfo(arr);
   int[] coins = info.coins;
   int[] zhangs = info.zhangs;
   int N = coins.length;
   int[][] dp = new int[N + 1][aim + 1];
   dp[N][0] = 1;
   for (int index = N - 1; index >= 0; index--) {
      for (int rest = 0; rest <= aim; rest++) {
         dp[index][rest] = dp[index + 1][rest];
         if (rest - coins[index] >= 0) {
            dp[index][rest] += dp[index][rest - coins[index]];
         }
         if (rest - coins[index] * (zhangs[index] + 1) >= 0) {
            dp[index][rest] -= dp[index + 1][rest - coins[index] * (zhangs[index] + 1)];
         }
      }
   }
   return dp[0][aim];
}

醉汉Bob

给定5个参数,N,M,row,col,k

表示在N*M的区域上,醉汉Bob初始在(row,col)位置

Bob一共要迈出k步,且每步都会等概率向上下左右四个方向走一个单位

任何时候Bob只要离开N*M的区域,就直接死亡

返回k步之后,Bob还在N*M的区域的概率

public static double livePosibility1(int row, int col, int k, int N, int M) {
   return (double) process(row, col, k, N, M) / Math.pow(4, k);
}

// 目前在row,col位置,还有rest步要走,走完了如果还在棋盘中就获得1个生存点,返回总的生存点数
public static long process(int row, int col, int rest, int N, int M) {
   if (row < 0 || row == N || col < 0 || col == M) {
      return 0;
   }
   // 还在棋盘中!
   if (rest == 0) {
      return 1;
   }
   // 还在棋盘中!还有步数要走
   long up = process(row - 1, col, rest - 1, N, M);
   long down = process(row + 1, col, rest - 1, N, M);
   long left = process(row, col - 1, rest - 1, N, M);
   long right = process(row, col + 1, rest - 1, N, M);
   return up + down + left + right;
}

public static double livePosibility2(int row, int col, int k, int N, int M) {
   long[][][] dp = new long[N][M][k + 1];
   for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
         dp[i][j][0] = 1;
      }
   }
   for (int rest = 1; rest <= k; rest++) {
      for (int r = 0; r < N; r++) {
         for (int c = 0; c < M; c++) {
            dp[r][c][rest] = pick(dp, N, M, r - 1, c, rest - 1);
            dp[r][c][rest] += pick(dp, N, M, r + 1, c, rest - 1);
            dp[r][c][rest] += pick(dp, N, M, r, c - 1, rest - 1);
            dp[r][c][rest] += pick(dp, N, M, r, c + 1, rest - 1);
         }
      }
   }
   return (double) dp[row][col][k] / Math.pow(4, k);
}