Best Time to Buy and Sell Stock 解答

时间:2022-06-19 20:16:13

Question

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.

Solution

Key to the problem is to record money that we buy the stock.

For each element prices[i], we need to compare it with current buy-in price "buyIn":

If prices[i] > buyIn, we calculate the profit and compare it with current profit.

If prices[i] < buyIn, we set prices[i] as new buyIn.

Time complexity O(n), space cost O(1)

 public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2)
return 0;
int result = Integer.MIN_VALUE;
int buyIn = prices[0], length = prices.length;
for (int i = 1; i < length; i++) {
if (prices[i] > buyIn)
result = Math.max(result, prices[i] - buyIn);
else
buyIn = prices[i];
}
if (result < 0)
result = 0;
return result;
}
}

Best Time to Buy and Sell Stock 解答的更多相关文章

  1. leetcode:122&period; Best Time to Buy and Sell Stock II(java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...

  2. &lbrack;LeetCode&rsqb; Best Time to Buy and Sell Stock III 买股票的最佳时间之三

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

  3. &lbrack;LeetCode&rsqb; 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 ...

  4. Best Time to Buy and Sell Stock系列

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

  5. LeetCode&colon; Best Time to Buy and Sell Stock III 解题报告

    Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...

  6. Best Time to Buy and Sell Stock I II III IV

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

  7. &lbrack;LeetCode&rsqb; 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 ...

  8. &lbrack;LeetCode&rsqb; 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 ...

  9. &lbrack;LeetCode&rsqb; 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. iOS中的&lowbar;&lowbar;typeof与typeof

    做SDK开发引入AFNetworking那么多的文件不太合适,所以这几天在精简AFNetworking,并共享中github上,https://github.com/yjh4866/Simplifie ...

  2. 提高SQL查询效率&lpar;SQL优化&rpar;

    要提高SQL查询效率where语句条件的先后次序应如何写 http://blog.csdn.net/sforiz/article/details/5345359   我们要做到不但会写SQL,还要做到 ...

  3. javascript&lowbar;basic&lowbar;04之节点、元素

    1.DOM:文档对象模型,Document Object Model: 2.BOM:浏览器对象模型,Browser Object Model: 3.DOM组成:核心DOM,XML DOM,HTML D ...

  4. JS-DOM2级事件对象跨浏览器处理(已封装)

    var eventUill = { //添加事件 addHander: function(element, type, handler) { if(element.addEventListener) ...

  5. XE 的程序升级 XE5 问题处理记录

    XE 的程序升级 XE5 问题处理记录 1.  [dcc32 Fatal Error] frxClass.pas(3556): F1026 File not found: 'xxxxx\Registr ...

  6. bzoj 3131&colon; &lbrack;Sdoi2013&rsqb;淘金

    #include<cstdio> #include<iostream> #include<queue> #include<algorithm> #def ...

  7. Oracle 递归查询

    现实中我们经常需要用到一些递归查询,下面我们来介绍下ORACLE中递归查询的使用. 首先我们先新建一个表来存储以上信息 create table FAMILY ( person_id INTEGER, ...

  8. mysql查询锁表及解锁

    SHOW PROCESSLIST; KILL ; 锁表网上解释: 这牵涉到mysql的事务,简单通俗的话,就这样给你解释有一个任务序列控制sql语句的执行,第一次有select的语句查询表a,mysq ...

  9. 基本服务器的AAA实验(Cisco PT)

    1.实验拓扑 2.不通网段间的ping通测试 从pc-a ping到pc-b 从pc-a ping到pc-c 从pc-b ping到pc-c 3.配置过程 a.在路由器R1上配置一个本地用户账号并且利 ...

  10. 【CLR】解析AppDomain

    目录结构: contents structure [+] 什么是AppDomain 跨越AppDomain边界访问对象 按引用封送(Marshal-by-Reference) 按值封送(Marshal ...