Level:

​ ​ Easy

题目描述:

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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.

Example 2:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

思路分析:

​​ ​ 遍历数组,保存当前遇到的最小元素minval,然后计算访问到的元素与其差值,保留遇到的最大差值,就是结果。

代码:

class Solution {
public int maxProfit(int[] prices) {
int minVal=Integer.MAX_VALUE;
int difference=0;
for(int i=0;i<prices.length;i++){
minVal=Math.min(prices[i],minVal);
difference=Math.max(difference,prices[i]-minVal);
}
return difference;
}
}

3.Best Time to Buy and Sell Stock(买卖股票)的更多相关文章

  1. [LeetCode] 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 ...

  2. [LintCode] 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 ...

  3. [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 ...

  4. 121. Best Time to Buy and Sell Stock买卖股票12

    一 [抄题]: If you were only permitted to complete at most one transaction (ie, buy one and sell one sha ...

  5. [Leetcode] Best time to buy and sell stock 买卖股票的最佳时机

    Say you have an array for which the i th element is the price of a given stock on day i. If you were ...

  6. LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)

    题意:给定一个序列,第i个元素代表第i天这支股票的价格,问在最佳时机买入和卖出能赚多少钱?只买一次,且仅1股,假设本钱无限. 思路:要找一个最低价的时候买入,在最高价的时候卖出利润会最大.但是时间是不 ...

  7. 121. Best Time to Buy and Sell Stock 买卖股票的最佳时机

    网址:https://leetcode.com/problems/Best-Time-to-Buy-and-Sell-Stock/ 第一想法是滑动窗口法,稍微尝试后发现不可行,至少我不会... 而后想 ...

  8. 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 ...

  9. 【leetcode】121. Best Time to Buy and Sell Stock(股票问题)

    You are given an array prices where prices[i] is the price of a given stock on the ith day. You want ...

  10. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. 生产环境该如何选择lvs的工作模式,和哪一种算法

    lvs的工作模式有这几种: 1.RR : 轮叫算法,平均分配,你一个,我一个: 2.WRR :加权轮叫算法,谁的处理能力强,谁的权重就高: 3.LC :最少链接算法,谁的连接数最少,谁就处理更多的链接 ...

  2. C# 正规表达式

    在C#中怎么用正则表达式限制文本框内不能输入数字?只能输入数字:"^[0-9]*$".只能输入n位的数字:"^\d{n}$".只能输入至少n位的数字:" ...

  3. C#高级参数ref的使用

    ref关键字用于将方法内的变量改变后带出方法外.具体我们通过例子来说明: 例子中,将变量n1和n2交换了.如果没有加ref参数,由于没有swadDemo()方法没有返回值,调用后,n1和n2是不会交换 ...

  4. C#windows窗体应用程序如何自适应大小

    用C#的windows窗体应用程序做界面十分轻松,但是系统默认是没有让控件跟随窗体的大小改变而已改变的.所以需要我们手动去设置让窗体控件随着窗体的大小改变而改变.所以我们只需要将控件选择 然后把Anc ...

  5. 【259】ucpole.dat update

    2017年2月21日 57871 +0.020896 0.007232 +0.414732 0.009212 +0.418044 0.007533 p 57872 +0.022055 0.007284 ...

  6. Struts2框架02 消息传递

    1 消息传递 浏览器和服务器之间的数据传递 2 服务器项浏览器发送数据 2.1 在控制器类中增加一个属性,该属性的值就是服务器需要向浏览器发送的数据 2.2 为该属性增加 get 方法 2.3 在处理 ...

  7. leetCode编程题

    已知链表1->2->3->4,先需要删除3这个节点,请完成函数.注意,这里只给定要删除的节点3,并不知道3之前的节点是哪个,以及整个链表是什么节点. void delete(List ...

  8. 操作系统 Linux ex1 note

    ctrl + alt + T 命令行 ctrl + alt + F7 ctrl + alt + F1-6 ls 列出所有文件 / 根目录 ~ /home/username cd 切换路径 . 当前目录 ...

  9. Java 分析模板方法设计模型

    http://www.cnblogs.com/maowang1991/archive/2013/04/15/3023236.html //父类 abstract class Operate{ prot ...

  10. Android之ContextMenu的使用方法以及与OptionMenu的区别(转)

    >> ContextMenu是android的context menu上下文菜单,选择某项VIEW后长按menu键,就会显示出来.比如EditeText就可以通过长按来弹出拥有“cut”, ...