public class Solution {
public int MaxProfit(int[] prices) {
var list = new List<KeyValuePair<int, int>>();
//记录所有的极大值和极小值 if (prices.Length <= )
{
return ;
}
else if (prices.Length == )
{
return prices[] - prices[] > ? prices[] - prices[] : ;
}
else
{
for (int i = ; i < prices.Length - ; i++)
{
var last = prices[i - ];
var cur = prices[i];
var next = prices[i + ];
if ((last < cur && cur >= next) || (last <= cur && cur > next))
{
list.Add(new KeyValuePair<int, int>(i, ));//记录极大值
}
if ((last > cur && cur <= next) || (last >= cur && cur < next))
{
list.Add(new KeyValuePair<int, int>(i, -));//记录极小值
}
} var firstnode = list.FirstOrDefault();
var lastnode = list.LastOrDefault(); if (firstnode.Value == )
{
list.Add(new KeyValuePair<int, int>(, -));//第一个坐标当做极小值
}
else if (firstnode.Value == -)
{
list.Add(new KeyValuePair<int, int>(, ));//第一个坐标当做极大值
}
else
{
if (prices[] < prices[prices.Length - ])
{
list.Add(new KeyValuePair<int, int>(, -));//第一个坐标当做极小值
}
else
{
list.Add(new KeyValuePair<int, int>(, ));//第一个坐标当做极大值
}
} if (lastnode.Value == )
{
list.Add(new KeyValuePair<int, int>(prices.Length - , -));//最后一个坐标当做极小值
}
else if (lastnode.Value == -)
{
list.Add(new KeyValuePair<int, int>(prices.Length - , ));//最后一个坐标当做极大值
}
else
{
if (prices[] < prices[prices.Length - ])
{
list.Add(new KeyValuePair<int, int>(prices.Length - , ));//最后一个坐标当做极大值
}
else
{
list.Add(new KeyValuePair<int, int>(prices.Length - , ));//最后一个坐标当做极大值
}
} list = list.OrderBy(x => x.Key).ToList(); var sum = ; var min = -;
var max = -; int pair = ;
for (int i = ; i < list.Count; i++)
{
if (list[i].Value == -)
{
min = list[i].Key;
pair = ;
}
if (pair == && list[i].Value == )
{
max = list[i].Key;
pair += ; }
if (pair == )
{
sum += prices[max] - prices[min];
pair = ;
}
}
return sum;
}
}
}

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/#/description

上面这种方法比较复杂,还有一种更简单直接的方法

public class Solution {
public int MaxProfit(int[] prices) {
int total = ;
for (int i=; i< prices.Length-; i++) {
if (prices[i+]>prices[i]) total += prices[i+]-prices[i];
} return total;
}
}

只要后面的比前面的多,就进行一次买卖。计算的结果是一样的,但是与题意并不相符。题意要求在同一时间不能既买又卖。也就是要尽量减少交易次数。

所以严格来讲,第二种方法并不是是正确的解答,虽然答案一样。

时隔一年半的时间,在学习了贪婪算法的思想后,重新解此题,明白了上面的思想。只要下一次比上一次的金额高,就进行一次累计,C++程序如下:

int maxProfit(vector<int>& prices) {
int sum = ;
if (prices.size() > )
{
int lastP = prices[];
for (int i = ; i < prices.size(); i++)
{
int p = prices[i];
if (p > lastP)
{
sum += p - lastP;
}
lastP = p;
}
}
return sum;
}

补充一个python的实现:

 class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
sums =
for i in range(,n):
if prices[i] > prices[i-]:
sums += prices[i] - prices[i-]
return sums

最新文章

  1. RedHat Enterprise Linux 6.4 使用 Centos 6 的yum(转)
  2. 第一章-第三题(目前流行的源程序版本管理软件和项目管理软件优缺点)--By梁旭晖
  3. Python安装pandas
  4. QA、Selenium WebDriver (Q&A)
  5. python 安装加环境变量
  6. HTML &lt;meta&gt; 标签
  7. Power Map 入门
  8. Netsharp快速入门(之10) 销售管理(插件、资源、业务建模)
  9. 【HTML】Jquery前台传参及接收
  10. HDU 1546 Idiomatic Phrases Game(最短路,Dijsktra,理解题意很重要)
  11. async:false同步请求,浏览器假死
  12. python 输出小数控制
  13. SQL Server 空间监测
  14. html系列教程--p param progress rp rt ruby script select small source
  15. Javascript 装载和执行
  16. requests从api中获取数据并存放到mysql中
  17. 5.Flask-Migrate
  18. vue 使用v-html指令渲染的富文本无法修改样式的解决方法
  19. php小数加减精度问题,比特币计算精度问题
  20. OpenCV轮廓vectorvector

热门文章

  1. 网络流--最小费用最大流MCMF模板
  2. jQuery--- .hasOwnProperty 用法
  3. 定时器setTimeout()的传参方法
  4. Documentation/usb/gadget_configfs.txt
  5. 在 ASP.NET 网页中不经过回发而以编程方式实现客户端回调
  6. JVM 之:Class 类文件结构
  7. django admin model使用技巧
  8. MapReduce-朴素贝叶斯
  9. ubuntu 14.04 git clone 出现 fatal: Unable to find remote helper for &#39;https&#39;
  10. 【Spring学习笔记-2】Myeclipse下第一个Spring程序-通过ClassPathXmlApplicationContext加载配置文件