zoukankan      html  css  js  c++  java
  • LeetCde 1499 满足不等式的最大值

    单调队列,维护合法窗口内的当前最大值(队头)及未来可能成为最大值的元素下标。

    class Solution {
        public int findMaxValueOfEquation(int[][] p, int k) {
            int n = p.length;
            int res = Integer.MIN_VALUE;
            LinkedList<Integer> q = new LinkedList<>();
            for(int j=0; j < n; j++) {
                //1. 维护窗口合法
                while(!q.isEmpty() && p[j][0] - p[q.peek()][0] > k)
                    q.removeFirst();
                //2. 更新答案
                if(!q.isEmpty()) {
                    int y = p[q.peek()][1], x = p[q.peek()][0];
                    res = Math.max(res, p[j][0] + p[j][1] + y - x);
                }
                //3. 保持队列单调递减的前提下,加入当前元素下标
                while(!q.isEmpty() && p[q.peekLast()][1] - p[q.peekLast()][0] < p[j][1] - p[j][0]) 
                    q.removeLast();
                q.offer(j);
            }
            return res;
        }
    }
    
  • 相关阅读:
    奇异值分解
    特征值和特征向量
    矩阵
    矢量化
    符号数组
    通用函数
    数据平滑
    多项式拟合
    协方差/相关矩阵/相关系数
    json
  • 原文地址:https://www.cnblogs.com/lixyuan/p/13300800.html
Copyright © 2011-2022 走看看