GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #2016 - Maximum Difference Between Increasing Elements - LeetCode JavaScript Solutions

Back to all solutions

#2016 - Maximum Difference Between Increasing Elements

Easy • View on LeetCode • View on GitHub
Array

Problem Description

Given a 0-indexed integer array nums of size n, find the maximum difference between nums[i] and nums[j] (i.e., nums[j] - nums[i]), such that 0 <= i < j < n and nums[i] < nums[j].

Return the maximum difference. If no such i and j exists, return -1.

Solution

/**
 * @param {number[]} nums
 * @return {number}
 */
var maximumDifference = function(nums) {
  let max = 0;

  for (let i = 0, min = nums[0]; i < nums.length; i++) {
    min = Math.min(min, nums[i]);
    max = Math.max(max, nums[i] - min);
  }

  return max === 0 ? -1 : max;
};
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.