当前位置:   article > 正文

动态规划汇总_各类动态规划汇总

各类动态规划汇总

一、俄罗斯套娃信封Leecode354,参考宫水三叶答案

  1. package zsh;
  2. import java.util.Arrays;
  3. import java.util.Comparator;
  4. import java.util.Scanner;
  5. public class MaxEnvelopes {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. String[] s = scanner.nextLine().trim().replace("[", "").replace("]", "").split(",");
  9. int num = s.length/2; //信封个数
  10. int[][] arr = new int[num][2];
  11. for(int i = 0; i < s.length/2; i++){
  12. arr[i][0] = Integer.parseInt(s[i*2]);
  13. arr[i][1] = Integer.parseInt(s[i*2+1]);
  14. }
  15. int[] count = new int[num];
  16. // 因为我们在找第 i 件物品的前一件物品时,会对前面的 i - 1 件物品都遍历一遍,因此第二维(高度)排序与否都不影响
  17. // 先按底边排序
  18. Arrays.sort(arr, new Comparator<int[]>() {
  19. @Override
  20. public int compare(int[] o1, int[] o2) {
  21. return o1[0] - o2[0];
  22. }
  23. });
  24. int max = 1; //最少是本身1个
  25. for(int i = 0; i < num; i++){
  26. count[i] = 1; //最小值为1
  27. for(int j = i - 1; j >= 0; j--){
  28. if(arr[j][0] < arr[i][0] && arr[j][1] < arr[i][1]){
  29. count[i] = Math.max(count[i], count[j]+1);
  30. }
  31. }
  32. max = Math.max(max, count[i]);
  33. }
  34. System.out.println(max);
  35. }
  36. }

改编:垒积木,当一个积木长宽不比另一个积木大,则这个积木能够垒在另一个积木上面,问最多垒多少层,积木可旋转

不同:(1)积木可旋转,我们可以先将较大的一边保存在a[i][0],较小的边保存到a[i][1];

(2)不用绝对大于,不小于即可,这里我们在首先对数组排序就要多加一步,先比长边,长边相等比短边,这样在下面排序 就会为 [5,4][6,3][6,4][6,6][7,6], 而原来的排序规则会      [5,4][6,3][6,6][6,4][7,6]这样现象出现,在按照上述从前向后动态规划时就会漏掉(如[6,6]装不到[6,4],程序只装前面的),上面题不会出现是因为比的绝对大,不考虑相同边情况,而绝对大一定会按顺序来。

输入  [[5,4],[6,3],[6,7],[6,6],[4,6]]

输出 4

  1. package zsh;
  2. import java.util.Arrays;
  3. import java.util.Comparator;
  4. import java.util.Scanner;
  5. public class MaxSquare {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. String[] s = scanner.nextLine().trim().replace("[", "").replace("]", "").split(",");
  9. int num = s.length/2; //信封个数
  10. int[][] arr = new int[num][2];
  11. for(int i = 0; i < s.length/2; i++){
  12. arr[i][0] = Math.max(Integer.parseInt(s[i*2]), Integer.parseInt(s[i*2+1]));
  13. arr[i][1] = Math.min(Integer.parseInt(s[i*2]), Integer.parseInt(s[i*2+1]));
  14. // arr[i][0] = Integer.parseInt(s[i*2]);
  15. // arr[i][1] = Integer.parseInt(s[i*2+1]);
  16. }
  17. int[] count = new int[num];
  18. // 因为我们在找第 i 件物品的前一件物品时,会对前面的 i - 1 件物品都遍历一遍,因此第二维(高度)排序与否都不影响
  19. // 先按底边排序
  20. Arrays.sort(arr, new Comparator<int[]>() {
  21. @Override
  22. public int compare(int[] o1, int[] o2) {
  23. if(o1[0] != o2[0]){
  24. return o1[0] - o2[0];
  25. }
  26. return o1[1] - o2[1];
  27. }
  28. });
  29. int max = 1; //最少是本身1个
  30. for(int i = 0; i < num; i++){
  31. count[i] = 1; //最小值为1
  32. for(int j = i - 1; j >= 0; j--){
  33. if(arr[j][0] <= arr[i][0] && arr[j][1] <= arr[i][1]){
  34. count[i] = Math.max(count[i], count[j]+1);
  35. }
  36. }
  37. max = Math.max(max, count[i]);
  38. }
  39. System.out.println(max);
  40. }
  41. }
  42. // [[5,4],[6,3],[6,7],[6,6],[4,6]]

