您的位置:首页 > 其它

leetcode Best Time to Buy and Sell Stock I II III

2013-10-30 21:01 399 查看
这三个题目是连续的就一同写下自己的想法吧。

题目1:

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.

题意:就是从一个数组中,找出差价最大的两天(买进时间必须在卖出时间之前)。这个很简单了,思路是遍历一遍数组即可,假设遍历到第j天,计算j之前最低价即可。更新最低价。。。代码也很简单。。。

leetcode 不知道为什么不能将vector<int>::size_type 和int 兼容。所以for(int i=0;i<prices.size();i++)不能通过,有时候又可以,我没有仔细研究。。。很郁闷。

class Solution {
public:
int maxProfit(vector<int> &prices) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int mm=INT_MAX;
int mmax=0;
for(vector<int>::size_type i=0;i<prices.size();i++)
{
if(prices[i]-mm >mmax)
mmax=prices[i]-mm;
if(prices[i]<mm)
mm=prices[i];
}
return mmax;
}};


题目2:

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

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock
before you buy again)

题意:也是计算最大差价。不同的是可以多次买卖,意思就是可以卖完继续买,只要后一天可以赚钱就可以买。解法就是只要连续两天能赚钱,即前一天价格低于后一天价格,就可以买进。

于是代码就更简单了。

class Solution {
public:
int maxProfit(vector<int> &prices) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int mmax=0;
int n=prices.size();
for(int i=0;i<n-1;i++)
{
mmax+=prices[i+1]-prices[i]>0 ? prices[i+1]-prices[i]:0;
}
return mmax;
}
};


题目3:

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

Design an algorithm to find the maximum profit. You may complete at most two transactions.

Note:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
题意:背景还是买卖问题。这次是计算两次买卖能赚到的最大差价。而且不可以连续买两次,第二次买之前第一次要卖出。

解法:其实有了第一题的思路,这个就很好想到解法了。就是将数组分两段,使得左边最大差价+右边的最大差价最大就可以了。如果直接写代码可能就写出了遍历数组,然后分别求两端最大差价,这样的算法时间复杂度还是在O(n*n)级别的。可以做下预处理将计算两端差价保存在数组中,sl[i]保存左边的i左边的最大差价,sr[i]保存i右边的最大差价。计算sl和sr的时间复杂度是O(n),遍历数组的时间也是O(n)。总体算法时间复杂度还是保持在O(n)。

class Solution {
public:
int maxProfit(vector<int> &prices) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<int> sl(prices.size(),0);
vector<int> sr(prices.size(),0);
int n=prices.size();
int mm=prices[0];
for(int i=1;i<n;i++)
{
sl[i]=max(sl[i-1],prices[i]-mm);
if(prices[i]<mm)
mm=prices[i];
}
mm=prices[n-1];
for(int i=n-2;i>=0;i--)
{
sr[i]=max(sr[i+1],mm-prices[i]);
if(prices[i]>mm)
mm=prices[i];
}
int mmax=0;
for(int i =0;i<n;i++)
{
if(sl[i]+sr[i]>mmax)
mmax=sl[i]+sr[i];
}
return mmax;
}
};


今天陪学长玩了几局dota罪过。。。。晚上还帮我哥做他的英语作业,我了个去。。。。干什么都要学习英语的么。。mark完博客我还是去看看书吧,最近看的是c++之父写的那本厚书,太厚了。慢慢看吧。

希望看到题目的同学,有什么好的想法告诉我。或者我的解法有问题,请指正谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 算法 遍历