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.

Solution 1:

min记录最小买入价

maxProfit记录最大利润

遍历array,不断更新最小买入价,计算更新最大利润

 public class Solution {
public int maxProfit(int[] prices) {
if (prices == null) {
return 0;
} int maxProfit = 0;
int minValue = Integer.MAX_VALUE; for (int i: prices) {
minValue = Math.min(minValue, i);
maxProfit = Math.max(maxProfit, i - minValue);
} return maxProfit;
}
}

GitHub代码链接

LeetCode: Best Time to Buy and Sell Stock 解题报告的更多相关文章

  1. 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...

  2. LeetCode:Best Time to Buy and Sell Stock I II III

    LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...

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

  4. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

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

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

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

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

  8. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

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

  9. LeetCode Best Time to Buy and Sell Stock IV

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...

随机推荐

  1. Java and C# Comparison

    原文:http://www.harding.edu/fmccown/java_csharp_comparison.html Java Program Structure C# package hell ...

  2. 对于“Newtonsoft.Json”已拥有为“NETStander.Library”定义的依赖项,解决办法

    问题描述: 在使用visual studio中的NuGet包管理下载程序时,有时会出现-对于“Newtonsoft.Json”已拥有为“NETStander.Library”定义的依赖项,这样的错误. ...

  3. 【MySQL】MySQL的索引

    索引是存放在模式中的一个数据库对象,虽然索引总是从属于数据表,但它也和数据表一样属于数据库对象.创建索引的唯一作用就是加速对表的查询,索引通过使用快速路径访问方法来快速定位数据,从而减少了磁盘的I/O ...

  4. Swift 对象

    1.对象 对象是类的具体化的东西,从抽象整体中具体化出的特定个体. 对象是一个动态的概念. 每一个对象都存在着有别于其他对象的属于自己的独特属性和行为. 对象的属性可以随着他自己的行为的变化而改变. ...

  5. Runway for Mac(UML 流程图绘图工具)破解版安装

    1.软件简介    Runway 是 macOS 系统上一款强大实用的软件开发工具,Runway for Mac 是一个界面简单功能强大的UML设计师.此外,Runway for Mac 带给你所有你 ...

  6. 禁用gridview,listview回弹或下拉悬停

    不同的安卓厂商对ListView或ScrollView都做了一些动画效果,比如下拉时为了产生弹性美感而有大幅度回弹效果,再比如魅族的下拉悬停,有时做了一个下拉刷新的功能会与之冲突.其实该美化实为多此一 ...

  7. android 4.4 支持透明状态栏和透明导航栏

    @Override protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceSt ...

  8. 简单获取cpu使用率,以及后台运行的问题

    做了一个运维平台,有一个功能定时执行一个脚本,获取cpu使用率和内存使用情况到监控平台. 获取cpu使用率使用的是top中的信息.直接运行没有问题.通过nohup xxx.sh & 之后获取不 ...

  9. Oracle 12C -- ADR结构

    ADR路径由参数diagnostic_dest参数决定: SQL> show parameter diagnostic_dest NAME TYPE VALUE ---------------- ...

  10. stm32定时器PWM模式和输出比较模式

    pwm模式是输出比较模式的一种特例,包含于输出比较模式中 /** @defgroup TIM_Output_Compare_and_PWM_modes * @{ */ #define TIM_OCMo ...