题目:

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. 【小思考】Python的float转换精度损失所想到的

    首先,为啥会要讨论这个问题. 我得为昨天拖了小组后腿深表歉意.其实程序逻辑很快就理通了的,但自己总是会因为各种各样的小问题束缚手脚,看接下来这个图片: 稍微有数据敏感性的同学就能看出,中间这么一大堆又 ...

  2. c/c++--strlen()小问题

    int x = 2; char * str = "abcd"; int y = (x - strlen(str)) / 3; printf("%d\n", y) ...

  3. JAVAEE——SpringMVC第二天:高级参数绑定、@RequestMapping、方法返回值、异常处理、图片上传、Json交互、实现RESTful、拦截器

    1. 课前回顾 https://www.cnblogs.com/xieyupeng/p/9093661.html 2. 课程计划 1.高级参数绑定 a) 数组类型的参数绑定 b) List类型的绑定 ...

  4. Android DecorView浅析

    摘要 一.DecorView为整个Window界面的最顶层View. 二.DecorView只有一个子元素为LinearLayout.代表整个Window界面,包含通知栏,标题栏,内容显示栏三块区域. ...

  5. 你了解border-radius吗?

    1.圆角正方形 .rounded-square{ width: 200px; height: 200px; background-color: pink; border-radius: 50px; } ...

  6. html5那些事儿

    一.优势1.标签的改变:<header>,<footer>,<dialog>,<aside>,<figure>,<section> ...

  7. Charles 对于线上比一比抓包怎么修改

    1: http://m.1768.com/?act=game_biyibi&abeam=1 Map Local /Users/zhengming/Desktop/biyibi.html biy ...

  8. pycharm 激活相关

    https://blog.csdn.net/u014044812/article/details/78727496dfasdfasdfdfaasdkjfhaskljdfhkajsdhfljahsdfk ...

  9. 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)

    贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于  ...

  10. wdcp(WDlinux Control Panel) 快速安装RPM包,lanmp一件安装

    lanmp一键安装包是wdlinux官网2010年开始推出的lamp,lnmp,lnamp(apache,nginx,php,mysql,zend,eAccelerator,pureftpd)应用环境 ...