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.
复杂度:O(N)
class Solution {
public:
int maxProfit(vector<int> &prices) {
if (prices.size() < 2) return 0;
int curres = 0;
int curmin = prices[0];
for (int i = 1; i < prices.size(); i++) {
if (curres < prices[i] - curmin)
curres = prices[i] - curmin;
curmin = (curmin > prices[i] ? prices[i] : curmin);
}
return curres;
}
};
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).
注意: you must sell the stock before you buy again. 就是说只能有一个stock在手中。否则的话,问题会很复杂。
之前没有注意这个条件。想错了。
方法:寻找连续递增区间。
复杂度:O(N)
class Solution {
public:
int maxProfit(vector<int> &prices) {
if (prices.size() < 2) return 0;
int res = 0;
int start = prices[0];
for (int i = 1; i < prices.size(); i++) {
if (prices[i] >= prices[i-1]) continue;
res += prices[i-1] - start;
start = prices[i];
}
res += *(prices.end()-1) - start;
return res;
}
};
class Solution {
public:
int maxProfit(vector<int> &prices) {
int n = prices.size();
if (n == 0) return 0;
int res = 0;
auto &a = prices;
int s = 0, e;
while (s < n) {
e = s + 1;
while (e < n && a[e] >= a[e-1]) e++;
res += a[e-1] - a[s];
s = e;
}
return res;
}
};
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)
#define min(a,b) ((a) > (b) ? (b) : (a))
#define max(a,b) ((a) < (b) ? (b) : (a))
class Solution {
public:
int maxProfit(vector<int> &prices) {
if (prices.size() < 2) return 0;
int s = prices.size();
int *a = new int[s+1];
int *b = new int[s+1];
memset(a, 0, 4*s+4);
memset(b, 0, 4*s+4);
int curm = prices[0];
a[0] = 0;
for (int i = 1; i < s; ++i) {
a[i] = max(a[i-1], prices[i] - curm);
curm = min(curm, prices[i]);
}
curm = prices[s-1];
b[s-1] = 0;
for (int i = s-2; i >=0; --i) {
b[i] = max(b[i+1], curm - prices[i]);
curm = max(curm, prices[i]);
}
int res = 0;
for (int i = 0; i < s; i++) {
res = max(res, a[i] + b[i+1]);
}
delete []a;
delete []b;
return res;
}
};
class Solution {
public:
int maxProfit(vector<int> &prices) {
int n = prices.size();
if (n == 0) return 0;
vector<int> r1(n,0);
vector<int> r2(n,0);
auto &a = prices;
int m = a[0];
for (int i = 1; i < n; i++) {
r1[i] = max(r1[i-1], a[i] - m);
m = min(a[i], m);
}
m = a[n-1];
for (int i = n - 2; i >= 0; i--) {
r2[i] = max(r2[i+1], m - a[i]);
m = max(a[i], m);
}
int res = r2[0];
for (int i = 0; i < n - 1; i++) {
res = max(res, r1[i] + r2[i+1]);
}
res = max(res, r1[n-1]);
return res;
}
};