zoukankan      html  css  js  c++  java
  • 左侧数组最大值减去右侧数组最大值的最大绝对值

    package com.srie.test;
    
    
    public class Test02 {
    
        public int sol(int[] nums) {
            if (nums == null || nums.length == 0 || nums.length == 1) {
                return 0;
            }
            int n = nums.length;
            // store the right max in an array;
            int rightmax[] = new int[n];
    
            rightmax[n - 1] = nums[n - 1]; // just save the last element; 
            int max = nums[n - 1];
            for (int i = n - 2; i >= 0; --i) {
                max = Math.max(nums[i], max); 
                rightmax[i] = Math.max(rightmax[i + 1], max); // store the max element;
            }
            // System.out.println(Arrays.toString(rightmax));
    
            // left max element
            int leftmax = nums[0];
            int res = Math.abs(leftmax - rightmax[1]);
    
            for (int i = 1; i < n - 1; i++) {
                leftmax = nums[i];
                res = Math.max(res, Math.abs(leftmax - rightmax[i + 1]));
            }
            return res;
        }
    
        public static void main(String[] args) {
            int[] a = { 1, 2, 4, 8 };
            Test02 t02 = new Test02();
            int i = t02.sol(a);
            System.out.println(i);
            int[] b = { 1, 3, -3 };
            System.out.println(t02.sol(b));
            // maxmaxdifference
            // bugfixingCoins
        }
    
    }
  • 相关阅读:
    PHP安装linux
    nginx 安装
    Redis安装
    linux启动http服务
    收藏的有用的网页
    laravel框架部署后有用命令
    .net 报错access to the path c: empimagefilesmsc_cntr_0.txt is denied
    oracle 触发器
    学习Auxre记录
    mysql数据库索引
  • 原文地址:https://www.cnblogs.com/stono/p/6430766.html
Copyright © 2011-2022 走看看