记录一下最小值,O(N)扫一遍
class Solution { public: /** * * @param prices int整型vector * @return int整型 */ int maxProfit(vector<int>& prices) { int minn = 0x3f3f3f3f,ans = 0; for(auto i:prices) { ans = max(ans,i - minn); minn = min(i,minn); } return ans; } };
记录一下最小值,O(N)扫一遍
class Solution { public: /** * * @param prices int整型vector * @return int整型 */ int maxProfit(vector<int>& prices) { int minn = 0x3f3f3f3f,ans = 0; for(auto i:prices) { ans = max(ans,i - minn); minn = min(i,minn); } return ans; } };