Best Time to Buy and Sell Stock 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/


Description

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.

Example 1:

Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 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 In this case, no transaction is done, i.e. max profit = 0.

Solution

class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.empty() || prices.size() == 1)
return 0; int i, j, max = 0, low, high;
int stockCount = prices.size();
bool flag = false;
for (i = 0; i < stockCount - 1; i++)
if (prices[i] < prices[i + 1]) {
low = i;
flag = true;
break;
} if (!flag)
return 0; for (j = stockCount - 1; j >= low + 1; j--)
if (prices[j] > prices[j - 1]) {
high = j;
break;
} for (i = low; i <= high - 1; i++) {
for (j = i + 1; j <= high; j++) {
if (max < prices[j] - prices[i])
max = prices[j] - prices[i];
}
}
return max;
}
};

解题描述

这道题一开始想到的就是暴力破解,第一遍交就TLE,看了测例之后通过先分别从前往后和从后往前探测符合要求的买入点和卖出点之后才大大降低了对比的时间。

[Leetcode Week6]Best Time to Buy and Sell Stock的更多相关文章

  1. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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

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

  3. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 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. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

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

  5. [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

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

  6. [LeetCode] 309. 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 ...

  7. 【LeetCode】Best Time to Buy and Sell Stock IV

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

  8. [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III

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

  9. 【leetcode】Best Time to Buy and Sell Stock III

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

随机推荐

  1. Python 3基础教程24-读取csv文件

    本文来介绍用Python读取csv文件.什么是csv(Comma-Separated Values),也叫逗号分割值,如果你安装了excel,默认会用excel打开csv文件. 1. 我们先制作一个c ...

  2. 12-Mysql数据库----多表查询

    本节重点: 多表连接查询 符合条件连接查询 子查询 准备工作:准备两张表,部门表(department).员工表(employee) create table department( id int, ...

  3. POJ 2186 Popular Cows(强联通+缩点)

    Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...

  4. Bitcoin-NG

    Bitcoin-NG,一个新的可扩展的区块链协议 Bitcoin-NG仅受限于网络的传输延时,它的带宽仅受限于个人节点的处理能力.通过将比特币的区块链操作分解为两部分来实现这个性能改善:首领选择(le ...

  5. not1,not2,bind1st,bind2nd

    例子需要包含头文件 #include <vector> #include <algorithm> #include <functional> bind1st和bin ...

  6. WCF身份验证三:自定义身份验证之<MessageHeader>

    关于使用SoadHeader验证Robin已经有一篇十分精彩的文章: WCF进阶:为每个操作附加身份信息, 不过我的思维方式总是跟别人有点不太一样, 还是把类似的内容用我的方式重新组织一下. 使用He ...

  7. IntellIJ IDEA 配置 Git,顺带解决Git Push rejected问题

    1.下载便携版本git https://git-scm.com/download/win 弹出的下载取消,重新选择 2.解压自压缩文件. 3.配置IDEA 4.测试 5.配置终端环境shell为bas ...

  8. [POJ1631]Bridging signals

    题目大意:不知,根据样例猜测为最长上升子序列(竟然还对了) 题解:$O(n log_2 n)$,求二维偏序,(q为存答案的序列,a存原序列,len为答案) for(int i = 1; i <= ...

  9. 【MySQL】数据库 --MySQL的安装

    本篇教程主要讲解在CentOS 6.5下编译安装MySQL 5.6.14! 1.卸载旧版本: 使用下面的命令检测是否安装有MySQL server <span style="font- ...

  10. Spring源码解析-AOP简单分析

    AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等,不需要去修改业务相关的代码. 对于这部分内容,同样采用一个简单的例子和源码来说明. 接口 public ...