leetcode122
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
leetcode122的更多相关文章
- LeetCode122:Best Time to Buy and Sell Stock II
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- Leetcode-122 Best Time to Buy and Sell Stock II
#122 Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the pric ...
- [Swift]LeetCode122. 买卖股票的最佳时机 II | Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode122.买卖股票的最佳时机II
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必须在再次 ...
- LeetCode--122、167、169、189、217 Array(Easy)
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- leetcode122 买卖股票的最佳时机 python
题目:给定一个数组,它表示了一只股票的价格浮动,第i个元素代表的是股票第i天的价格.设计一个函数,计算出该股票的最大收益,注意,可以多次买入卖出,但下一次买入必须是在本次持有股票卖出之后.比如[1,7 ...
- leetcode122 Best Time to Buy and Sell Stock
题意:有一个数组,第i个数据代表的是第i天股票的价格,每天只能先卖出再买进(可以不卖出也可以不买进),求最大收益. 思路:自己去弄几个数组比划比划就知道了,比如[1,2,5,3,6],第一天买进,第二 ...
- LeetCode122——Best Time to Buy and Sell Stock II
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- leetcode 买卖股票问题
leetcode121 Best Time to Buy and Sell Stock 说白了找到最大的两组数之差即可 class Solution { public: int maxProfit(v ...
随机推荐
- hadoop之 Yarn 调度器Scheduler详解
概述 集群资源是非常有限的,在多用户.多任务环境下,需要有一个协调者,来保证在有限资源或业务约束下有序调度任务,YARN资源调度器就是这个协调者. YARN调度器有多种实现,自带的调度器为Capaci ...
- gitlab HA集群
https://docs.gitlab.com/ee/administration/high_availability/gitlab.html https://about.gitlab.com/hig ...
- Microsoft Dynamics CRM 4.0导入组织(Import Organization)时间过长的原因总结
952934 How to move the Microsoft Dynamics CRM 4.0 deployment http://support.microsoft.com/default ...
- JS怎么把字符串数组转换成整型数组
今天在学习highcharts时,遇到了一个把字符串数组转换为整形数组的问题,拿在这里讨论一下: 比如有一个字符串: var dataStr="1,2,3,4,5"; 现在需要把它 ...
- 关于Spring IOC (DI-依赖注入)
<Spring入门经典>这本书无论对于初学者或者有经验的工程师还是很值一看的,最近花了点时间回顾了Spring的内容,在此顺带记录一下,本篇主要与spring IOC相关 ,这篇博文适合初 ...
- 轻松理解execl系列函数
execl函数功能如下:启动一个可执行文件,并且对他进行传送参数.一些原型如下 #include <unistd.h> extern char **environ; int execl(c ...
- c#数据类型 与sql的对应关系 以及 取值范围
Short Name .NET Class Type Width Range (bits) SQL Datatype (Closest Match) Constraint to use (if nee ...
- 读书笔记:Sheldon.M.Ross:概率论基础教程:2014.01.22
贝叶斯公式与全概率公式 全概率公式:如果一件事情的发生有多个可能途径,那么这件事情的发生概率就是在不同途径下此事件发生的条件概率的加权平均.权值为各途径本身的发生概率. 贝叶斯公式:通过例子说明其含义 ...
- C/C++基础----表达式
1 基本概念 类型转换,小整型通常会被提升. 运算符重载,运算对象的个数.运算符的优先级和结合律都是无法改变的. 左值右值,对象被用做右值时,使用的是对象的值(内容):用做左值时,使用的是对象的身份( ...
- SpringBoot中实现依赖注入功能
本文转载自:https://blog.csdn.net/linzhiqiang0316/article/details/52639888 今天给大家介绍一下SpringBoot中是如何实现依赖注入的功 ...