本文为senlie原创。转载请保留此地址:http://blog.csdn.net/zhengsenlie

Best Time to Buy and Sell Stock

Total Accepted: 13234 Total
Submissions: 43145

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

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

题意:给定一组数,表示某种股票每天的交易价格。求怎样进行一次买卖。使收益最大。

思路:贪心

买进价越低越好,卖出价越高越好。终于的目的是利润越高越好

设置两个变量,一个表示当前的最低卖出价cur_min_price,还有一个表示当前的最高利润max_profit

遍历数组prices。以当前值为卖出价

更新

max_profit = max(max_profit, prices[i] - cur_min_price)

cur_min_price = min(cur_min_price, prices[i])

复杂度:时间O(n),空间O(1)

相关题目:

Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock III

class Solution {
public:
int maxProfit(vector<int> &prices) {
if (prices.size() < 2) return 0;
int cur_min_price = prices[0];
int max_profit = 0;
for(int i = 1; i < prices.size(); i++){
max_profit = max(max_profit, prices[i] - cur_min_price);
cur_min_price = min(cur_min_price, prices[i]);
}
return max_profit;
}
};

Leetcode 贪心 Best Time to Buy and Sell Stock的更多相关文章

  1. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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

  2. [LeetCode] 121. 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 ...

  3. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

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

  4. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

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

  5. [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

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

  6. [LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期

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

  7. 【LeetCode】Best Time to Buy and Sell Stock IV

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

  8. [Leetcode Week6]Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...

  9. [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III

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

随机推荐

  1. idea常用快捷键(转)

    ---恢复内容开始--- IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表达式时按 “!”键 Ctrl+E,最近的文件 Ctrl+S ...

  2. python 12:list(range(...)) (转化参数列表)

    numbers = list(range(1,11)) #把范围产生的数字串转化为列表储存 print(numbers) 运行结果应该是: [1,2,3,4,5,6,7,8,9,10]

  3. CentOS7 搭建Kafka(三)工具篇

    CentOS7 搭建Kafka(三)工具篇 做为一名懒人,自然不喜欢敲那些命令,一个是容易出错,另外一个是懒得记,能有个工具就最好了,一查还挺多,我们用个最主流的Kafka Manager Kafka ...

  4. Spring的AOP机制---- AOP的注解配置---- AOP的注解配置

    3333隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢 ...

  5. xml之基本操作

    XML:Extensible Markup Language(可扩展标记语言)的缩写,是用来定义其它语言的一种元语言,其前身是SGML(Standard Generalized Markup Lang ...

  6. C#三种创建对象方法所需时间比较。。。。。

    C#创建对象的三种方法  new().Activator.Assembly,接下来通过代码直接来看看运行的速度.... 首先,先看看三种创建对象实例的方法: //new(); public stati ...

  7. Android环境的搭建遇到的问题和解决方案

    安卓环境的搭建,我花了将近一天的时间,在最后终于找到了一个比较好的方案. 第一个问题是安卓的官网(http://developer.android.com)很难登录.SDK和ADT都是需要在官网上下载 ...

  8. 【Oracle】解决oracle sqlplus 中上下左右backspace不能用

    一. 解决输入 BACKSPACE 键变成 ^h 的问题 #su - oracle   $stty erase ^h. 要永久生效,可以加入到用户环境配置文件.bash_profile中(vi .ba ...

  9. UID卡、CUID卡、FUID卡的区别

    UID卡(国外称GEN1) 所有区块可被重复读写 卡片ID可改且使用后门指令更改ID ID可被重复修改 响应后门指令(意味着可被使用后门指令检测是否为克隆卡的机器发现) CUID卡(国外称GEN2) ...

  10. JAVA软件工程师应该具备的技能有哪些?

    前言:有朋友问我:学历和能力哪个重要?我个人觉得能力大于学历,没有能力哪来的学历,学历只是证明能力的一方面.为此在能力方面畅谈java软件工程师必备的能力.作为一名合格的java工程师,不仅需要学历, ...