public class Solution {
public int MaxProfit(int[] prices)
{
//寻找最优极值点(包括2个端点) if (prices.Length < )
{
return ;
}
else if (prices.Length == )
{
return prices[] - prices[] > ? prices[] - prices[] : ;
}
else//至少3个点
{
//先对原始数据进行压缩
var list = new List<int>();
for (int i = ; i < prices.Length - ; i++)
{
if (prices[i] != prices[i + ])
{
list.Add(prices[i]);
}
}
var last = list.LastOrDefault();
if (last != prices[prices.Length - ])
{
list.Add(prices[prices.Length - ]);
} var dic = new Dictionary<int, int>();//记录所有极值点极其类型
//Key是index,Value是类型:-1是极小,1是极大 //list已经压缩,没有平行点了
for (int i = ; i < list.Count - ; i++)
{
//判断是否是极大值点
if (list[i - ] < list[i] && list[i] > list[i + ])
{
dic.Add(i, );
}
else if (list[i - ] > list[i] && list[i] < list[i + ])
{
dic.Add(i, -);
}
//判断是否是极小值点
} //处理端点
if (dic.Count == )
{
return list[list.Count - ] - list[] > ? list[list.Count - ] - list[] : ;
}
else
{
var list2 = dic.OrderBy(x => x.Key).ToList();
var d1 = list2.FirstOrDefault();
var d2 = list2.LastOrDefault();
if (d1.Value == )
{
list2.Add(new KeyValuePair<int, int>(, -));
}
else
{
list2.Add(new KeyValuePair<int, int>(, ));
} if (d2.Value == )
{
list2.Add(new KeyValuePair<int, int>(list.Count - , -));
}
else
{
list2.Add(new KeyValuePair<int, int>(list.Count - , ));
} list2 = list2.OrderBy(x => x.Key).ToList();//得到全部的极值点 var maxProfit = ; for (int i = ; i < list2.Count; i++)
{
if (list2[i].Value == -)
{
for (int j = i; j < list2.Count; j++)
{
if (list2[j].Value == )
{
if (list[list2[j].Key] - list[list2[i].Key] > maxProfit)
{
maxProfit = list[list2[j].Key] - list[list2[i].Key];
}
}
}
}
}
return maxProfit;
} }
}
}

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

原来的计算方式太复杂了,使用优化的方式如下:

class Solution {
public:
int maxProfit(vector<int>& prices) {
int minprice = INT_MAX;
int maxprofit = ;
for (int i = ; i < prices.size(); i++)
{
if (minprice > prices[i])
{
minprice = prices[i];
}
else if (prices[i] - minprice > maxprofit)
{
maxprofit = prices[i] - minprice;
}
}
return maxprofit;
}
};

补充一个python的实现:

 import sys
class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
maxval =
minval = sys.maxsize
for i in range(n):
cur = prices[i]
minval = min(minval,cur)
maxval = max(maxval,cur-minval)
return maxval

leetcode121的更多相关文章

  1. LeetCode121: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. If you w ...

  2. Leetcode-121 Best Time to Buy and Sell Stock

    #121   Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price ...

  3. [Swift]LeetCode121. 买卖股票的最佳时机 I | 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. If you were ...

  4. LeetCode121.买卖股票的最佳时机

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...

  5. [leetcode121]股票买卖 Best Time to Buy and Sell Kadane算法

    [题目] Say you have an array for which the ith element is the price of a given stock on day i. If you ...

  6. leetcode121—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. If you were ...

  7. leetcode 买卖股票问题

    leetcode121 Best Time to Buy and Sell Stock 说白了找到最大的两组数之差即可 class Solution { public: int maxProfit(v ...

  8. LeetCode-714.Best Time to Buy and Sell Stock with Transaction Fee

    Your are given an array of integers prices, for which the i-th element is the price of a given stock ...

  9. LeetCode-188.Best Time to Buy and Sell Stock IV

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. plsql中文乱码问题

    问题: 打开 plsql,执行 sql 语句,中文显示乱码: 解决方案: 1)输入 sql 语句 select userenv('language') from dual 查看数据库字符集 ​ 输出结 ...

  2. 第一章:深入web请求过程

    初学者,总结一下读书笔记,望海涵. 1.1 B/S网络架构概述 相比于C/S架构,B/S网络架构(Browser/Server)从前端到后端都得到了简化,都基于统一的应用层协议HTTP来交互数据,采用 ...

  3. input做一个开关按钮

    .mui-switch { width: 52px; height: 31px; position: relative; border: 1px solid #dfdfdf; background-c ...

  4. 非常好的开源C项目tinyhttpd(500行代码)

    编译命令 gcc -W -Wall -lpthread -o httpd httpd.c 源码 #include <stdio.h> #include <sys/socket.h&g ...

  5. 用vs2008打开vs2005项目

    1 使用记事本打开*.sln解决方案文件,将Visual Studio 2005改为Visual Studio 2008 将版本号改为9.00 2 打开扩展名为*.csproj的项目文件,在Defau ...

  6. cocos2dx解决中文乱码方法

    使用plist文件,优点方便做多国语言支持~也不用去做编码转换 1.Resource目录下新建text.plist文件,内容格式如下 <?xml version="1.0" ...

  7. "Error: [$compile:multidir] Multiple directives

    angularjs 中控制台报"Error: [$compile:multidir] Multiple directives错误 可以在当前html元素之上增加父元素,在父元素中加入第二个指 ...

  8. Go Example--defer

    package main import ( "fmt" "os" ) func main() { f := createFile("/tmp/defe ...

  9. oracle12c安装+配置,plsql 13安装+激活

    oracle12c安装下载地址 oracle12c安装安装教程 Oracle 11g R2 Client(64bit)的下载与安装(图文详解) PLSQL Developer 11安装与配置 list ...

  10. Nginx网站实现ssl安全套接字

    nginx.conf配置 server { listen 443 ssl; server_name www.example.com; ssl on; ssl_certificate cert.pem; ...