leetcode121
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的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- LeetCode121.买卖股票的最佳时机
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...
- [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 ...
- 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 ...
- leetcode 买卖股票问题
leetcode121 Best Time to Buy and Sell Stock 说白了找到最大的两组数之差即可 class Solution { public: int maxProfit(v ...
- 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 ...
- 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 ...
随机推荐
- java中的线程问题(二)——线程的创建和用法。
在java中一个类要当作线程来使用有两种方法. 1.继承Thread类,并重写run函数 2.实现Runnable接口,并重写run函数 因为java是单继承的,在某些情况下一个类可能已经继承了某个父 ...
- SharePoint Framework Extensions GA Release
博客地址:http://blog.csdn.net/FoxDave SharePoint Framework Extensions GA版本已经发布了,介于最近个人工作的变动调整,还没时间好好了解一下 ...
- the principle of redbalck tree
- (转)junit简介
什么是单元测试? 所谓单元测试是测试应用程序的功能是否能够按需要正常运行,并且确保是在开发人员的水平上,单元测试生成图片.单元测试是一个对单一实体(类或方法)的测试.单元测试是每个软件公司提高产品质量 ...
- Go语言for循环语句
for循环语句解释与使用 首先我们定义sum:=0 格式:for i:=1;i<=100;i++{ sum+=i } for 循环100次,首先执行i=1,然后判断i<100,再执行sum ...
- 利用开源项目jadx反编译Android应用
原文转自:http://bbs.itheima.com/thread-200475-1-1.html 利用开源项目jadx反编译Android应用 利用Github开源项目jadx可以直接对 .dex ...
- 定时任务BlockingScheduler
def task(): current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') print(current_time) ...
- Java虚拟机的内部体系结构
1.Java程序执行流程 Java程序的执行依赖于编译环境和运行环境.源码代码转变成可执行的机器代码,由下面的流程完成: Java技术的核心就是Java虚拟机,因为所有的Java程序都在虚拟机上运行. ...
- spring @Autowired注入对象,在构造方法中为null问题
出现问题的代码如下: @Service public class BaseHttpServiceImpl implements BaseHttpClient { private final stati ...
- sourcetree file status checkbox gone (文件状态下的勾选文件 list 消失)
原来是这样的(sourcetree 版本 2.7.1) 不知道触发了什么条件,sourcetree 变成了以下状态(官方解释是拖动面板小于 1 px 后会导致这个问题) 中间的那一列可勾选的已修改的文 ...