2016: Maximum-Difference-Between-Increasing-Elements
Easy


table of contents

Just use a greedy algorithm; while iterating through the array nums:

code

class Solution {
public:
    int maximumDifference(vector<int>& nums) {
        int ans = -1;
        int mn = nums[0];
        for (int i = 1; i < nums.size(); ++i) {
            if (mn < nums[i]) {
                ans = max(ans, nums[i]-mn);
            } else {
                mn = nums[i];
            }
        }
        return ans;
    }
};

complexity

time taken