增量元素之间的最大差值 (Maximum Difference Between Increasing Elements)

 

思路:暴力,空间o1,时间on^2

// @Title: 增量元素之间的最大差值 (Maximum Difference Between Increasing Elements)
// @Author: qisiii
// @Date: 2022-02-26 11:09:04
// @Runtime: 4 ms
// @Memory: 40.6 MB
// @comment: 暴力,空间o1,时间on^2
// @flag: BLUE
class Solution {
    public int maximumDifference(int[] nums) {
        int max=-1;
        for(int i=1;i<nums.length;i++){
            for(int j=0;j<i;j++){
                if(nums[i]==nums[j]){
                    continue;
                }
               int temp= nums[i]-nums[j];     
                max=Math.max(temp,max);
            }
        }
        return max;
    }
}

+++ title = “增量元素之间的最大差值 (Maximum Difference Between Increasing Elements)” draft = false +++

思路:最小值定位

// @Title: 增量元素之间的最大差值 (Maximum Difference Between Increasing Elements)
// @Author: qisiii
// @Date: 2022-02-26 11:21:21
// @Runtime: 4 ms
// @Memory: 40.6 MB
// @comment: 最小值定位
// @flag: BLUE
class Solution {
    public int maximumDifference(int[] nums) {
        int max=-1;
        for(int i=1,maxIndex=1,minIndex=0;i<nums.length;i++){
            for(int j=minIndex;j<i;j++){
                if(nums[i]==nums[j]){
                    continue;
                }
               int temp= nums[i]-nums[j];     
                max=Math.max(temp,max);
                if(nums[j]<=nums[minIndex]){
                    minIndex=j;
                }
            }
        }
        return max;
    }
}

思路:固定最小值

// @Title: 增量元素之间的最大差值 (Maximum Difference Between Increasing Elements)
// @Author: qisiii
// @Date: 2022-02-26 11:37:01
// @Runtime: 0 ms
// @Memory: 40.8 MB
// @comment: 固定最小值
// @flag: BLUE
class Solution {
    public int maximumDifference(int[] nums) {
        int max=-1;
        for(int i=1,minV=nums[0];i<nums.length;i++){
            if(nums[i]>minV){
                max=Math.max(nums[i]-minV,max);
            }else{
                minV=nums[i];
            }
        }
        return max;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18