classSolution{publicintmaxValueAfterReverse(int[] nums){int n = nums.length;int raw =sum(nums);//计算未翻转时的数组值int delta =0;//处理两个边界条件for(int r =0; r < n-1; r++){
delta = Math.max(delta, Math.abs(nums[0]-nums[r+1])-Math.abs(nums[r]-nums[r+1]));}for(int l =0; l < n -1; l++){
delta = Math.max(delta, Math.abs(nums[l]-nums[n-1])-Math.abs(nums[l]-nums[l+1]));}//计算其余情况for(int r =2; r < n-1; r++){for(int l =1; l < r; l++){
delta = Math.max(delta,(Math.abs(nums[l]-nums[r+1])+Math.abs(nums[l-1]-nums[r]))-(Math.abs(nums[l-1]-nums[l])+Math.abs(nums[r]-nums[r+1])));}}return raw + delta;}privateintsum(int[] nums){int sum =0;for(int i=0; i < nums.length -1; i++){
sum+= Math.abs(nums[i]- nums[i+1]);}return sum;}}
去绝对值
classSolution{publicintmaxValueAfterReverse(int[] nums){int n = nums.length;int raw =sum(nums);//计算未翻转时的数组值int delta =0;//处理两个边界条件for(int r =0; r < n-1; r++){
delta = Math.max(delta, Math.abs(nums[0]-nums[r+1])-Math.abs(nums[r]-nums[r+1]));}for(int l =0; l < n -1; l++){
delta = Math.max(delta, Math.abs(nums[l]-nums[n-1])-Math.abs(nums[l]-nums[l+1]));}int[] dp =newint[4];
Arrays.fill(dp,Integer.MIN_VALUE);for(int i =1; i < n; i++){int x = Math.abs(nums[i-1]- nums[i]);
dp[0]=Math.max(dp[0],nums[i]-nums[i-1]-x);
dp[1]=Math.max(dp[1],-nums[i]-nums[i-1]-x);
dp[2]=Math.max(dp[2],nums[i]+nums[i-1]-x);
dp[3]=Math.max(dp[3],-nums[i]+nums[i-1]-x);}
delta = Math.max(delta,Math.max(dp[0]+dp[3],dp[1]+dp[2]));return raw + delta;}privateintsum(int[] nums){int sum =0;for(int i=0; i < nums.length -1; i++){
sum+= Math.abs(nums[i]- nums[i+1]);}return sum;}}