MaxProfit 發表於 2023-02-13 | 分類於 codility , Mamimum Slice problem problem一次的股票交易最大可以獲得多少獲益 solution123456789101112131415161718// you can use includes, for example:// #include <algorithm>// you can write to stdout for debugging purposes, e.g.// cout << "this is a debug message" << endl;int solution(vector<int> &A) { // write your code in C++14 (g++ 6.2.0) if(A.empty()) return 0; int mn = A[0], profit = 0; for(int a:A){ mn = min(mn, a); profit = max(profit, a-mn); } return profit;} analysis time complexity O(n) space complexity O(1)