编程之美 - 二维数组最大和的子序列
来源:互联网 发布:商标查询软件 免费 编辑:程序博客网 时间:2024/05/21 14:02
问题描述:
找出二维数组中的最大和的子序列。二维数组中的子序列是二维数组中的一个方块。
思路一:
使用暴力的方式,从每一个元素开始,变量以它为左上定点的所有的可构造的方块。
时间复杂度 N*N*M*M* sum(MN)
函数:getMaxSum1()
思路二:
参考一维数组的方式,将二维数组的转化为一个一维数组后,再进行求和。
例如:在二维数组中选择了 a 到 c 行,把a~c行中的列元素进行求和,就可以得到一个一维数组。从0到N不断改变a~c的范围就可以变量整个数组。
复杂度为 N*N*M
函数:getMaxSum2()
#include <iostream>using namespace std;#define INF -10000000int max(int x, int y){ return (x > y) ? x : y;}///////////////////////////////////////////////////////////////////////////////////// Get Sum function 1///////////////////////////////////////////////////////////////////////////////////int sum_rect(int arr[][6], int row_min, int row_max, int col_min, int col_max){ int sum = 0; int i = 0, j = 0; for (i = row_min; i <= row_max; i++) for (j = col_min; j <= col_max; j++) sum += arr[i][j]; cout << "(" << row_min << ", " << row_max << ", " << col_min << ", " << col_max << ", " << ") = " << sum << endl; return sum;}int getMaxSum1(int arr[][6], int row, int col){ int ret = 0; int row_min, row_max, col_min, col_max; for (row_min = 0; row_min < row; row_min++) for (row_max = row_min; row_max < row; row_max++) for (col_min = 0; col_min < col; col_min++) for (col_max = col_min; col_max < col; col_max++) ret = max(ret, sum_rect(arr, row_min, row_max, col_min, col_max)); return ret;}///////////////////////////////////////////////////////////////////////////////////// Get Sum function 2///////////////////////////////////////////////////////////////////////////////////int BC(int arr[][6], int a, int c, int col){ int i = 0; int sum = 0; for (i = a; i <= c; i++) { sum += arr[i][col]; } cout << "(" << a << ", " << c << ", " << col << ", " << ") = " << sum << endl; return sum;}int getMaxSum2(int arr[][6], int row, int col){ int a = 0, c = 0; int i = 0; int start = 0, sum = 0, max_val = INF; for (a = 0; a < row; a++) for(c = a; c < row; c++) { start = BC(arr, a, c, col-1); sum = start; for (i = col-2; i >= 0; i--) { if (start < 0) start = 0; start += BC(arr, a, c, i); if (start > sum) sum = start; } if (sum > max_val) max_val = sum; } return max_val;}void main(){ int i = 0; int test1[4][6] = {{1, -2, 3, 5, -3, 2}, {1, -6, 20, 20, -2, 1}, {5, 0, 20, 20, -10, 4}, {5, 0, 1, -8, 2, 4}}; i = getMaxSum1(test1, 4, 6); cout << "max value = "<< i << endl; i = getMaxSum2(test1, 4, 6); cout << "max value = "<< i << endl; cin>> i;}
0 0
- 编程之美 - 二维数组最大和的子序列
- 编程之美 - 数组最大和的子序列
- 编程之美2.15 二维数组最大子数组的和(数组下标从(1,1)开始)
- 编程之美 - 最大子数组和
- [编程之美]子数组的最大和扩展题
- 编程之美 子数组之和的最大值(二维)
- 编程之美---子数组之和的最大值(二维)
- 剑指offer-连续子数组的最大和----编程之美-连续子数组的最大和
- 编程之美之"子数组的最大乘积"
- 编程之美---子数组最大乘积
- 编程之美 - 数组最长子序列
- 子数组的最大乘积-编程之美
- 编程之美----子数组的最大乘积
- [编程之美]子数组的最大乘积
- 编程之美 子数组的最大乘积
- 编程之美:子数组的最大乘积
- 编程之美2.13 子数组的最大乘积
- 编程之美--子数组的最大乘积
- Shell中的循环语句for、while、until
- 表单设计中<input>的size属性与maxlength属性的区别
- java连接网上已有的天气webService
- python实例
- jquery刷新部分界面
- 编程之美 - 二维数组最大和的子序列
- python packages
- 管家婆辉煌Ⅱ TOP+ 10.3最新全版本完美免加密狗破解版
- 一个成功的 Git 分支模型
- 如何才能让你的平台独占鳌头
- android studio layout中的preview异常
- Intellij idea 保存代码到Onenote空格消失问题
- vs2010串口开发随记(工具)
- javascript原型与原型链