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. C#中IEnumerable和IEnumerator区别

    IEnumerator:是一个真正的集合访问器,提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型.IEnumerable: ...

  2. kubernetes下的Nginx加Tomcat三部曲之三:实战扩容和升级

    本章是<kubernetes下的Nginx加Tomcat三部曲系列>的终篇,今天咱们一起在kubernetes环境对下图中tomcat的数量进行调整,再修改tomcat中web工程的源码, ...

  3. 片段的findFragmentById

    class类名 名字 = calss类名 getFragmentManager().findFragmentById(R.id.布局id) 因为需要获取到片段的管理者,才可以去寻找到相应的布局.

  4. soql取第一件数据

    User u = [select ID,Name from User Limit 1];

  5. table 如何给tr border颜色

    border-collapse属性值 说明 separate 默认值,边框分开,不合并 collapse 边框合并,如果相邻,则共用一个边框 table,th,td{border:1px solid  ...

  6. 蚂蚁金服 Service Mesh 实践探索

    SOFAMesh是蚂蚁金服在ServiceMesh方向上的探索,下面是它高级技术专家敖小剑在QCon上海2018上的演讲. Service Mesh 是一个 基础设施层,用于处理服务间通讯.现代云原生 ...

  7. bzoj2750最短路计数

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2750 枚举每一个起点,通过该边的子树中有多少节点就知道本次它被经过几次了: 因为同一起点到该 ...

  8. vim配置之安装脚本

    vimConfig/install/install.sh git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle cp ...

  9. nginx 点播mp4方法

    1.配置文件 配置文件中rmtp部分: application vod { play html; } 配置文件中http部分: location ~ .mp4$ { root html; mp4; l ...

  10. c端用户体系实现方案

    问题: 同个用户的来源 有微信,脸书,浏览器 怎么鉴定他们为同一个人呢? 解决: 数据库2张表 c_user表 记录c端用户 信息 记录 唯一uuid ,平台来源 手机号码 浏览器进来是匿名用户 无手 ...