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.

这道题相当简单,感觉达不到Medium的难度,只需要遍历一次数组,用一个变量记录遍历过数中的最小值,然后每次计算当前值和这个最小值之间的差值最为利润,然后每次选较大的利润来更新。当遍历完成后当前利润即为所求,代码如下:

C++ 解法:

class Solution {
public:
int maxProfit(vector<int>& prices) {
int res = , buy = INT_MAX;
for (int price : prices) {
buy = min(buy, price);
res = max(res, price - buy);
}
return res;
}
};

Java 解法:

public class Solution {
public int maxProfit(int[] prices) {
int res = 0, buy = Integer.MAX_VALUE;
for (int price : prices) {
buy = Math.min(buy, price);
res = Math.max(res, price - buy);
}
return res;
}
}

类似题目:

Best Time to Buy and Sell Stock with Cooldown

Best Time to Buy and Sell Stock IV

Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock II

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间的更多相关文章

  1. [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 ...

  2. [LintCode] 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] Best Time to Buy and Sell Stock 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] Best Time to Buy and Sell Stock 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] Best time to buy and sell stock 买卖股票的最佳时机

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

  6. LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)

    题意:给定一个序列,第i个元素代表第i天这支股票的价格,问在最佳时机买入和卖出能赚多少钱?只买一次,且仅1股,假设本钱无限. 思路:要找一个最低价的时候买入,在最高价的时候卖出利润会最大.但是时间是不 ...

  7. [LintCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

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

  8. 121. Best Time to Buy and Sell Stock买卖股票12

    一 [抄题]: If you were only permitted to complete at most one transaction (ie, buy one and sell one sha ...

  9. 121. Best Time to Buy and Sell Stock 买卖股票的最佳时机

    网址:https://leetcode.com/problems/Best-Time-to-Buy-and-Sell-Stock/ 第一想法是滑动窗口法,稍微尝试后发现不可行,至少我不会... 而后想 ...

随机推荐

  1. TCP四步挥手的各种状态转换图

    对于TCP四步挥手时的各种状态转换,网上有很多资料.但是有很多描述不是很容易理解,甚至是描述错误,不如这篇文章里http://www.cnblogs.com/Jessy/p/3535612.html# ...

  2. ASP.NET Core 中文文档 第四章 MVC(2.3)格式化响应数据

    原文:Formatting Response Data 作者:Steve Smith 翻译:刘怡(AlexLEWIS) 校对:许登洋(Seay) ASP.NET Core MVC 内建支持对相应数据( ...

  3. 从express源码中探析其路由机制

    引言 在web开发中,一个简化的处理流程就是:客户端发起请求,然后服务端进行处理,最后返回相关数据.不管对于哪种语言哪种框架,除去细节的处理,简化后的模型都是一样的.客户端要发起请求,首先需要一个标识 ...

  4. Hive安装部署

    目录 一.        安装Hive. 1 1.       选择CDH版本的... 1 2.       解压文件... 1 二.        配置Hive. 1 1.       配置环境变量 ...

  5. alias拦截器的使用

    在SSH项目中,有时需要由一个Action跳转到另一个Action.有两种方式可以实现Action之间的跳转,一种是chain,另一种是redirectAction,这两种方式之间的区别是chain是 ...

  6. Amazon Interview | Set 27

    Amazon Interview | Set 27 Hi, I was recently interviewed for SDE1 position for Amazon and got select ...

  7. 时隔两个月再写的Echarts(Enterprise Charts,商业级数据图表)一文

    简介 ECharts,缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10 ...

  8. Hibernnate延迟加载策略(这么详细你还看不懂)

    好久没有认真写过博客了,今天就好好的写一篇吧!!!!!!!!! 当Hibernate 从数据库中加载某个对象(例如:Dept对象)时,如果同时自动加载所有的关联的某个对象(例如:Emp对象),而程序实 ...

  9. 很强大的HTML+CSS+JS面试题(附带答案)

    一.单项选择(165题) 1.HTML是什么意思? A)高级文本语言 B)超文本标记语言 C)扩展标记语言 D)图形化标记语言 2.浏览器针对于HTML文档起到了什么作用? A)浏览器用于创建HTML ...

  10. Java学习-序列化

    参考资料: http://www.2cto.com/kf/201405/305380.html http://www.cnblogs.com/xdp-gacl/p/3777987.html   序列化 ...