LeetCode OJ 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 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.
【思路】
我么知道,如果要获得利润,售出的价格必须要高出买入的价格,而且出售的日期要大于买入的日期。因此我们在最低时候买入,在最高的时候卖出。从第一天开始,吧第一天的价格设置为当前最低价格,如果后一天的价格高于最低价,我们就记录此时的价格差,如果此时的利润大于之前的最高利润则把它设置为当前最大利润,然后下标向后移动。如果有一天的价格低于当前最低价格,我们就把当前价格设置为最低价格,然后继续上述过程,直到遍历到数组末尾。这个过程其实是把数组分为一段一段,每一段的开始都是这一段范围的内的最低值。举个例子:[2,24,4,1,3,7]红色的一段中2是最小值,24是最大值。绿色的一段中1是最小值,7是最大值。
代码如下:
public class Solution {
public int maxProfit(int[] prices) {
int min = 0;
int mp = 0; for (int i = 1; i < prices.length; i++) {
if (prices[min] > prices[i]) min = i;
else mp = Math.max(mp, prices[i] - prices[min]);
} return mp;
}
}
LeetCode OJ 121. Best Time to Buy and Sell Stock的更多相关文章
- 【LeetCode OJ】Best Time to Buy and Sell Stock III
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ Linear Time Solut ...
- LeetCode OJ 123. 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 ...
- 【刷题-LeetCode】121 Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- 【LeetCode OJ】Best Time to Buy and Sell Stock II
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ We solve this prob ...
- 【LeetCode OJ】Best Time to Buy and Sell Stock
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/ We solve this problem ...
- 【一天一道LeetCode】#121. Best Time to Buy and Sell Stock
# 一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say ...
- 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...
- LeetCode OJ 122. 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 ...
- LeetCode OJ: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 ...
随机推荐
- Unity3D脚本使用:游戏对象访问
Unity3D中用到的组件 组件在js中对应的对象 使用如图: 注意:一个物体可以添加多个组件和多个js 同个物体上添加的js间引用
- CodeForces 708B Recover the String
构造. 根据$a[0][0]$可以求得$0$的个数$p$,根据$a[1][1]$可以求得$1$的个数$q$. 如果找不到$p$或$q$,那么就无解. 每一个$0$放到序列中的任何一个位置,假设和前面的 ...
- C#隐藏tabcontrol
//tabControl1.SizeMode = TabSizeMode.Fixed; //tabControl1.ItemSize = new Size(0, 1);
- Office下载地址
文件名cn_office_professional_plus_2016_x86_x64_dvd_6969182.isoSHA1277926A41B472EE38CA0B36ED8F2696356DCC ...
- Lua math库
函数名 描述 示例 结果 pi 圆周率 math.pi 3.1415926535898 abs 取绝对值 math.abs(-2012) 2012 ceil 向上取整 math.ceil(9.1) 1 ...
- maven 项目 pom.xml文件中配置的jar包下载报错
[ERROR] [ERROR] Some problems were encountered while processing the POMs:[ERROR] 'dependencies.depen ...
- Comparer<T> IComparer<T> IComparable<T>
Comparer<T>.Default Property Comparer<T>.Default doesn't use your FooComparer class. It ...
- 【转】Freemarker输出$和html标签等特殊符号
原文:http://blog.csdn.net/achilles12345/article/details/41820507 场景:程序员都不喜欢看文档,而更喜欢抄例子.所以,我们把平台组的组件都做成 ...
- 变更mysql数据库文件目录 Linux
本次需要将mysql默认的数据库文件路径/var/lib/mysql 改为新挂载的目录/data/mysql上,需要做以下修改 1.停止mysql服务 service mysqld stop 2.复制 ...
- ajax对文件上传
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...