(Array) find Unsorted Subarray
16 Nov 2019 | algorithm programming leetcodehttps://leetcode.com/problems/shortest-unsorted-continuous-subarray
class Solution { public int findUnsortedSubarray(int[] nums) { int[] sortedNums = Arrays.copyOf(nums, nums.length); Arrays.sort(sortedNums); int start = -1; int end = -1; for(int i=0; i<nums.length; i++) { if(nums[i] != sortedNums[i]) { if(start == -1) { start = i; end = i+1; } else { end = i; } } } if(end ==-1 && start==-1){ return 0; } else { return end-start+1; } } }
class Solution { public int findUnsortedSubarray(int[] nums) { int lastIndex= nums.length-1; int start = -1; // or start = lastIndex; int end = -2; // or end = 0; int max=nums[0]; //max scan from index: 1 to lastIndex, update max and end accordingly int min=nums[lastIndex]; //min scan from index: (lastIndex-1) to 0, update min and start accordingly for(int i=1;i<=lastIndex;i++){ if(nums[i]<max){ end= i; }else if(nums[i]>max){ max = nums[i]; } if(nums[lastIndex-i]>min){ start = lastIndex-i; }else if(nums[lastIndex-i]<min){ min = nums[lastIndex-i]; } } return (end-start+1); //return end-start<0?0:(end-start+1); } }