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.

题目

给定一系列股价,允许买卖各一次。求最大收益。

思路

扫一遍input array,对于每个price: 更新当前minPrice, 更新当前maxProfit

代码

 class Solution {
public int maxProfit(int[] prices) {
if(prices.length < 2) return 0;
int maxProfit = 0;
int minPrice = prices[0];
for(int price : prices){
minPrice = Math.min(minPrice, price);
maxProfit = Math.max(maxProfit, price - minPrice);
}
return maxProfit;
}
}

[leetcode]121. Best Time to Buy and Sell Stock 最佳炒股时机的更多相关文章

  1. 30. leetcode 121. Best Time to Buy and Sell Stock

    121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  2. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

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

  5. Java for 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 ...

  6. leetcode 121. Best Time to Buy and Sell Stock ----- java

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

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

  8. Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)

    题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...

  9. LeetCode 121. Best Time to Buy and Sell Stock (stock problem)

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

随机推荐

  1. 关于protel 99se 汉化后某些菜单消失的解决方法

    本人在使用protel 99se 画PCB时,遇到了好些问题,通过网上查资料基本都解决了. 下面给大家分享 关于protel 99se 汉化后某些菜单消失的解决方法. 其他的许多看不见的菜单也可以自己 ...

  2. EasyNVR H5无插件RTSP直播方案在Windows server 2012上修复无法定位GetNumaNodeProcessorMaskEx的问题

    今天遇到一个客户在使用EasyNVR无插件安防直播解决方案的时候,在Windows Server 2012上出现一个问题提示: 经过反复的查找,虽然提示上显示问题出在KERNEL32.dll上,但是已 ...

  3. SVN的 安装

    关于svn的安装,如图分别是服务端和客户端 下载地址:http://subversion.apache.org/packages.html 1.双击VisualSVN,点下一步,选择目录,就可以,很简 ...

  4. 经典排序方法 python

    数据的排序是在解决实际问题时经常用到的步骤,也是数据结构的考点之一,下面介绍10种经典的排序方法. 首先,排序方法可以大体分为插入排序.选择排序.交换排序.归并排序和桶排序四大类,其中,插入排序又分为 ...

  5. Inno Setup 软件封装

    例子: ; 脚本用 Inno Setup 脚本向导 生成. ; 查阅文档获取创建 INNO SETUP 脚本文件的详细资料! #define MyAppName "隧道照明系统客户端程序&q ...

  6. cobbler装机错误--Failed to create kernel channel,-22

    最近使用cobbler安装GPU的机器遇到了一个错误:Failed to create kernel channel,-22 经过各种搜索发现是英伟达的显卡与centos7的默认显卡驱动冲突导致. 网 ...

  7. 《DSP using MATLAB》第2章习题Problem2.1

    1.代码: %% ------------------------------------------------------------------------ %% Output Info abo ...

  8. os、os.path、shutil操作文件和文件路径的常用方法总结

    os模块是python标准库中的一个用于访问操作系统功能的模块,下面简要介绍一下常用的命令 1.os.name().    判断现在正在使用的平台,windows返回’nt’,Linux返回‘posi ...

  9. 在服务器上运行db:seed数据填充时,出错的问题解决

    在服务器上运行db:seed数据填充时,出错的问题解决 运行composer  dump-autoload

  10. Android中关于JNI 的学习(一)对于JNIEnv的一些认识

    一个简单的样例让我们初步地了解JNI的作用.可是关于JNI中的一些概念还是需要了解清楚,才干够更好的去利用它来实现我们想要做的事情. 那么C++和Java之间的是怎样通过JNI来进行互相调用的呢? 我 ...