前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【剑指Offer】京东笔经-金字塔弹球-2020-08-27

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

作者头像
瑞新
发布2020-12-07 10:08:27
2530
发布2020-12-07 10:08:27
举报
文章被收录于专栏:用户3288143的专栏
代码语言: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);
     }
 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/08/28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 解题思路
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档