【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
【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(最佳买卖股票的时间)】的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- Redis的好处知识
参考文章 http://www.cnblogs.com/wupeiqi/articles/5132791.html 使用Redis有哪些好处? () 速度快,因为数据存在内存中,类似于HashMap, ...
- BZOJ 4551 HEOI 2016 树 (并查集)
思路: 考虑时光倒流 这不就是并查集裸题了-----. //By SiriusRen #include <cstdio> #include <cstring> #include ...
- Kafka框架基础
* Kafka框架基础 官网:kafka.apache.org 框架简介 Apache Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为Apache项目的一部分.Kaf ...
- 无滚动条GridView少量图片展示
import android.content.Context; import android.util.AttributeSet; import android.util.Log; import an ...
- Spring MVC登录注册以及转换json数据
项目结构; 代码如下: BookController package com.mstf.controller; import javax.servlet.http.HttpServletRespons ...
- 洛谷 P1368 工艺 后缀自动机 求最小表示
后缀自动机沙茶题 将字符串复制一次,建立后缀自动机. 在后缀自动机上贪心走 $n$ 次即可. Code: #include <cstdio> #include <algorithm& ...
- iOS开发—— Couldn't add the Keychain Item
报错:*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn ...
- HNU 12886 Cracking the Safe 二十四点的判断
经典的一个题,今天竟然写跪了…… 题意: 给你4个数字,让你判断是否能通过四则运算和括号,凑成24点. 思路: 暴力枚举运算顺序和运算符. 代码: #include <iostream> ...
- ECNUOJ 2143 端午节快乐
端午节快乐 Time Limit:1000MS Memory Limit:65536KBTotal Submit:1720 Accepted:868 Description 有一段有趣的传说.公元前 ...
- MethodFilterInterceptor(方法拦截器)配置excludeMethors
由于该类有setExcludeMethods方法,因此在xml中可以配置一个excludeMethods参数 刚开始老是拦截不成功,tomcat显示这个参数没找到,后来终于找到错误:不应该在拦截器栈中 ...