[codility]Max-profit

云洋
2023-12-01
// 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;
}

 类似资料: