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 ...
随机推荐
- Java: MissingResourceException, "Can't find bundle for base name myMessage, locale zh_CN"
在java中,在多语言国际化时可以用 *.java 类来作为资源文件使用. 1. 首先定义类, 类必须继承ListResourceBundle 类所在路径为: src/I18N public clas ...
- Objective-C Runtime 运行时之四:Method Swizzling(转载)
理解Method Swizzling是学习runtime机制的一个很好的机会.在此不多做整理,仅翻译由Mattt Thompson发表于nshipster的Method Swizzling一文. Me ...
- Unity3DGUI:GUILayout
显示效果,注意GUILayout控件默认垂直布局,且在水平布局模块里控件大小默认按控件内容来显示,因此对于水平滑块HorizontalSlider来说需要自定义大小避免变形
- 【转】delphi Format格式化函数
转自:http://www.cnblogs.com/mumble/archive/2011/05/25/2056462.html Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助 ...
- jsp中,对window对象的简单总结
window的对象有: alert(message) 弹出一个警示对话框confirm(message) 弹出一个确认对话框prompt(message,defaultmessage) 弹出一个提示对 ...
- underScore学习1:匿名函数中call(this)的作用
匿名函数中(function(){}).call(this) 中的.call(this) 有什么用? 我们都知道,.call()可以改变函数执行时的context,即this的指向,源码中的.call ...
- 浏览器exp使用经验
0x00背景 windows平台下,浏览器安全是绕不过的话题,其涉及的安全问题涵盖二进制和web,攻击场景也非常多样化: 用户点击攻击者的恶意URL链接被感染恶意代码 访问恶意站点被绕过同源策略窃取c ...
- Using YARN with Cgroups testing in sparkml cluster
部署服务器: sparkml 集群 ########### sparkml ########## sparkml-node1 # yarn resource manager sparkml-node2 ...
- 对dpkg: error processing package xxx (--configure) 的处理
这个问题是在在安装Deepdive时候碰到的,具体来说在执行 sudo apt-get update碰到的sudo mv /var/lib/dpkg/info/ /var/lib/dpkg/info_ ...
- 【第五篇】Volley代码修改之图片二级缓存以及相关源码阅读(重写ImageLoader.ImageCache)
前面http://www.cnblogs.com/androidsuperman/p/8a157b18ede85caa61ca5bc04bba43d0.html 有讲到使用LRU来处理缓存的,但是只是 ...