initial commit
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include "maxprofit2.hpp"
|
||||
|
||||
int main() {
|
||||
std::vector<int> prices = {7, 1, 5, 3, 6, 4};
|
||||
int k = maxProfit(prices);
|
||||
std::cout << "k: " << k << std::endl;
|
||||
assert(k == 7);
|
||||
prices = {1, 2, 3, 4, 5};
|
||||
k = maxProfit(prices);
|
||||
std::cout << "k: " << k << std::endl;
|
||||
assert(k == 4);
|
||||
prices = {7, 6, 4, 3, 1};
|
||||
k = maxProfit(prices);
|
||||
std::cout << "k: " << k << std::endl;
|
||||
assert(k == 0);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <vector>
|
||||
|
||||
inline int maxProfit(std::vector<int> &prices) {
|
||||
int profit = 0;
|
||||
for (int i = 1; i < int(prices.size()); i++) {
|
||||
if (prices[i] > prices[i - 1]) {
|
||||
profit += prices[i] - prices[i - 1];
|
||||
}
|
||||
}
|
||||
return profit;
|
||||
}
|
||||
Reference in New Issue
Block a user