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. ADB常用指令

    adb 命令是adb程序自带的一些命令:adb shell则是调用Android系统的命令,Android系统特有的命令都放在Android设备的/system/bin目录中 MonkeyRunner ...

  2. PL/SQL查看表结构

    SET LONG 99999;SET LINESIZE 140 PAGESIZE 1000;SELECT DBMS_METADATA.GET_DDL('&OBJECT_TYPE','& ...

  3. annoy安装

    yum install gcc-c++ #linux下需安装c++编译器 sudo pip install annoy

  4. PHP Warning: File upload error - unable to create a temporary file in Unknown on line 0

    代码在本地运行一切都OK,放到服务器上,网站访问正常,上传就出现该错误. 提示:PHP Warning: File upload error - unable to create a temporar ...

  5. 一个简单的NetCore项目:1 - 搭建框架,生成数据库

    1- 启动项目 安装.NETCORE SDK,教程在网上可以搜索的到,这里就不讲述了.简单粗暴的方式就是安装最新的VS2015. 2-搭建框架 2.1 打开VS新建一个项目,在弹出的新建项目对话框中, ...

  6. Hadoop2.6.0伪分布式搭建

    环境: 1.Ubuntu14.04 首先要在linux系统上新建一个账户,比如就叫做hadoop,用于专门运行hadoop. 2.配置jdk 我是使用的版本是jdk1.8. 解压:创建/usr/jav ...

  7. HDU 1445 Ride to School

    http://acm.hdu.edu.cn/showproblem.php?pid=1445 Problem Description Many graduate students of Peking ...

  8. Java IO 小结

    Java IO 的学习需要明白流设计的体系结构,这样才能在实际需要的时候,通过API文档查阅,快速实现功能.

  9. ElasticSearch1.7.1拼音插件elasticsearch-analysis-pinyin-1.3.3使用介绍

    ElasticSearch拼音插件elasticsearch-analysis-pinyin使用介绍 https://my.oschina.net/xiaohui249/blog/214505 摘要: ...

  10. CSS兼容性总结

    一.针对IE6的 !important 必须写在前面,例如: background:#9C6 !important;background:#999; 二.CSS HACK //IE6 专用 _heig ...