二、接雨水leecode42

参考解法三

https://leetcode-cn.com/problems/trapping-rain-water/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-w-8/     

  1. package zsh;
  2. import java.util.Scanner;
  3. public class RainLeecode42 {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. String[] strArr = scanner.nextLine().trim().replace("[", "").replace("]", "").split(",");
  7. int num = strArr.length;
  8. int[] height = new int[num];
  9. for(int i = 0; i < num; i++){
  10. height[i] = Integer.parseInt(strArr[i]);
  11. }
  12. int[] leftMax = new int[num];
  13. int[] rightMax = new int[num];
  14. for(int i = 1; i < num - 1; i++){ //第i列左边最高
  15. leftMax[i] = Math.max(leftMax[i-1], height[i-1]);
  16. }
  17. for(int i = num - 2; i > 0; i--){ //第i列右边最高
  18. rightMax[i] = Math.max(rightMax[i+1], height[i+1]);
  19. }
  20. int sum = 0;
  21. for(int i = 1; i < num - 1; i++){
  22. int min = Math.min(leftMax[i], rightMax[i]); //木桶效应,找出两边最高中较小值
  23. if(height[i] < min){ //i列高度小于两边较小值,和较小值相差部分即可存水
  24. sum += min - height[i];
  25. }
  26. }
  27. System.out.println(sum);
  28. }
  29. }

改编:坑处存货(时间复杂度O(M*N)太大,不行)

  1. package zsh;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. public class LadderCargo {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7. int cargoLength = Integer.parseInt(scanner.nextLine().trim());
  8. int num = Integer.parseInt(scanner.nextLine().trim());
  9. int[] height = Arrays.stream(scanner.nextLine().trim().split(",")).mapToInt(Integer::valueOf).toArray();
  10. int minHeight = Arrays.stream(height).min().getAsInt();
  11. int sum = 0;
  12. if(minHeight < 0) {
  13. int[] count = new int[Math.abs(minHeight)];
  14. for (int i = 0; i < num; i++) {
  15. if (height[i] >= 0) {
  16. Arrays.fill(count, 0); //大于等于地平面累计中断清零
  17. continue;
  18. }
  19. for(int j = 0; j < Math.abs(height[i]); j++){
  20. count[j]++; //每一层+1
  21. if(count[j] == cargoLength){ //达到货物长度清零
  22. sum++; //已放货物数量
  23. count[j] = 0;
  24. }
  25. }
  26. }
  27. }
  28. System.out.println(sum);
  29. }
  30. }
  31. // 0,-1,-2,0
  32. //3 8 0,-1,-2,2,1,1,1,2
  33. // 2
  34. // 11
  35. // 0,-1,-2,-3,-3,-2,0,1,2,-3,-3
  36. // 输出 8

三、送外卖,大厦共L层(0<L<=10^5,处于第N层,每分钟可步行向上到N+1层,或向下到N-1层,或者坐电梯到2*N层,计算到达M层的最短时间。

输入 N M

5 17

输出 4

解释 5-10-9-18-17

  1. package zsh;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. public class ClimbStairs {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7. int base = scanner.nextInt();
  8. int aim = scanner.nextInt();
  9. int res = 0;
  10. int[] time = new int[aim+1]; //爬到每一层需要的时间
  11. if(aim <= base){ //目标楼层低于当前楼层,步行,电梯只上不下
  12. System.out.println(base - aim);
  13. }
  14. else{
  15. for (int i = 1; i <= base; i++){
  16. time[i] = base - i;
  17. }
  18. for (int i = base+1; i < aim+1; i++){
  19. int walk = time[i-1]+1; //走路
  20. int elevator = 0; //电梯
  21. if(i%2 == 0){
  22. elevator = time[i/2]+1;
  23. }
  24. else{
  25. elevator = time[(i+1)/2]+2; //多上一层再下一层
  26. }
  27. time[i] = Math.min(walk, elevator);
  28. }
  29. }
  30. System.out.println(time[aim]);
  31. }
  32. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号