题目:

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.

分析:



思路:贪心算法找出股票最低用来计算最大收益。同一时候保存眼下为止的最大收益,并依据产生的新的最大收益是否是最大来进行更新。



代码:

public class Solution {
    public int maxProfit(int[] prices) {
        int lowest = 0;
        int maxProfit = 0;
        if(prices.length > 0){
            lowest = prices[0];
            for(int i = 0; i < prices.length; i++){
                if(lowest > prices[i]){
                    lowest = prices[i];
                }
                maxProfit = Math.max(maxProfit, prices[i] - lowest);
            }
        }
        return maxProfit;
    }
}

PS:

Math.max(val1, val2);用来比較val1和val2的大小

Leetcode-Best Time to Buy and Sell Stock -java的更多相关文章

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

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

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

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

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

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

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

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

  9. [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费

    Your are given an array of integers prices, for which the i-th element is the price of a given stock ...

  10. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)

    先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...

随机推荐

  1. Laravel源码解析之从入口开始

    前言 提升能力的方法并非使用更多工具,而是解刨自己所使用的工具.今天我们从Laravel启动的第一步开始讲起. 入口文件 laravel是单入口框架,所有请求必将经过index.php define( ...

  2. webpack不打包指定的js文件

    背景: 在项目实际开发中,有一些IP地址需要随时修改,进行部署,例如websocket的地址.因此在项目打包的时候,不希望保持IP地址的文件被打包,因此就需要把需要修改的常量独立出来,存放在一个js文 ...

  3. 2019-03-18 使用Request POST获取CNABS网站上JSON格式的表格数据,并解析出来用pymssql写到SQL Server中

    import requests import pymssql url = 'https://v1.cn-abs.com/ajax/ChartMarketHandler.ashx' headers = ...

  4. cocos2d-js 热更新具体解释(一)

    本文将会具体解说cocos2d-js下的热更新机制.这篇内容先给大家介绍一下两个manifest文件就当热身了. 首先介绍project.manifest:  举个样例 { "package ...

  5. 从QQ聊天看交流的有效性

    首先让我们看一则约10分钟的QQ群聊天记录.截图例如以下.已经进行了隐私保护. 交流的主体为大二的在校生与刚刚毕业的学长之间的对话,学长參加过培训,在校学弟想了解一下.故有了以下的交流.(从上到下,从 ...

  6. updatefile.sh - Linux下代码更新脚本

    以下写的是一个关于文件上传的代码shell脚本 该篇文章主要有下面几个方面的考虑: 1.文章主要用于在Linux下代码包批量上传: 2.将被覆盖的代码备份做备份,用于兴许做问题查看或者代码的回退(回退 ...

  7. linux进程控制函数详解

    进程控制 fork函数 创建一个子进程. pid_t fork(void); 失败返回-1:成功返回:① 父进程返回子进程的ID(非负) ②子进程返回 0 pid_t类型表示进程ID,但为了表示-1, ...

  8. JavaScript学习记录四

    title: JavaScript学习记录四 toc: true date: 2018-09-16 20:31:22 --<JavaScript高级程序设计(第2版)>学习笔记 要多查阅M ...

  9. mybatis与spring整合配置

    mybatis与spring整合配置: 第一种方式:(此处配置扫描的包路径.注解.每个mapper类上面需要加@Repository才能纳入spring的bean管理器中) <!-- 自动扫描m ...

  10. 常用css框架 Sass/Less

    Bootstrap less/sass Sass (Syntactically Awesome Stylesheets)是一种动态样式语言,Sass语法属于缩排语法,比css比多出好些功能(如变量.嵌 ...