本文为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. springboot创建项目

    Springboot作为轻量级快速开发受到无数java人的青睐,Spring Boot是为了简化Spring应用的创建.运行.调试.部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过 ...

  2. python利用有道翻译实现“语言翻译器”的功能

    import urllib.request import urllib.parse import json while True: content = input('请输入需要翻译的内容(退出输入Q) ...

  3. Oracle 数据导入导出(imp/exp)

    环境:windows下,oracle11g 1.启动oracle服务 net start OracleDBConsoleorclnet start OracleOraDb11g_home1TNSLis ...

  4. CSS的常用属性(二)

    盒子模型之边框 border-(top/bottom/left/right)-style: solid 边框的风格 如(solid 实线,dotted 点线,dashed 虚线) border-top ...

  5. vsftp服务器搭建

    1.FTP的主动模式和被动模式的区别: 最大的区别是数据端口并不总是20, 主动模式和被动模式的优缺点: 主动FTP对FTP服务器的管理和安全很有利,但对客户端的管理不利.因为FTP服务器企图与客户端 ...

  6. 人工机器:NDC-谷歌机器翻译破世界纪录,仅用Attention模型,无需CNN和RNN

    终于找到ML日报的微信链接,抄之...................................... 请拜访原文链接:[谷歌机器翻译破世界纪录]仅用Attention模型,无需CNN和RNN. ...

  7. 三维重建:SFM中BA的并行化

    1. BA在重建中的作用 借鉴于运动中重建的方法,BA引入SLAM过程,而传统的滤波方法引入BA是跟随闭环检测出现. 1.1 BA在滤波方法中的嵌入 PTAM 1.2 BA在闭环检测之后的应用 在三维 ...

  8. (转) Arcgis for js加载百度地图

    http://blog.csdn.net/gisshixisheng/article/details/44853709 概述: 在前面的文章里提到了Arcgis for js加载天地图,在本节,继续讲 ...

  9. 25-Ubuntu-文件和目录命令-其他命令-重定向

    重定向 Linux允许将命令执行结果重定向到一个文件. 将本应显示到终端上的内容输出或追加到指定文件中. 重定向命令 含义 > 表示输出,会覆盖原有文件. >> 表示追加,会将内容追 ...

  10. [kernel学习]----如何debug kernel

    #ll /sys/kernel/debug/tracing/events/kmem total 0 -rw-r--r-- 1 root root 0 Feb 3 20:17 enable -rw-r- ...