-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRemoveShortesSubarray.java
94 lines (85 loc) · 2.87 KB
/
RemoveShortesSubarray.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/submissions/*/
class Solution {
public int findLengthOfShortestSubarray(int[] arr) {
if (arr.length == 0 || arr.length == 1) return 0;
int start = -1, end = -1;
//checking some boundary conditions
for (int i = 1; i < arr.length; ++i)
if (arr[i] < arr[i-1])
{
start = i;
break;
}
for (int i = arr.length-2; i >= 0; --i)
if (arr[i] > arr[i+1])
{
end = i;
break;
}
if (start == -1 && end == -1) return 0;
boolean isDecreasing = true;
for (int i = 1; i < arr.length; ++i)
if (arr[i] > arr[i-1])
{
isDecreasing = false;
break;
}
if (arr[start-1] <= arr[end+1]) return end-start+1; //if we can join the two right away
if (!isDecreasing && arr[0] > arr[arr.length-1]) //if it is not in reverse order and the condition holds
return start >= (arr.length-end-1) ? (arr.length-start) : (end+1);
start = -1;
end = -1;
//fixing start
for (int i = 1; i < arr.length; ++i)
if (arr[i] < arr[i-1])
{
start = i;
break;
}
//if start unchanged
if (start == -1) return 0;
int minLength = Integer.MAX_VALUE;
//finding end
end = start;
while (end < arr.length && arr[start-1] > arr[end])
++end;
for (int j = arr.length-1; j > end; --j)
if (arr[start-1] > arr[j] || (j != arr.length-1 && arr[j] > arr[j+1]))
{
end = j+1;
break;
}
minLength = end-start;
--end;
boolean isReducing = false, flag = false;
//checking for better result
while (true)
{
--start;
while (start != 0 && arr[end] >= arr[start-1] && (end == arr.length-1 || arr[end] <= arr[end+1]))
--end;
if (end-start+1 > minLength && !isReducing)
{
flag = true;
break;
}
else if (end-start+1 < minLength)
{
minLength = end-start+1;
isReducing = true;
}
else if (isReducing)
break;
}
//if start is at 0
if (flag)
{
if (arr[start+1] >= arr[start]) ++start;
while (end == arr.length-1 || arr[end] < arr[end+1])
--end;
if (end-start+1 < minLength && (start == 0 || arr[start-1] <= arr[end+1]))
minLength = end-start+1;
}
return minLength;
}
}