Leetcode1499满足不等式的最大值

问题分析

双端队列按照y-x的值从大到小组织,队列中存储点的编号。

如果y-x的值大于队列尾部元素的y-x值,则从尾部弹出元素。

如果当前点的x值与队列头部元素的x值之差大于k时,则从头部弹出元素。

求解代码

java 复制代码
public static int MAXN = 100001;
	public static int[][] deque = new int[MAXN][2];
	public static int h,t;

	public static int findMaxValueOfEquation(int[][] points,int k){
		h = t = 0;
		int n = points.length;
		int ans = Integer.MIN_VALUE;
		for(int i=0,x,y;i<n;i++){
			x=points[i][0];
			y=points[i][1];

			while (h<t&&deque[h][0]+k<x) {
				h++;
			}

			if(h<t){
				ans = Math.max(ans, x+y+deque[h][1]-deque[h][0]);
			}

			while (h<t&&deque[t-1][1]-deque[t-1][0]<=y-x) {
				t--;
			}

			deque[t][0]=x;
			deque[t++][1]=y;
		}
		return ans;
	}
相关推荐
devilnumber1 小时前
Java 递归算法 详解 + 核心要点 + 实战运用 + 避坑指南
java·开发语言·算法
asdfg12589633 小时前
JavaBean是什么?怎么理解?有什么用途?
java·开发语言
dsyyyyy11013 小时前
JavaScript变量
开发语言·javascript·ecmascript
z落落4 小时前
C#WinForm 窗体切换与窗体传值(登录跳转案例)+WinForm 窗体传值(从上往下传、从下往上传)
开发语言·windows·c#
allway24 小时前
How to Echo Multiline to a File in Bash [3 Methods]
开发语言·chrome·bash
weixin_462446234 小时前
手把手教你用 Bash 脚本自动更新 /etc/hosts —— 自动绑定网卡 IP 与节点名
开发语言·tcp/ip·bash
一个梦醒了4 小时前
安装git bash选项推荐
开发语言·git·bash
摇滚侠4 小时前
SpringMVC 入门到实战 文件上传 75-77
java·后端·spring·maven·intellij-idea
GIS数据转换器4 小时前
城市排水生命线安全运行监测平台深度解析
java·运维·人工智能·python·安全·数据挖掘·无人机
ct9785 小时前
React 状态管理方案深度对比
开发语言·前端·react