编程之美2.15子数组之和的最大值(二维)Java版
来源:互联网 发布:廖雪峰python下载 编辑:程序博客网 时间:2024/05/22 07:53
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package Test;/** * * 2.15 子数组之和的最大值(二维) */public class SearchArrayMaxSum2 { public static void main(String[] args) { int[][] arry = new int[][]{{2, -4, 1, -6, 7, 10}, {4, 3, 5, -8, 11, -9},{6,-3,9,-7,-1,-4}}; //解法一 int num1 = searchMax1(arry, arry.length, arry[0].length); System.out.println("num1 ="+num1); //解法二 int num2 = searchMax2(arry,arry.length,arry[0].length); System.out.println("num2 ="+num2); } private static int searchMax1(int[][] arry, int n, int m) { int[][] ps = new int[n + 1][m + 1]; //预处理 for (int i = 0; i <= n; i++) { ps[i][0] = 0; } for (int j = 0; j <= m; j++) { ps[0][j] = 0; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { ps[i][j] = ps[i - 1][j] + ps[i][j - 1] - ps[i - 1][j - 1] + arry[i - 1][j - 1]; } } Integer maxsum = null; for (int i_min = 1; i_min <= n; i_min++) { for (int i_max = i_min; i_max <= n; i_max++) { for (int j_min = 1; j_min <= m; j_min++) { for (int j_max = j_min; j_max <= m; j_max++) { int sum = ps[i_max][j_max] - ps[i_min - 1][j_max] - ps[i_max][j_min - 1] + ps[i_min - 1][j_min - 1]; if (maxsum == null || sum > maxsum) { if (maxsum == null) { maxsum = new Integer(sum); } maxsum = sum; } } } } } return maxsum; } private static int searchMax2(int[][] arry, int n, int m) { int[][] ps = new int[n + 1][m + 1]; //预处理 for (int i = 0; i <= n; i++) { ps[i][0] = 0; } for (int j = 0; j <= m; j++) { ps[0][j] = 0; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { ps[i][j] = ps[i - 1][j] + ps[i][j - 1] - ps[i - 1][j - 1] + arry[i - 1][j - 1]; } } Integer maxsum = null; for(int a =1;a<=n;a++){ for(int c =a ;c<=n;c++){ int Start = ps[c][m]-ps[a-1][m]-ps[c][m-1]+ps[a-1][m-1]; int All = ps[c][m]-ps[a-1][m]-ps[c][m-1]+ps[a-1][m-1]; for(int i =m-1;i>=1;i--){ if(Start < 0) Start =0; Start +=ps[c][i]-ps[a-1][i]-ps[c][i-1]+ps[a-1][i-1]; if(Start > All) All = Start; } if(maxsum == null || All > maxsum){ if(maxsum == null) maxsum = new Integer(All); maxsum = All; } } } return maxsum; }}
0 0
- 编程之美2.15子数组之和的最大值(二维)Java版
- 编程之美 子数组之和的最大值(二维)
- 编程之美---子数组之和的最大值(二维)
- 读书笔记之编程之美 - 2.15 子数组之和的最大值(二维)
- 编程之美:第二章 数字之魅 2.15子数组之和的最大值(二维)
- 编程之美2.15——二维子数组之和的最大值
- 编程之美2.15 子数组之和的最大值(二维)
- 编程之美2.15——二维子数组之和的最大值
- 编程之美读书笔记2.15 - 子数组之和的最大值(二维)
- 编程之美2.15子数组之和的最大值(二维)
- 编程之美-2.15 子数组之和的最大值(二维)
- 编程之美2.14求数组的子数组之和的最大值Java版
- 编程之美2.14求数组的子数组之和的最大值Java版
- 编程之美——子数组之和的最大值(二维)
- [编程之美] PSet2.15 子数组之和的最大值(二维)
- 编程之美-子数组之和的最大值(二维)方法整理
- 编程之美--求子数组之和的最大值
- 编程之美2.14求子数组之和的最大值
- mapreduce(二):wordcount详解
- 贩妖记 > 第六十九章,沈教授
- POJ 2689 Prime Distance
- HDU 1301-Jungle Roads (Kruscal 最小生成树)
- 学OD -- 消息断点 RUN跟踪
- 编程之美2.15子数组之和的最大值(二维)Java版
- 类的static成员函数和const成员函数
- 从苏宁电器到卡巴斯基第08篇:来到苏宁之前的过渡
- Angular中的$cacheFactory的作用和用法
- android 图片缓存
- Siege压力测试和评测工具
- C# socket通讯
- mysql 5.7.13免安装版配置。
- mysql 5.7.13 linux 安装 配置