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.

Have you met this question in a real interview?

Yes
Example

Given array [3,2,3,1,2], return 1.

LeetCode上的原题,请参见我之前的解法Best Time to Buy and Sell Stock

class Solution {
public:
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
int maxProfit(vector<int> &prices) {
int res = , mn = INT_MAX;
for (int i = ; i < prices.size(); ++i) {
mn = min(mn, prices[i]);
res = max(res, prices[i] - mn);
}
return res;
}
};

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

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

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

  6. 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 ...

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

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

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

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

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

随机推荐

  1. cocos2d-js 学习笔记 --安装调试(1)

    目前从国内来看,比较知名的,适合前端程序员使用的游戏框架,基本是 egret 和cocos2d-js;刚好两个框架都学习了下, egret的是最近兴起的一个框架主要使用TypeScript.js语言做 ...

  2. C++内存管理

    1. 栈(Stack):         位于函数内的局部变量(包括函数实参),由编译器负责分配释放,函数结束,栈变量失效.2. 堆(Heap):        由new申请的内存,由delete负责 ...

  3. tp框架实现ajax

    不墨迹,直接进主题. tp框架实现ajax 首先,我们先做一个testajax.html用来显示页面(只是一个简单的下拉列表^_^) <!DOCTYPE html PUBLIC "-/ ...

  4. 基于SSM的租赁管理系统1.0_20161225_框架搭建

    搭建SSM底层框架 1. 利用mybatis反向工程generatorSqlmapCustom完成对数据库十表的映射 generatorConfig.xml <?xml version=&quo ...

  5. 学号20145332 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验

    实验目的 掌握在 ARM 开发板实现一个简单 WEB 服务器的过程. 学习在 ARM 开发板上的 SOCKET 网络编程. 学习 Linux 下的 signal()函数的使用. 实验内容 学习使用 s ...

  6. canvas星星炫耀

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. asp.net core的TagHelper简单使用

    TagHelper(标签助手)是ASP.NET Core非常好的一种新特性.可以扩展视图,让其看起来像一个原生HTML标签. 应该使用TagHelper替换HtmlHelper,因其更简洁更易用,且支 ...

  8. iOS开发学习笔记

    1 常用的第三方工具 1.1 iPhone Simulator 测试程序需要模拟器iPhone Simulator 1.2 设计界面需要Interface Builder,Interface Buil ...

  9. spring spring data jpa save操作事务

    整合spring spring data jpa的时候,在save方法上加了@Transactional注解.此时调用springdatajpa save方法并不会真的把数据提交给数据库,而是缓存起来 ...

  10. 在iIBatis中使用MySql中出现的配置问题

    1.首先需要下载mySQL-connector-net的安装包.可以通过下面两种方式,第一种是需要安装的,第二种是直接可以下载使用的. 第一种: http://dev.mysql.com/downlo ...