-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
32 lines (32 loc) · 935 Bytes
/
Solution.cs
File metadata and controls
32 lines (32 loc) · 935 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Solution
{
public int FindLengthOfShortestSubarray(int[] arr)
{
int n = arr.Length;
int left = 0, right = n - 1;
while (left + 1 < n && arr[left] <= arr[left + 1]) left++;
while (right > 0 && arr[right] >= arr[right - 1]) right--;
if (left >= right) return 0;
int ret = Math.Min(n - (left + 1), right);
for (int i = 0; i <= left; i++)
{
int target = arr[i];
int low = right, high = n - 1, ans = n;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (arr[mid] >= target)
{
ans = mid;
high = mid - 1;
}
else
{
low = mid + 1;
}
}
ret = Math.Min(ret, ans - i - 1);
}
return ret;
}
}