-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode030225.java
More file actions
31 lines (25 loc) · 800 Bytes
/
Copy pathLeetcode030225.java
File metadata and controls
31 lines (25 loc) · 800 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
//https://leetcode.com/problems/longest-strictly-increasing-or-strictly-decreasing-subarray/?envType=daily-question&envId=2025-02-03
class Solution {
public int longestMonotonicSubarray(int[] nums) {
int n =nums.length;
if(n==1) return 1;
int[] dp =new int[n];
Arrays.fill(dp,1);
int maxi=0;
for(int i=1;i<n;i++){
if(nums[i]>nums[i-1] && dp[i]<dp[i-1]+1){
dp[i]=dp[i-1]+1;
}
maxi=Math.max(maxi,dp[i]);
}
int[] dp1 =new int[n];
Arrays.fill(dp1,1);
for(int i=n-2;i>=0;i--){
if(nums[i]>nums[i+1] && dp1[i]<dp1[i+1]+1){
dp1[i]=dp1[i+1]+1;
}
maxi=Math.max(maxi,dp1[i]);
}
return maxi;
}
}