581. Shortest Unsorted Continuous Subarray
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the length of the shortest continuous subarray that, if sorted, will result in the entire array being sorted in non-decreasing order. If the array is already sorted, the length is 0.
Solution
Shortest Unsorted Continuous Subarray in Python
class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
n = len(nums)
if n < 2:
return 0
# Find the right boundary of the unsorted subarray.
# Iterate from left to right, finding the last element that is smaller than the maximum so far.
right = -1
max_so_far = nums[0]
for i in range(1, n):
if nums[i] < max_so_far:
right = i
max_so_far = max(max_so_far, nums[i])
# If the array is already sorted, right will not be updated.
if right == -1:
return 0
# Find the left boundary of the unsorted subarray.
# Iterate from right to left, finding the first element that is larger than the minimum so far.
left = -1
min_so_far = nums[n - 1]
for i in range(n - 2, -1, -1):
if nums[i] > min_so_far:
left = i
min_so_far = min(min_so_far, nums[i])
return right - left + 1
Shortest Unsorted Continuous Subarray in Java
class Solution {
public int findUnsortedSubarray(int[] nums) {
int n = nums.length;
if (n < 2) {
return 0;
}
// Find the right boundary of the unsorted subarray.
// Iterate from left to right, finding the last element that is smaller than the maximum so far.
int right = -1;
int maxSoFar = nums[0];
for (int i = 1; i < n; i++) {
if (nums[i] < maxSoFar) {
right = i;
}
maxSoFar = Math.max(maxSoFar, nums[i]);
}
// If the array is already sorted, right will not be updated.
if (right == -1) {
return 0;
}
// Find the left boundary of the unsorted subarray.
// Iterate from right to left, finding the first element that is larger than the minimum so far.
int left = -1;
int minSoFar = nums[n - 1];
for (int i = n - 2; i >= 0; i--) {
if (nums[i] > minSoFar) {
left = i;
}
minSoFar = Math.min(minSoFar, nums[i]);
}
return right - left + 1;
}
}
Shortest Unsorted Continuous Subarray in C++
class Solution {
public:
int findUnsortedSubarray(vector<int>& nums) {
int n = nums.size();
if (n < 2) {
return 0;
}
// Find the right boundary of the unsorted subarray.
// Iterate from left to right, finding the last element that is smaller than the maximum so far.
int right = -1;
int max_so_far = nums[0];
for (int i = 1; i < n; ++i) {
if (nums[i] < max_so_far) {
right = i;
}
max_so_far = max(max_so_far, nums[i]);
}
// If right remains -1, the array is already sorted.
if (right == -1) {
return 0;
}
// Find the left boundary of the unsorted subarray.
// Iterate from right to left, finding the first element that is larger than the minimum so far.
int left = -1;
int min_so_far = nums[n - 1];
for (int i = n - 2; i >= 0; --i) {
if (nums[i] > min_so_far) {
left = i;
}
min_so_far = min(min_so_far, nums[i]);
}
return right - left + 1;
}
};
Shortest Unsorted Continuous Subarray in TypeScript
function findUnsortedSubarray(nums: number[]): number {
const n = nums.length;
if (n < 2) {
return 0;
}
// Find the right boundary of the unsorted subarray.
// Iterate from left to right, finding the last element that is smaller than the maximum so far.
let right = -1;
let maxSoFar = nums[0];
for (let i = 1; i < n; i++) {
if (nums[i] < maxSoFar) {
right = i;
}
maxSoFar = Math.max(maxSoFar, nums[i]);
}
// If the array is already sorted, right will not be updated.
if (right === -1) {
return 0;
}
// Find the left boundary of the unsorted subarray.
// Iterate from right to left, finding the first element that is larger than the minimum so far.
let left = -1;
let minSoFar = nums[n - 1];
for (let i = n - 2; i >= 0; i--) {
if (nums[i] > minSoFar) {
left = i;
}
minSoFar = Math.min(minSoFar, nums[i]);
}
return right - left + 1;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.