-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily151.cpp
More file actions
79 lines (65 loc) · 1.99 KB
/
Copy pathdaily151.cpp
File metadata and controls
79 lines (65 loc) · 1.99 KB
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
// Solution 1 - FAIL
class Solution {
public:
int findLengthOfShortestSubarray(vector<int>& arr) {
/*
start from the right and decrement
count current subarray
start counting removal subarray
while num n < num n - 1
removal subarray size ++
shortest subarray = min(curr subarr, rem subarr)
*/
auto subarr = INT_MAX;
int curr_arr_len = 1;
auto curr = arr.back();
int i = arr.size() - 2;
while (i >= 0) {
if (curr > arr[i]) {
curr_arr_len++;
curr = arr[i];
--i;
continue;
}
int rem = 0;
while (i >= 0 and curr <= arr[i]) {
rem++;
curr = arr[i];
--i;
}
subarr = std::min(subarr, std::min(curr_arr_len, rem));
curr_arr_len = 1;
}
return subarr;
}
};
// Solution 2
class Solution {
public:
int findLengthOfShortestSubarray(vector<int>& arr) {
int n = arr.size();
int left = 0, right = n - 1;
// Find the longest non-decreasing prefix
while (left < n - 1 && arr[left] <= arr[left + 1]) {
left++;
}
// If the array is already sorted
if (left == n - 1) return 0;
// Find the longest non-decreasing suffix
while (right > 0 && arr[right - 1] <= arr[right]) {
right--;
}
// Compute the minimum subarray to remove
int result = std::min(n - left - 1, right);
// Attempt to merge the prefix and suffix
int i = 0, j = right;
while (i <= left && j < n) {
if (arr[i] <= arr[j]) {
result = std::min(result, j - i - 1); // Size of the middle subarray
i++; // Move the prefix pointer forward
} else {
j++; // Move the suffix pointer forward
}
}
}
};