【剑指Offer】京东笔经-金字塔弹球-2020-08-27

2020-12-07 10:08:27 浏览数 (1)

代码语言:javascript复制
      * 3
      *     1
      *   2 1 2
      * 3 4 2 1 3

解题思路

动态规划

代码语言:javascript复制
/**
 * @Author bennyrhys
 * @Date 2020-08-27 20:07
 */
/*
 public class T31 {
     public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         int n = sc.nextInt();
         int a[][] = new int[n][2*n - 1];
         for(int i = 0;i < n;i  ){
             for(int k = (2*n-1)/2 - i;k < (2*n - 1)-((2*n-1)/2 - i);k  ){
                 System.out.print(2*n-1);
                 System.out.print(k);
                 a[i][k] = sc.nextInt();
             }
             System.out.println();

         }

         for (int i = 0; i < a.length; i  ) {
             for (int j = 0; j < a[0].length; j  ) {
                 System.out.print(a[i][j]);
             }
             System.out.println();
         }
         /**
          * 3
          *     1
          *   2 1 2
          * 3 4 2 1 3
          * 00100
          * 02120
          * 34213
          * 7
          */

         for(int i = 1;i < n;i  ){
             for(int j = 0;j < a[0].length;j  ){
                 if(j == 0){
                     a[i][j] = Math.max(a[i - 1][j],a[i - 1][j   1])   a[i][j];
                 }else if(j == a[0].length - 1){
                     a[i][j] = Math.max(a[i - 1][j],a[i - 1][j - 1])   a[i][j];
                 }else{
                     int temp = Math.max(a[i - 1][j],a[i - 1][j - 1]);
                     a[i][j] = Math.max(temp,a[i - 1][j   1])   a[i][j];
                 }
             }
         }
         int res = a[n - 1][0];
         for(int k = 0;k < a[0].length;k  ){
             if (a[n - 1][k] > res){
                 res = a[n - 1][k];
             }
         }
         System.out.println(res);
     }
 }

0 人点赞