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) {
if(prices.length<2) return 0;
int res=prices[1]-prices[0]; //开始是想用定义一个新的数组的,写到一半发现只要2个变量就好了
int curmin=(res<0?prices[1]:prices[0]);
for(int i=2;i<prices.length;i++){
res=Math.max(res,prices[i]-curmin);
curmin=Math.min(prices[i],curmin);
}
return (res>0)?res:0; //注意返回值肯定不是负数
}
}

  

(Array)121. Best Time to Buy and Sell Stock的更多相关文章

  1. 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记

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

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

  3. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

  4. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  5. 121. Best Time to Buy and Sell Stock@python

    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 买卖股票的最佳时间

    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】121 Best Time to Buy and Sell Stock

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

  8. 121. Best Time to Buy and Sell Stock (Array;DP)

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

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

随机推荐

  1. MySQL集群在断网后再启动报"Unable to start missing node group"问题处理

    总所周知,MySQL集群又名ndb cluster,而ndb就是network based database的简称,数据库节点之间依靠网络来通信和保证数据分块间的一致性.今天由于机房交换机损坏,导致集 ...

  2. CentOS 系统安装

    1.安装时分区建立 建立/boot分区 500M /swap 10000M 剩余空间建立PV PV加入VG 建立LV 全部挂在到/ 2安装模式选择:基本安装 开发工具全部装,SERVER 全部不选,中 ...

  3. UE4 材质切换(带动画效果)

    先看效果图:小木块掉到地板上(小木块本身会消失掉),地板就开始了动效材质切换.引擎版本用的是4.11.2 方法步骤: 首先在UE4内容浏览器中新建一个材质. 第一步要实现一个扫光的效果,如下图. 实现 ...

  4. 粗略了解struts2

    花了半天的时间再把struts2详细拟了一遍,之前用习惯了servlet加jsp,再看struts2的时候终于明白为什么大家都愿意学,以人类天生的惰性,要让他们愿意去学习一个新的东西,这东西一定可以让 ...

  5. iOS开发之直接使用UISearchBar

    iOS开发中经常需要使用SearchBar,我们可以选择使用UISearchBar+UISearchController或者UISearchBar+UISearchDisplayController( ...

  6. css中的背景、边框、补丁相关属性

    css中的背景.边框.补丁相关属性 关于背景涉及到背景颜色与背景图片 背景颜色background-color即可设定: 背景图片background-image即可设定: 但是背景图片还涉及到其他的 ...

  7. Poco C++ MySQl demo

    #include "Poco/Exception.h"#include "Poco/Data/Session.h"#include "Poco/Dat ...

  8. UVa 673 平衡的括号

    题意:给出包含"()"和"[]"的括号序列,判断是否合法. 用栈来完成,注意空串就行. #include<iostream> #include< ...

  9. hadoop 集群跑的时候用到hbasejar 文件的引用问题

    1. 创建软连接 ln -s /home/hadoop/bigdater/hbase-0.98.6-cdh5.3.6/conf/hbase-site.xml ./hbase-site.xml(记得这里 ...

  10. hdu1260 dp

    题意:有 k 个人需要买电影票,a[i] 表示第 i 个人单独买票要花费的时间,b[i] 表示第 i-1 个和第 i 个人一起买票需要花费的时间,问卖给所有人各一张票最少需要到什么时候. dp[i]表 ...