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