2020-02-11 12:01:21

问题描述:

问题求解:

本题的难度个人感觉还是蛮大的,主要是不容易想到O(n)的解。

对于 ...a, [b, ... , c], d, ...,如果我们将其中的[b, ... , c]进行翻转。

如果两线段有重复,必减小原先的值。

如果两线段无重复,必增加原先的值,且diff为2 * gap。

可通过如下的图进行分类讨论。

最后,再对边界做一个处理即可。

int inf = (int)1e9;
public int maxValueAfterReverse(int[] nums) {
int res = 0;
int n = nums.length;
int diff = 0;
for (int i = 0; i < n - 1; i++) res += Math.abs(nums[i + 1] - nums[i]); // 端点不在两顶点
int min = inf;
int max = -inf;
for (int i = 0; i <= n - 2; i++) {
min = Math.min(min, Math.max(nums[i], nums[i + 1]));
max = Math.max(max, Math.min(nums[i], nums[i + 1]));
}
diff = Math.max(diff, 2 * (max - min)); // 一端点在左顶点
for (int i = 1; i <= n - 2; i++) {
diff = Math.max(diff, Math.abs(nums[i + 1] - nums[0]) - Math.abs(nums[i + 1] - nums[i]));
} // 一端点在右顶点
for (int i = 1; i <= n - 2; i++) {
diff = Math.max(diff, Math.abs(nums[i - 1] - nums[n - 1]) - Math.abs(nums[i - 1] - nums[i]));
} return res + diff;
}

  

数学-绝对值-Reverse Subarray To Maximize Array Value的更多相关文章

  1. Reverse Subarray To Maximize Array Value

    2020-02-03 20:43:46 问题描述: 问题求解: public boolean canTransform(String start, String end) { int n = star ...

  2. LeetCode之“数学”:Reverse Integer && Reverse Bits

    1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2:  ...

  3. Get the largest sum of contiguous subarray in an int array

    When I finished reading this problem,I thought I could solve it by scanning every single subarray in ...

  4. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  5. 152. Maximum Product Subarray (Array; DP)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. 53. Maximum Subarray (Array; DP)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. js常用API 数据类型 基本类型,基本包装类型,引用类型 Object String Array Boolean Number Date Math

    数据类型 变量.作用域及内存 基础类型(primitive value):Undefined.Null.Boolean.Number和String.这些类型在内存中分别占用固定大小的空间,他们的值保存 ...

  8. array题目合集

    414. Third Maximum Number 给一个非空的整数数组,找到这个数组中第三大的值,如果不存在,那么返回最大的值.要求时间复杂度为o(n) 例如: Example 1: Input: ...

  9. leetcode-Maximum Subarray

    https://leetcode.com/problems/maximum-subarray/ Find the contiguous subarray within an array (contai ...

随机推荐

  1. Jupyter自定义设置详解

    今天专门花时间总结梳理一下jupyter的一些高级设置,jupyter我已经介绍过一次基本内容了,Setup and Linux | James Chen's Blogs,尤其是如何在服务器运行jup ...

  2. 【Hardware】i386、x86和x64的故事

    (1)x86的由来 x86架构首度出现在1978年推出的Intel 8086中央处理器,它是从Intel 8008处理器中发展而来的,而8008则是发展自Intel 4004的.在8086之后,Int ...

  3. Golang 使用Protocol Buffer 案例

    目录 1. 前言 2. Protobuf 简介 2.1 Protobuf 优点 2.2 Protobuf 缺点 2.3 Protobuf Golang 安装使用 3. Protobuf 通讯案例 3. ...

  4. 使用QT绘制一个多边形

    目录 1. 概述 2. 实现 2.1. 代码 2.2. 解析 3. 结果 1. 概述 可以通过QT的重绘事件和鼠标事件来绘制多边形,最简单的办法就是在继承QWidget的窗体中重写paintEvent ...

  5. 一步步去阅读koa源码,整体架构分析

    阅读好的框架的源码有很多好处,从大神的视角去理解整个框架的设计思想.大到架构设计,小到可取的命名风格,还有设计模式.实现某类功能使用到的数据结构和算法等等. 使用koa 其实某个框架阅读源码的时候,首 ...

  6. SpringBoot入门系列(四)整合模板引擎Thymeleaf

    前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...

  7. Android开发进阶 -- 通用适配器 CommonAdapter

    在Android开发中,我们经常会用到ListView 这个组件,为了将ListView 的内容展示出来,我们会去实现一个Adapter来适配,将Layout中的布局以列表的形式展现到组件中.     ...

  8. 【转】Maven详细

    Maven maven 中央仓库 网站 https://mvnrepository.com/ 全世界 发布到Maven仓库 供用类着使用 maven 本质上下载工具和构建工具 下载工具 迅雷 只能下载 ...

  9. 20 本地SQL查询

    Spring Data JPA同样也支持sql语句的查询 //nativeQuery : 使用本地sql的方式查询 @Query(value="select * from customer& ...

  10. 内网渗透之信息收集-windows系统篇

    windows 用户相关 query user #查看当前在线的用户 whoami #查看当前用户 net user #查看当前系统全部用户 net1 user #查看当前系统全部用户(高权限命令) ...