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]) {
= max(ans, nums[i]-mn);
ans } else {
= nums[i];
mn }
}
return ans;
}
};