每天一道算法题(10)——数对之差的最大值
来源:互联网 发布:无锡cnc编程招聘 编辑:程序博客网 时间:2024/05/16 01:50
题目:在数组中,数字减去它右边的数字得到一个数对之差。求所有数对之差的最大值。例如在数组{2, 4, 1, 16, 7, 5, 11, 9}中,数对之差的最大值是11,是16 减去5 的结果。
1.思路
Solution1: 将其转换成求最大子数组问题。引入辅助数组diff,长度为n-1。diff[i]=dif[i]-diff[i+1]。求出最大子数组以及位置low,high。则对应最大差为data[low]-data[high+1]。
Solution2: 使用动态规划法。假设data[i]减去某个数,其最大的数对差为currentMax。则data[i-1]的最大数对差为data[i-1]-data[i]和data[i-1]-data[i]+currentMax中的较大的那一个。从后往前遍历数组,记录最大的currentMax以及对应的low、high输出即可。
时间复杂度O(n),空间复杂度O(1)。
2.代码
solution2:
#include"iostream"using namespace std;void fun(int data[],int size,int &low,int &high){ int max=data[size-1]-data[size-2];//最大的数对差int currentMax=data[size-1]-data[size-2];//当前的数对差int l=size-2;int h=size-1;low=l;//被减数位置high=h;//减数位置for(int i=size-3;i>=0;i--){ if(currentMax>0){ currentMax=data[i]-data[i+1]+currentMax; l--; } else{ currentMax=data[i]-data[i+1]; l=i; h=i+1; } if(currentMax>max){ low=l; high=h; max=currentMax; } }}void main(){int data[]={2,4,1,16,7,5,11,9};int low,high;fun(data,sizeof(data)/sizeof(int),low,high);cout<<"the max is "<<data[low]-data[high]<<endl;}
0 0
- 每天一道算法题(10)——数对之差的最大值
- 每日一道算法题:求数对之差的最大值
- 数对之差的最大值【算法】
- 数对之差的最大值[算法]
- 数对之差的最大值[算法]
- 动态规划——数对之差的最大值
- 数对之差的最大值
- 数对之差的最大值
- 数对之差的最大值
- 数对之差的最大值
- 数对之差的最大值
- 数对之差的最大值
- 数对之差的最大值
- 数对之差的最大值
- 数对之差的最大值
- 求数对之差的最大值
- 数对之差的最大值
- 数对之差的最大值
- 第12周项目4-点、圆的关系(2)
- 1289 大鱼吃小鱼
- Leetcode #10 Regular Expression Matching
- 分布式文件系统比较
- opengl配置
- 每天一道算法题(10)——数对之差的最大值
- 分支界限法
- AutoLayout自动布局公式的理解
- ubuntu下eclipse tomcat jdk8组合进行j2ee开发
- CSS中表示cellpadding和cellspacing的方法
- Combinations
- [Hive]Hive多分区操作
- 回溯法
- 面向对象中比较重要的this,static,super,单利设计的总结和归纳