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

    find 命令用于查找文件系统中的指定文件,其命令格式为:find   要查找的路径   表达式例如:find . -name 1.txt      在当前目录及其子目录下查找文件 1.txtfind ...

  2. i5 6300HQ 和 i7 6700HQ

    i7 6700HQ: 四核八线程 14nm 45W 2.6-3.5GHz 支持DDR4 2133内存 三级缓存6M HD Graphics 530集显 i5 6300HQ: 四核四线程 14nm 45 ...

  3. Java设计模式(三) 抽象工厂模式

    原创文章,同步发自作者个人博客,转载请注明出处 http://www.jasongj.com/design_pattern/abstract_factory/ 抽象工厂模式解决的问题 上文<工厂 ...

  4. MySql 中游标,事务,终止存储过程方法总结

    最近在项目开发中,有段逻辑处理,需要在网站,app,后台分别运行,这样给后期的维护带来了很大的不方便,容易遗漏app端或者后台,所以讲java代码转换成存储过程,把逻辑处理写在了mysql端,其中遇到 ...

  5. jsp中target="_blank"的用法

    对于点击查询按钮或a标签等,打开一个新页面并显示结果的做法如下: 1.form表单: 在form标签上加target="_blank"后,点击搜索按钮,显示查询结果时会打开一个新页 ...

  6. 【P1825】表达式整除

    又自己写出来了好开心OvO 原题: 24点这个游戏好多人都玩过,就是给你4个数,添加相应的运算符,是否可以得到结果是24.小x在玩了很多遍这个游戏之后,想把这个游戏给改变一下.给你n个整数,在n个整数 ...

  7. Ubuntu学习——第一篇

    一. Ubuntu简介 Ubuntu(乌班图)是一个基于Debian的以桌面应用为主的Linux操作系统,据说其名称来自非洲南部祖鲁语或科萨语的“ubuntu”一词,意思是“人性”.“我的存在是因为大 ...

  8. Sort merge join、Nested loops、Hash join(三种连接类型)

    目前为止,典型的连接类型有3种: Sort merge join(SMJ排序-合并连接):首先生产driving table需要的数据,然后对这些数据按照连接操作关联列进行排序:然后生产probed ...

  9. 1.AutoMapper核心:扁平化

    对象 - 对象映射的一个常见用法是获取一个复杂的对象模型,并将其展开成一个更简单的模型. 您可以采取复杂的模型,如: public class Order { private readonly ILi ...

  10. net不安装Oracle11g客户端直接使用ODAC

    好处: 1.不安装客户端也能访问服务器上的oracle(假设Application Server与DB Server 分开) 2.不需要安装ODAC 3.不需要配置TnsNames.Ora文件 缺点: ...