← 返回 cisco 的题目列表Max Difference in Array
类型:online_judge
cisco
Given an integer array arr, find the two numbers such that the second number appears after the first one and their difference is as large as possible. Return the maximum difference. If no such pair exists, return 0.
Input Format:
An integer array arr of length n (1 <= n <= 10^5), with array elements in the range [-10^4, 10^4].
Output Format:
An integer representing the maximum difference.
Example 1:
Input: [7, 1, 5, 3, 6, 4] Output: 5 Explanation: Buy on the second day (price = 1) and sell on the fifth day (price = 6), profit = 6-1 = 5.
Example 2:
Input: [7, 6, 4, 3, 1] Output: 0 Explanation: In this case, no transactions can yield a profit.
Note:
You can only perform one transaction (buy and sell) at a time, and cannot buy and sell on the same day.
Example
Input
7\n1 5 3 6 4\n