动态规划子数组系列一>最长湍流子数组
1.题目:
解析:
代码:
public int maxTurbulenceSize(int[] arr) {int n = arr.length;int[] f = new int[n];int[] g = new int[n];for(int i = 0; i < n; i++)f[i] = g[i] = 1;int ret = 1;for(int i = 1; i <= n-1; i+,m. l.kmddsfsdafsd+){int a = arr[i-1]; int b = arr[i];if(a > b) g[i] = f[i-1]+1;//全部初始化为1,这里f表不用考虑else if(a < b) f[i] = g[i-1]+1;//全部初始化为1,这里g表不用考虑ret = Math.max(ret,Math.max(f[i],g[i]));}return ret;}