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. 【Alpha版本】冲刺阶段——Day7

    [Alpha版本]冲刺阶段--Day7 阅读目录 今日进展 今日贡献量 贡献量汇总 TODOList 及项目燃尽图 [今日进展] 将项目源文件打成jar包,并运行测试 完成答辩ppt 项目运行情况 主 ...

  2. GUI常用对话框3

    %进度条 %waitbar h=waitbar(,'实例'); get(h); %获得进度条的子对象 get(get(h,'Children')) ha=get(h,'Children'); %获得坐 ...

  3. 5、CentOS 6.5系统安装配置Nginx-1.2.7+PHP-5.3.22环境

    一,操作系统 以最小服务器形式安装系统,并添加开发工具库,便于后期编译使用. 此处基本都是下一步,下一步,不再废话. 安装完成,进入系统,调通网络,关闭防火墙或打开相应的WEB端口. 以下安装操作默认 ...

  4. <转>jmeter(十六)配置元件之计数器

    本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...

  5. checkbox 全选效果

    html部分 <p id="all">全选</p> <input type="checkbox" /><br/> ...

  6. 算法笔记 #007# Backtracking

    留着备用. 题目描述和代码参考:https://www.geeksforgeeks.org/8-queen-problem/ NQueenProblem(js代码): class NQueenProb ...

  7. linux 安装 ImageMagick 和 imagick 扩展

    使用命令安装 1.依次运行以下命令 yum install ImageMagick yum install ImageMagick-devel yum install php-pear 安装php-p ...

  8. php 把秒数转换为时长(h:i:s格式)

    /** * 把秒数转换为时分秒的格式 * @param Int $times 时间,单位 秒 * @return String */ function secToTime($times){ $resu ...

  9. oracle 12c直方图收集的增强

    在oracle 12c之前,收集直方图信息是相对比较耗费资源的,因为要重复扫描几次:在oracle 12c中,则有较大的提升,具体可参考https://jonathanlewis.wordpress. ...

  10. log buffer space等待事件

    最近,我们有台服务器在delete操作期间发现一直在等待log buffer space,其他节点就没与这个问题.经查,向重做缓冲区上写入重做记录的进程,为了确保拥有重做缓冲区内必要的空间,需要获得r ...