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.

 
Example

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

题意

假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格。如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润。

解法一:

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

参考@NineChapter 的代码

解法二:

 public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
} int min = Integer.MAX_VALUE; //just remember the smallest price
int profit = 0;
for (int i : prices) {
min = i < min ? i : min;
profit = (i - min) > profit ? i - min : profit;
} return profit;
}
}

参考@NineChapter 的代码

149. Best Time to Buy and Sell Stock【medium】的更多相关文章

  1. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

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

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

  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 II 买股票的最佳时间之二

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

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

  9. LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)

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

随机推荐

  1. MISRA 2004 VS MISRA 2012

  2. std::move的实际工作过程

    std::move的定义如下: template <typename T> typename remove_reference<T>::type && move ...

  3. maven 创建web项目的标准目录结构

      maven 创建web项目的标准目录结构 CreateTime--2018年4月18日21:05:37 Author:Marydon 1.标准目录介绍(开发目录) 2.在eclipse下,目录展示 ...

  4. Java编程思想(十五) —— 类型信息之反射

    讲完.class,Class之后,继续. 1)泛化的Class引用 Class也能够增加泛型,增加之后会进行类型检查. 贴一下书上原话,Class<?>优于Class,尽管他们是等价的,C ...

  5. python 2.7疑难问题之 编码

    #http://www.cnblogs.com/bluescorpio/p/4303656.html •在遇到错误提示时,注意查看错误提示内容,同时注意查看type类型. 1.TypeError: d ...

  6. 图文例解C++类的多重继承与虚拟继承

    文章导读:C++允许为一个派生类指定多个基类,这样的继承结构被称做多重继承. 在过去的学习中,我们始终接触的单个类的继承,但是在现实生活中,一些新事物往往会拥有两个或者两个以上事物的属性,为了解决这个 ...

  7. 微信小程序用户数据解密

    概述 通过微信web开发者工具创建登录,获取用户信息,发送至后台,进行用户数据解密 详细 代码下载:http://www.demodashi.com/demo/10705.html 一.准备工作 1. ...

  8. 关于iOS 热更新(热修复)你必须知道的一种方法- JSPatch

    本文为转载文章 . 版权归原文所有. 原文链接:iOS 5分钟集成热修复(JSPatch) 前言 在iOS中有很多种热修复方案,在这里我就不一一介绍了 这里有一篇介绍热修复的文章:iOS中的HotFi ...

  9. Foundations of Machine Learning: The Margin Explanation for Boosting's Effectiveness

    Foundations of Machine Learning: The Margin Explanation for Boosting's Effectiveness 在这一节,我们要回答的一个问题 ...

  10. Swift和Objective-C混编的注意啦

    文/仁伯安(授权) 原文链接:http://www.jianshu.com/p/2ed48b954612 前言 Swift已推出数年,与Objective-C相比Swift的语言机制及使用简易程度上更 ...