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 ...
随机推荐
- python面向对象封装案例(附:is和==的区别)
面向对象封装案例 目标 封装 小明爱跑步 存放家具 01. 封装 封装 是面向对象编程的一大特点 面向对象编程的 第一步 —— 将 属性 和 方法 封装 到一个抽象的 类 中 外界 使用 类 创建 对 ...
- 线性表seqList类及其父类list,模板类
seqList模板类,线性表代码 # include "list.h" //代码清单2-2 顺序表类的定义和实现 // The Definition of seqList temp ...
- LINUX系统配置
LINUX系统配置 Linux 安装jdk方法; Linux Tomcat 安装与配置 Linux redis 安装与配置 (例1) Linux redis安装配置(例2) NGINX 安装 Linu ...
- c++ 11 移动语义
C++ 已经拥有了拷贝构造函数, 和赋值函数,它们主要定位为浅和深度拷贝, 新增加一个移动构造函数,主要避免拷贝构造. 在定义了移动构造函数的情况下,在实参(argument)是一个右值(rvalue ...
- “必须执行Init_Clk函数,才能采集到二氧化碳接口485数据的问题”的解决
这个问题困扰了我很长一段时间,而且如果这个问题不解决,就有一个无法调和的矛盾:执行Init_Clk函数,能采集到二氧化碳接口485数据,但是功耗大:不执行Init_Clk函数,不能采集到二氧化碳接口4 ...
- spring ico
代码非常简单.如果缺少jar包将导致报错. 需要5个spring jar包和1个logging jar,否则报错. org.springframework.asm.jarorg.springframe ...
- supervisord.conf
; Sample supervisor config file.;; For more information on the config file, please see:; http://supe ...
- 20155208徐子涵 《网络对抗》Exp1 PC平台逆向破解
20155208徐子涵 <网络对抗>Exp1 PC平台逆向破解 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数 ...
- 14.python-CS编程
一.客户端/服务器架构1.C/S架构:(1)硬件C/S架构(打印机)(2)软件C/S架构(web服务)2.生活中的C/S架构:饭店是S端,所有食客是C端3.C/S架构与socket的关系:socke就 ...
- ios-项目启动页面
项目运行启动页面: 点工程项目targets-(或Images.xcasets)-LaunchImage(iphone四种规格图片:320*480/350*568/640*960/640*1136)将 ...