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 ...
随机推荐
- VM虚拟机的配置文件(.vmx)损坏修复
来源://http://blog.csdn.net/houffee/article/details/18398603 VM虚拟机中使用.vmx文件保存虚拟机的所有软硬件配置,如果意外损坏的话将会出现不 ...
- top batch output
echo 3 > sudo tee /proc/sys/vm/drop_caches top -d30 -bn20 > a
- Gs_Class._BaseQueryWeb查询页面基类(aspx.net)
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web; ...
- 1. LAMP----PHP开发环境搭建(Win)
LAMP=Linux+Apache+MySQL+PHP. Step1 安装Apache http://httpd.apache.org/download.cgi 1.打开上面网址->点击File ...
- Java多线程的信号量
Java的信号量主要的作用是控制线程对资源的访问例如我一个线程池里面有100个线程等待执行,但是我允许最多同时运行5个线程,这5个线程只有其中一个线程执行完毕后,在线程池中等待的线程才能进入开始执行, ...
- hdu 2149 Public Sale 简单博弈
Problem Description 虽然不想,但是现实总归是现实,Lele始终没有逃过退学的命运,因为他没有拿到奖学金.现在等待他的,就是像FarmJohn一样的农田生涯.要种田得有田才行,Lel ...
- centos6 搭建hdwiki
前期准备:安装好Mysql+apache+PHP,测试apache能够解析index.php文件后就可以. 用户名 xiaohe 密码 123456 #### mysql安装好后: adduser w ...
- PHP ckeditor富文本编辑器 结合ckfinder实现图片上传功能
一:前端页面代码 <script src="/www/res/ckeditor/ckeditor.js"></script> <textarea id ...
- Request for the permission of type异常
调用wcf调用的时候引发一个错误,错误信息如下: <Message>Request for the permission of type 'System.Configuration.Con ...
- TheFifthWeekText
类的构造方法是当创建对象时,对象自动调用的对对象进行初始化的方法.他没有返回值,而且构造方法名与类名是相同的.如果类中没有定义构造方法,Java编译器在编译时会自动给它提供一个没有参数的默认构造方法, ...