[Leetcode Week6]Best Time to Buy and Sell Stock
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的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 【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 ...
- [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 ...
- 【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 ...
随机推荐
- 第二十三篇 logging模块(******)
日志非常重要,而且非常常用,可以通过logging模块实现. 热身运动 import logging logging.debug("debug message") logging. ...
- Django 运行Admin 页面时出现 UnicodeDecodeError: 'gbk' codec can't decode byte XXXX解决方法
具体报错信息 Traceback (most recent call last): File "D:\Anaconda3\lib\site-packages\django\core\hand ...
- Android流式布局控件
1,自定义flowlayout代码 package com.hyang.administrator.studentproject.widget; import android.content.Cont ...
- pta数组作业
7-2 设计思路:本题要求处理数据并输出最大值及其对应的最小下标,首先输入n,然后定义一个长度为n的数组用于存储数据,定义m=a[0],n=0,从a[1]开始与m进行比较,若某项大于m,就把该项的值赋 ...
- 搭建springmvc项目没扫描到mapper和service
严重: Servlet.service() for servlet [spring] in context with path [/springmvc-demo] threw exceptionorg ...
- Java堆和栈
栈中存基本类型变量数据和对象的引用 堆中存new的对象
- 【bzoj1999】[Noip2007]Core树网的核 树的直径+双指针法+单调队列
题目描述 给出一棵树,定义一个点到一条路径的距离为这个点到这条路径上所有点的距离的最小值.求一条长度不超过s的路径,使得所有点到这条路径的距离的最大值最小. 输入 包含n行: 第1行,两个正整数n和s ...
- Codeforces 662C(快速沃尔什变换 FWT)
感觉快速沃尔什变换和快速傅里叶变换有很大的区别啊orz 不是很明白为什么位运算也可以叫做卷积(或许不应该叫卷积吧) 我是看 http://blog.csdn.net/liangzhaoyang1/ar ...
- Codeforce 721C DP+DAG拓扑序
题意 在一个DAG上,从顶点1走到顶点n,路径上需要消费时间,求在限定时间内从1到n经过城市最多的一条路径 我的做法和题解差不多,不过最近可能看primer看多了,写得比较复杂和结构化 自己做了一些小 ...
- 【电影影评】梦之安魂曲-败给了BGM和豆瓣影评
首先,这部电影豆瓣8.7分,一般来说,豆瓣的打分是比较准确的.能反映一个片子的质量,而较少受到环境的影响.但是这种关系当然也不全对,比如某些片子可能特别让某一种人喜欢(如退役军人和军旅题材),而在某些 ...