题目:

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.

题解:

这道题只让做一次transaction,那么就需要找到价格最低的时候买,价格最高的时候卖(买价的日期早于卖价的日期)。从而转化为在最便宜的时候买入,卖价与买价最高的卖出价最大时,就是我们要得到的结果。

因为我们需要买价日期早于卖价日期,所以不用担心后面有一天价格特别低,而之前有一天价格特别高而错过了(这样操作是错误的)。

所以,只许一次遍历数组,维护一个最小买价,和一个最大利润(保证了买在卖前面)即可。

代码如下:

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

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

  1. Best Time to Buy and Sell Stock - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Best Time to Buy and Sell Stock - LeetCode 注意点 在卖出之前必须要先购入 解法 解法一:遍历一遍,随时记录当前 ...

  2. leetcode 123. Best Time to Buy and Sell Stock III ----- java

    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 122. Best Time to Buy and Sell Stock II ----- java

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

  4. Best Time to Buy and Sell Stock——LeetCode

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

  5. 121. Best Time to Buy and Sell Stock——Leetcode

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

  6. 最佳时间买入卖出股票 Best Time to Buy and Sell Stock LeetCode

    LeetCode 我们有一个股票的数组,数组是每时间的钱,我们只能买入一次和卖出一次,求我们的最大收益. 我们知道了一个数组,那么我们可以在低价买入,然后高价卖出,但是需要知道我们的低价需要在高价之前 ...

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

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

  9. [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. V-by-one

    一:v-by-one技术产生背景     LVDS已经在业界盛行多年,近来电视解析度和播放格式的进展已经导致频宽需求大幅增加,具有60Hz和120Hz甚至240Hz更新频率的电视已经在商店内 贩售.2 ...

  2. jQuery的data() 和 h5 的 dataset()

    作用:获取自定义属性 命名规则:驼峰命名法  data-user==>user data-user-name==>userName 区别:jQuery:操作内存   h5: 操作DOM j ...

  3. ajax异步请求模式

    什么是异步请求 我们知道,在同步请求模型中,浏览器是直接向服务器发送请求,并直接接收.处理服务器响应的数据的.这就导致了浏览器发送完一个请求后,就只能干等着服务器那边处理请求,响应请求,在这期间其它事 ...

  4. 美团DB数据同步到数据仓库的架构与实践

    背景 在数据仓库建模中,未经任何加工处理的原始业务层数据,我们称之为ODS(Operational Data Store)数据.在互联网企业中,常见的ODS数据有业务日志数据(Log)和业务DB数据( ...

  5. Java 关于集合框架那点事儿

     1.引入集合框架  采用数组存在的一些缺陷:   1.数组长度固定不变,不能很好地适应元素数量动态变化的情况.   2.可通过数组名.length获取数组的长度,却无法直接获取数组中真实存储的个数. ...

  6. 网络数据修改工具netsed

    网络数据修改工具netsed   通过修改网络数据,可以绕过软件和防火墙的限制,达到特定的目的.Kali Linux提供一个简易数据修改工具netsed.该工具支持对TCP和UDP的数据进行修改.渗透 ...

  7. 【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】

    4152: [AMPPZ2014]The Captain Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2107  Solved: 820[Submi ...

  8. phalcon Model 'partitions' could not be loaded(模型不支持分区语句)

    注意: 很明确提示用phalcon自带的模型层是不能用partition这个关键字的 解决方法: 自己写个PDO类 然后用pdo中的query方法执行语句成功: mysql分区目的 是减少数据库的负担 ...

  9. Codeforces Round #248 (Div. 1) A. Ryouko's Memory Note 水题

    A. Ryouko's Memory Note 题目连接: http://www.codeforces.com/contest/434/problem/A Description Ryouko is ...

  10. Codeforces Round #293 (Div. 2) B. Tanya and Postcard 水题

    B. Tanya and Postcard time limit per test 2 seconds memory limit per test 256 megabytes input standa ...