【Codility】Max-profit ★★

阎功
2023-12-01

Problem is here:Codility

// you can also use includes, for example:
// #include <algorithm>
int solution(const vector<int> &A) {
    // write your code in C++98
    if(A.size() < 2)
        return 0;
    
    int smallest = A[0];
    int maxProfit = A[1] - A[0];
    
    for(unsigned int i = 0; i < A.size(); i++)
    {
        if(A[i] < smallest)
            smallest = A[i];
        if(maxProfit < A[i] - smallest)
            maxProfit = A[i] - smallest;
    }
    return maxProfit;
}


 类似资料: