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 ...
随机推荐
- Python基础之列表
列表的内置方法 1.append() 描述:用于在列表末尾添加新的对象. 示例: msg_list=["aaaa","bbbbb","cccccc&q ...
- VIP站长大会(北京站)常见问题解答
功能支持问题 1. react能否和MIP结合使用,如果暂时不能以后是否有考虑?是否会和其他 js 框架(比如angular )结合? 目前暂无计划支持. 2. MIP页是否支持自定义cookie?实 ...
- FormsCookieName保存登录用户名的使用
一,写一个类来实现 using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...
- @ResponseBody注解与JSON
MappingJacksonHttpMessageConverter 调用了 objectMapper.writeValue(OutputStream stream, Object)方法,使用@Res ...
- 一个数组分四个ul并且每个ul里边有四个li显示
<?php $a = $array; for($i=0;$i<4;$i++ ) {?> <ul class="new-hover"> <?php ...
- VB调用WebService(SOA2.0接口)(直接Post方式)并解析返回的XML
SOA 2.0接口 Function GetDepartmentCode(reqDeptCode) Dim soaRequestXML : soaRequestXML = "" D ...
- div显示与隐藏及height()函数
总结与网络 1. $("#id").show()表示display:block,$("#id").hide()表示display:none; $("# ...
- php笔记(三)PHP类和对象之访问控制
访问控制通过关键字public,protected和private来实现.被定义为公有的类成员可以在任何地方被访问.被定义为受保护的类成员则可以被其自身以及其子类和父类访问.被定义为私有的类成员则只能 ...
- response.setContentType与 request.setCharacterEncoding 区别
1.request.setCharacterEncoding()是设置从request中取得的值或从数据库中取出的值的编码 2.response.setContentType指定 HTTP 响应的编码 ...
- BT 的相关资料
1.Android中bluetooth的架构 http://blog.csdn.net/u011960402/article/details/11035947 2.Android4.0中Bluetoo ...