手撕第二题只遍历一遍的话感觉可以用单调队列解决 code: public static int MAX = 100001; public static int [] maxDeque = new int[MAX]; public static int [] minDeque = new int[MAX]; public static int maxHead,maxTail; public static int minHead,minTail; public static int method(int [] nums){ int n = nums.length; int ans = Integer.MIN_VALUE; for(int i = 0;i < n;i ++){ while(maxHead < maxTail && nums[maxDeque[maxTail - 1]] < nums[i]){ maxTail--; } maxDeque[maxTail++] = i; while(minHead < minTail && nums[minDeque[minTail - 1]] > nums[i]){ minTail--; } minDeque[minTail++] = i; int max = nums[maxDeque[maxHead]] - nums[minDeque[minHead]]; ans = Math.max(ans,max); } return ans; }
点赞 1

相关推荐

牛客网
牛客企业服务