2016. Maximum Difference Between Increasing Elements Easy
1/**
2 * [2016] Maximum Difference Between Increasing Elements
3 *
4 * 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].
5 * Return the maximum difference. If no such i and j exists, return -1.
6 *
7 * Example 1:
8 *
9 * Input: nums = [7,<u>1</u>,<u>5</u>,4]
10 * Output: 4
11 * Explanation:
12 * The maximum difference occurs with i = 1 and j = 2, nums[j] - nums[i] = 5 - 1 = 4.
13 * Note that with i = 1 and j = 0, the difference nums[j] - nums[i] = 7 - 1 = 6, but i > j, so it is not valid.
14 *
15 * Example 2:
16 *
17 * Input: nums = [9,4,3,2]
18 * Output: -1
19 * Explanation:
20 * There is no i and j such that i < j and nums[i] < nums[j].
21 *
22 * Example 3:
23 *
24 * Input: nums = [<u>1</u>,5,2,<u>10</u>]
25 * Output: 9
26 * Explanation:
27 * The maximum difference occurs with i = 0 and j = 3, nums[j] - nums[i] = 10 - 1 = 9.
28 *
29 *
30 * Constraints:
31 *
32 * n == nums.length
33 * 2 <= n <= 1000
34 * 1 <= nums[i] <= 10^9
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/maximum-difference-between-increasing-elements/
40// discuss: https://leetcode.com/problems/maximum-difference-between-increasing-elements/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45 pub fn maximum_difference(nums: Vec<i32>) -> i32 {
46 0
47 }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_2016() {
58 }
59}
60
Back
© 2025 bowen.ge All Rights Reserved.