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 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.
解题思路:
这道题不难,最简单的解题思路复杂度为O(n2),不过我想应该会超时,这里采用另一种解题方法,复杂度为O(n2)。
采用两个指针i和j,i用来指向所遍历过的元素中最小值,j则用来遍历数组,然后用j指向的值与i指向的值相减,如果差值大于max,则替换max,当j指向的值小于i指向的元素时,将i = j,继续之前的动作。
代码:
#include <iostream>
#include <climits>
#include <vector>
using namespace std; /**
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. */ class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty())
return 0;
int i = 0;
int j = i+1;
int max = 0;
while(j < prices.size())
{
if(prices[j] < prices[i])
{
i = j; }
else
{
int t = prices[j] - prices[i];
if(t > max)
max = t;
}
j++;
}
return max; }
}; int main(void)
{
int arr[] = {2,4,5,1,7,10};
int n = sizeof(arr) / sizeof(arr[0]);
vector<int> stock(arr, arr+n);
Solution solution;
int max = solution.maxProfit(stock);
cout<<max<<endl;
return 0;
}
LeetCode121:Best Time to Buy and Sell Stock的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- [LeetCode] 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 ...
- [LeetCode] 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 ...
- [LeetCode] 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 ...
- [LintCode] 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 ...
- [LintCode] 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 ...
随机推荐
- NanoProfiler - 适合生产环境的性能监控类库 之 大数据篇
上期回顾 上一期:NanoProfiler - 适合生产环境的性能监控类库 之 基本功能篇 上次介绍了NanoProfiler的基本功能,提到,NanoProfiler实现了MiniProfiler欠 ...
- 解决服务器每次都要输入Enter PEM pass phrase
今天架设好Python的HTTPS云服务器, 发现每次连接都要Enter PEM pass phrase 把服务器端的key里面的key剥离掉就好了 openssl rsa -in server.ke ...
- Permission is only granted to system apps
原文地址http://jingyan.baidu.com/article/9113f81b2e7a8c2b3314c711.html
- Lua字符串库(整理)
Lua字符串库小集 1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string.rep(s,n) 返回 ...
- Atitit oodbms的查询,面向对象的sql查询jpa jpql hql
Atitit oodbms的查询,面向对象的sql查询jpa jpql hql 1.1. 标准API历史1 1.2. JPA定义了独特的JPQL(Java Persistence Query Lang ...
- 《鸟哥的linux私房菜》 - linux命令温故而知新
在公司的某角落里,看到了<鸟哥的linux私房菜>,顿时想看看是什么鬼. 其他时候还要自己去买才有,现在正好,比图书馆方便.看完了,写点啥! 编辑器很重要,一个vim就主要是我的使用方向: ...
- iOS--------坐标系统(UIView的frame、bounds跟center属性)
1.概要翻开ios官方开发文档,赫然发现上面对这三个属性的解释如下: frame:描述当前视图在其父视图中的位置和大小. bounds:描述当前视图在其自身坐标系统中的位置和大小. center:描述 ...
- 技术往事:改变世界的TCP/IP协议(珍贵多图、手机慎点)
1.前言 作为应用层开发人员,接触最多的网络协议通常都是传输层的TCP(与之同处一层的另一个重要协议是UDP协议),但对于IP协议,对于应用程序员来说更多的印象还是IP地址这个东西,再往深一点也就很难 ...
- 用CSS text-transform转换字母大小写
以前我们都是用JS来实现字母的首字母大小写的,但现在没有必要了,CSS完全可以实现,话说兼容性还好从IE6开始就支持了. text-transform:none | capitalize | uppe ...
- SQL Pass北京举办1周年活动(本次活动特别邀请到了来自微软的SQL Server大师何雷谈数据库职业规划)
地点:北京微软(中国)有限公司[望京利星行],三层308室 时间:2013年 12 月28日 13:30-16:30 SQL PASS 北京QQ群号:2435349 新浪微群地址:http://q.w ...