63 最大的leftMax与rightMax之差的绝对值
Table of Contents

ac

import java.util.*;


public class Main {

    public static Scanner sc = new Scanner(System.in);

    static long getAns(int[] arr, int n){
        long[] leftMax= new long[n+1];
        long[] rightMax = new long[n+1];

        leftMax[0] = Integer.MIN_VALUE;
        for(int i=0;i<n;i++){
            if(arr[i] > leftMax[i]) {
                leftMax[i + 1] = arr[i];
            }else {
                leftMax[i + 1] = leftMax[i];
            }
        }

        rightMax[n] = Integer.MIN_VALUE;
        for(int i=n-1;i>=0;i--){
            if(arr[i] > rightMax[i+1]) {
                rightMax[i] = arr[i];
            }else {
                rightMax[i] = rightMax[i+1];
            }
        }

        long ans = 0;
        for(int i=0;i<n-1;i++){
            long left = leftMax[i+1];
            long right = rightMax[i+1];
            if(Math.abs(left - right) > ans){
                ans = Math.abs(left - right);
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        int n = sc.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;i++){
            arr[i] = sc.nextInt();
        }
        System.out.println(getAns(arr,n));
    }

}
/*
5
2 7 3 1 1

6
 */