classSolution { public: intmaxIncreasingSubarrays(vector<int> &nums){ int n = nums.size(); // n starts from 2 int k = n / 2; int a, b, i; int flag = true;
while (k > 0) { a = 0; // 0..k-1 b = a + k; // a+k.. b+k-1 while (b + k - 1 < n) { flag = true; for (i = 1; i < k; i++) { if (nums[a + i] > nums[a + i - 1] && nums[b + i] > nums[b + i - 1]) { continue; } else { a++; b = a + k; flag = false; break; } } if (flag) { return k; } } k--; } return0; } };
We define an array inc, where inc[i] represents the length of the longest increasing subarray ending at nums[i]. For two adjacent increasing segments starting at a and b, they must satisfy inc[a + k - 1] >= k && inc[b + k - 1] >= k. This also timed out, wuwu.
classSolution { public: intmaxIncreasingSubarrays(vector<int> &nums){ int n = nums.size(); int i; // n starts from 2 int k = n / 2; int a, b; vector<int> inc(n, 1); for (i = 1; i < n; i++) { if (nums[i] > nums[i - 1]) { inc[i] = inc[i - 1] + 1; } }
while (k > 0) { a = 0; b = a + k; while (b + k - 1 < n) { if (inc[a + k - 1] >= k && inc[b + k - 1] >= k) { return k; } else { a++; b = a + k; } } k--; if (k == 1) { return1; } } return1; } };
$$O(n)$$
If we know all strictly increasing adjacent segments, then k is the minimum of these two segments. We traverse the array once to find the maximum k. But note, we also need to record the length of a single strictly increasing segment, because it can also be split into two strictly increasing adjacent segments.
// #include <limits.h> #include<vector> using std::vector;
classSolution { public: // Traverse the array and calculate the length of all consecutive increasing segments. intmaxIncreasingSubarrays(vector<int> &nums){ int n = nums.size(); int i;
int curr_len = 1; // Set prev_len to 0, otherwise the first judgment will update max even if there is no preceding data segment. int prev_len = 0; int max_val = 0;
int len_max = 0; if (n <= 1) { return0; }
for (i = 1; i < n; i++) { if (nums[i] > nums[i - 1]) { curr_len++; } else { max_val = std::max(max_val, std::min(curr_len, prev_len)); len_max = std::max(curr_len, len_max); prev_len = curr_len; curr_len = 1; } } // Finally, don't forget to update len_max. len_max = std::max(len_max, curr_len);