Best Time to Buy and Sell Stock
来源:互联网 发布:mysql设置初始密码 编辑:程序博客网 时间:2024/06/07 00:59
这是求最大子数组的问题,编码不难,有穷举、分治、动态规划三种方法。引起我兴趣的是分治的子问题划分和动态规划的最优子结构的证明。
分治:
从数组的中间分开,最大子数组出现的三种情况:
1、在左半部分
2、在右半部分
3、跨越中点(求跨越中点的子数组等价于求从中间元素开始往左累加的最大值 + 从中间元素开始往右累加的最大值)
动态规划:
对于所有数组元素进行划分,如果加上该元素之前temp>0且temp+a[i]<0,那么该元素a[i]是一个边界,这样,数组会形成好多段,每段结束元素都满足temp>0且temp+a[i]<0.所以我们能得到多个划分块a[p..q],每个划分快的和是负值,划分块有这样的性质,对任意p<=i<q,显然,sum(a[p..i])>=0且sum(a[i..q])<0;根据划分块性质,容易证明,只要子数组横跨多个划分快,其求和值必定小于某个单独的划 分快中的数组求和。
0 0
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time To Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- 关于split函数
- Selenium+Java+Appium+TestNg环境搭建——Web自动化测试与HTML5测试
- 自定义sublime的快捷键
- mongodb3.0的索引管理学习整理
- C/C++的static关键字
- Best Time to Buy and Sell Stock
- 两个重叠控件的放大缩小实现
- 应届毕业生的户口、档案、三方协议、报到证、见习期、工龄计算、转正定级等重要知识普及!
- HTML5 Application Cache
- Windows下git log显示的注释是乱码
- can't resolve host-使用GIT 请求远程仓库时出现的问题以及解决办法
- postgresql函数
- 安装vbundle
- Struts2的三种传值方式