Given an array, find the length of the longest increasing subarray (contiguous elements) such that it is possible to change at most one number (change one number to any integer you want) from the sequence to make the sequence strictly increasing.
Input:
nums =[7,2,3,1,5,10]Output:
5Explanation:
Here, we can choose subarray [2,3,1,5,10] and by changing its 3rd element(that is1) to 4, it will become an increasing sequence.
Input:
nums =[10,10]Output:
2Explanation:
Here, we can choose subarray [10,10] and by changing its 2nd element(that is10) to 11, it will become an increasing sequence.
We want to find the longest increasing subarray, allowing at most one change. By keeping track of the longest increasing subarrays ending and starting at each index, we can efficiently combine segments by skipping one element.
funclongestIncreasingSubarray(nums []int) int {
n:= len(nums)
l:= make([]int, n)
r:= make([]int, n)
ans:=1fori:=rangel {
l[i], r[i] = 1, 1 }
fori:=1; i < n; i++ {
ifnums[i] > nums[i-1] {
l[i] = l[i-1] +1 }
}
fori:=n-2; i>=0; i-- {
ifnums[i] < nums[i+1] {
r[i] = r[i+1] +1 }
}
for_, v:=rangel {
ifv > ans {
ans = v }
}
fori:=1; i < n-1; i++ {
ifnums[i-1] < nums[i+1] {
ifl[i-1]+r[i+1] > ans {
ans = l[i-1]+r[i+1]
}
}
}
ifans+1 > n {
returnn }
returnans+1}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
publicintlongestIncreasingSubarray(int[] nums) {
int n = nums.length, ans = 1;
int[] l =newint[n], r =newint[n];
Arrays.fill(l, 1);
Arrays.fill(r, 1);
for (int i = 1; i < n; i++)
l[i]= nums[i]> nums[i-1]? l[i-1]+ 1 : 1;
for (int i = n-2; i >= 0; i--)
r[i]= nums[i]< nums[i+1]? r[i+1]+ 1 : 1;
for (int v : l) ans = Math.max(ans, v);
for (int i = 1; i < n-1; i++)
if (nums[i-1]< nums[i+1])
ans = Math.max(ans, l[i-1]+ r[i+1]);
return Math.min(ans+1, n);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funlongestIncreasingSubarray(nums: IntArray): Int {
val n = nums.size
val l = IntArray(n) { 1 }
val r = IntArray(n) { 1 }
var ans = 1for (i in1 until n)
l[i] = if (nums[i] > nums[i-1]) l[i-1] + 1else1for (i in n-2 downTo 0)
r[i] = if (nums[i] < nums[i+1]) r[i+1] + 1else1 ans = l.maxOrNull() ?:1for (i in1 until n-1)
if (nums[i-1] < nums[i+1])
ans = maxOf(ans, l[i-1] + r[i+1])
return minOf(ans+1, n)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
deflongest_increasing_subarray(nums: list[int]) -> int:
n = len(nums)
l = [1] * n
r = [1] * n
ans =1for i in range(1, n):
l[i] = l[i-1] +1if nums[i] > nums[i-1] else1for i in range(n-2, -1, -1):
r[i] = r[i+1] +1if nums[i] < nums[i+1] else1 ans = max(l)
for i in range(1, n-1):
if nums[i-1] < nums[i+1]:
ans = max(ans, l[i-1] + r[i+1])
return min(ans+1, n)
impl Solution {
pubfnlongest_increasing_subarray(nums: Vec<i32>) -> i32 {
let n = nums.len();
letmut l =vec![1; n];
letmut r =vec![1; n];
letmut ans =1;
for i in1..n {
if nums[i] > nums[i-1] {
l[i] = l[i-1] +1;
}
}
for i in (0..n-1).rev() {
if nums[i] < nums[i+1] {
r[i] = r[i+1] +1;
}
}
ans =*l.iter().max().unwrap();
for i in1..n-1 {
if nums[i-1] < nums[i+1] {
ans = ans.max(l[i-1] + r[i+1]);
}
}
ans = ans +1;
if ans > n {
n asi32 } else {
ans asi32 }
}
}