I

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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
  Not 7-1 = 6, as selling price needs to be larger than buying price.

Example 2:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0. 思路:
one pass 算法(在O(N)的时间内解决问题,solve the problem in one pass)
leetcode解释:

The points of interest are the peaks and valleys in the given graph. We need to find the largest peak following the smallest valley. We can maintain two variables - minprice and maxprofit corresponding to the smallest valley and maximum profit (maximum difference between selling price and minprice) obtained so far respectively.

找一个数组中的最大差值(先最小后(最小后出现的)最大)

注意:
  1. for (int price : prices) 这个写法更简洁,至于优点...待发掘
  2. minprice 的初值设为Integer.MAX_VALUE;
  3. maxprofit 的初值设为 0;
代码:
public int maxProfit(int[] prices) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int price : prices) {
minprice = Math.min(minprice, price);
maxprofit = Math.max(maxprofit, price - minprice);
}
return maxprofit;
}

Leetcode121-Best Time to Buy and Sell Stock I - Easy的更多相关文章

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

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

  3. LeetCode121: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 w ...

  4. leetcode121—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 ...

  5. Leetcode No.122 Best Time to Buy and Sell Stock II Easy(c++实现)

    1. 题目 1.1 英文题目 You are given an array prices where prices[i] is the price of a given stock on the it ...

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

  7. LeetCode_122. Best Time to Buy and Sell Stock II

    122. Best Time to Buy and Sell Stock II Easy Say you have an array for which the ith element is the ...

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

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

随机推荐

  1. python 读csv格式的文件

    对于大多数的CSV 格式的数据读写问题,都可以使用csv 库 1. 直接读csv 以下是要操作的csv文件内容 import csv with open(r'C:\Temp\f.csv') as f: ...

  2. linux安装部署Nginx

    两个参考地址: NGINX的百度百科:https://baike.baidu.com/item/nginx/3817705?fr=aladdin NGINX的中文网站:http://www.nginx ...

  3. 几种线程安全的Map解析

    转载自 面试必问-几种线程安全的Map解析 HashMap线程安全的吗? Java中平时用的最多的Map集合就是HashMap了,它是线程不安全的. 看下面两个场景: 1.当用在方法内的局部变量时,局 ...

  4. ajax 检测用户名是否可用

    下面是一个 ajax 检测用户名是否可用的例子. django  项目中. —— views.py 里—— from django.shortcuts import render,HttpRespon ...

  5. HDU 3461 Code Lock(并查集+二分求幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3461 A lock you use has a code system to be opened in ...

  6. [转载]oracle的常用函数 instr() 和substr()函数

    在Oracle中 可以使用instr函数对某个字符串进行判断,判断其是否含有指定的字符. 在一个字符串中查找指定的字符,返回被查找到的指定的字符的位置. 语法: instr(sourceString, ...

  7. Andriod post Api与返回值

    vs后台api控制器  post接收参数----HttpContext.Current.Request.Params["account"].ToString() 返回值为对象返回, ...

  8. TCP/IP协议三次握手与四次挥手

    一.标志位和序号 seq序号 :发送方随机生成的 ack确认序号:ack=seq+1 标志位ACK=1时确认序号有效 SYN标志位:发起一个新连接 ACK标志位:确认序号有效 FIN标志位:断开连接 ...

  9. shell 多行注释 块注释

    转自 https://www.cnblogs.com/emanlee/p/3749911.html 1 : ' 被注释的多行内容 ' 2 :<<eof 被注释的多行内容 eof 3 :&l ...

  10. pxc集群进入非主模式怎么让最后的节点允许提供服务

    这种情况一般是,集群其他节点意外宕机而最后一个节点无法自我仲裁,而进入非主模式. 该模式拒绝任何SQL的执行: ERROR 1047 (08S01): WSREP has not yet prepar ...