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

      * 3
      *     1
      *   2 1 2
      * 3 4 2 1 3

解题思路

动态规划

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

  • 12
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑞 新

请小哥喝杯茶~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值