[leetcode] Best Time to Buy and Sell Stock II
2015-11-26 12:25
197 查看
题目:
分析:同题目 Best Time to Buy and Sell Stock ,这些只要注意 叠加每一小段的收益
代码:
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
分析:同题目 Best Time to Buy and Sell Stock ,这些只要注意 叠加每一小段的收益
代码:
public int maxProfit(int[] prices) { if(prices == null || prices.length == 0) return 0; int min = prices[0], max = prices[0]; int result = 0; for (int i = 1; i < prices.length; i++) { if (prices[i] > max) { // 叠加每一小段的收益 max = prices[i]; result += max - min; min = prices[i]; } else if (prices[i] < min) { // 重新规定查找区域 min = prices[i]; max = prices[i]; } } return result; }
相关文章推荐
- 语义模型
- jQuery实现图片轮播效果(仿QQ商城首页、天猫首页)
- YUV与RGB格式转换
- 如何设置 zend studio 默认编码为UTF8
- 在创业公司的那些日子(一)
- Java中数组声明和初始化
- sublime2使用和配置
- SPRING官方网下载地址
- 详解iOS的UI开发中控制器的创建方法
- HTML中小meta的大作用
- CAS原子操作实现无锁及性能分析
- iOS App性能优化
- 源于缺乏控制力的不安全感
- visual studio 开发常用设置
- Android图形系统之Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的联系
- 插曲:Smoking
- 再不能生动的并查集讲解
- 《读书笔记》系列4:MySQL开发者SQL权威指南
- Description Resource Path Location Type 错误解决
- ACM学习历程—UESTC 1226 Huatuo's Medicine(数学)(2015CCPC L)