// you can also use includes, for example:
// #include <algorithm>
#include <climits>
int solution(const vector<int> &A) {
// write your code in C++98
//...find out the minimum price you haved scaned, then we can
//get current profits if we sell at current price, keep record of the maximum profit
int minPrice = INT_MAX;
int maxProfit = 0;
for(int i = 0; i < A.size(); ++i)
{
maxProfit = max(A[i]-minPrice, maxProfit);
minPrice = min(minPrice, A[i]);
}
//...return result
return maxProfit;
}
[codility]Max-profit
最新推荐文章于 2022-10-25 13:43:57 发布
本文介绍了一种寻找股票买卖最佳时机的算法实现。通过遍历价格数组,算法记录已扫描过的最低价格,并据此计算出若在当前价格卖出所能获得的最大利润。最终返回最大利润作为结果。

962

被折叠的 条评论
为什么被折叠?



