【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  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.

题目大意

  给一个数组prices[],prices[i]代表股票在第i天的售价,求出仅仅做一次交易(一次买入和卖出)能得到的最大收益。

解题思路

  仅仅须要找出最大的差值就可以。即 max(prices[j] – prices[i]) ,i < j。一次遍历就可以,在遍历的时间用遍历low记录 prices[o….i] 中的最小值,就是当前为止的最低售价,时间复杂度为 O(n)。

代码实现

算法实现类

public class Solution {

    public int maxProfit(int[] prices) {

        if (prices == null || prices.length < 1) {
return 0;
} int min = prices[0];
int profit = 0; // 第i天的价格能够看作是买入价也能够看作是卖出价
for (int i = 1; i < prices.length; i++) {
// 找到更低的买入价
if (min > prices[i]) {
// 更新买入价
min = prices[i];
}
// 当天的价格不低于买入价
else {
// 假设当天买出的价格比之前卖出的价格高
if (profit < prices[i] - min) {
// 更新卖出价
profit = prices[i] - min;
}
}
} return profit;
}
}

评測结果

  点击图片。鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47651235

【LeetCode-面试算法经典-Java实现】【121-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] 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 ...

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

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

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

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

  7. [Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机

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

  8. [Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机

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

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

随机推荐

  1. BZOJ 3143 高斯消元+贪心....

    思路: 先算一下每条边经过次数的期望 转化为每个点经过次数的期望 边的期望=端点的期望/度数 统计一下度数 然后高斯消元 贪心附边权--. //By SiriusRen #include <cm ...

  2. BZOJ 2424 DP OR 费用流

    思路: 1.DP f[i][j]表示第i个月的月底 还剩j的容量 转移还是相对比较好想的-- f[i][j+1]=min(f[i][j+1],f[i][j]+d[i]); if(j>=u[i+1 ...

  3. 机器学习规则:ML工程最佳实践----rules_of_ml section 1【翻译】

    作者:黄永刚 机器学习规则:ML工程最佳实践 本文旨在指引具有机器学习基础知识的工程师等人,更好的从机器学习的实践中收益.介绍一些应用机器学习需要遵循的规则,类似于Google C++ 风格指南等流行 ...

  4. 《剑指offer》矩形覆盖

    一.题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 二.输入描述 输入n 三.输出描述 输出有多少种不同的覆 ...

  5. c# protected public private internal

    1 internal 只能在一个项目中引用,不能跨项目引用,只有在同一程序集的文件中 2 public 最高级别的访问权限 对访问公共成员没有限制 3 private 最低级别的访问权限 只能在声明它 ...

  6. 手把手教你进行R语言的安装及安装过程中相关问题解决方案

    这篇文章旨在为R语言的新手铺砖引路,行文相对基础,希望对在R语言安装上有问题的小伙伴提供帮助和指引.一.什么是 R 语言R 编程语言被广泛应用在统计科学和商业领域. 在各种编程语言排名中 R 语言的排 ...

  7. 操作系统——第五章 输入输出(I/O)管理

    这就是SDT表和DCT表

  8. Http协议与TCP协议理解(转载的)

    TCP协议对应于传输层,而HTTP协议对应于应用层,从本质上来说,二者没有可比性.Http协议是建立在TCP协议基础之上的,当浏览器需要从服务器获取网页数据的时候,会发出一次Http请求.Http会通 ...

  9. bzoj2763 [JLOI]飞行路线 分层图最短路

    问题描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...

  10. [洛谷P1939]【模板】矩阵加速(数列)

    题目大意:给你一个数列a,规定$a[1]=a[2]=a[3]=1$,$a[i]=a[i-1]+a[i-3](i>3)$求$a[n]\ mod\ 10^9+7$的值. 解题思路:这题看似是很简单的 ...