2334. 元素值大于变化阈值的子数组

143 阅读1分钟

给你一个整数数组 nums 和一个整数 threshold 。

找到长度为 k 的 nums 子数组,满足数组中 每个 元素都 大于 threshold / k 。

请你返回满足要求的 任意 子数组的 大小 。如果没有这样的子数组,返回 -1 。

子数组 是数组中一段连续非空的元素序列。

示例 1:

输入: nums = [1,3,4,3,1], threshold = 6
输出: 3
解释: 子数组 [3,4,3] 大小为 3 ,每个元素都大于 6 / 3 = 2 。
注意这是唯一合法的子数组。

示例 2:

输入: nums = [6,5,6,5,8], threshold = 7
输出: 1
解释:子数组 [8] 大小为 1 ,且 8 > 7 / 1 = 7 。所以返回 1 。
注意子数组 [6,5] 大小为 2 ,每个元素都大于 7 / 2 = 3.5 。
类似的,子数组 [6,5,6][6,5,6,5][6,5,6,5,8] 都是符合条件的子数组。
所以返回 2, 3, 4 和 5 都可以。

题解:

/**
 * @param {number[]} nums
 * @param {number} threshold
 * @return {number}
 */
var validSubarraySize = function (nums, threshold) {
    let n = nums.length;
    const stack = []
    stack.push(-1) // 左边界-1
    for (let i = 0; i <= n; i++) {
        // 单调递增栈 栈顶值 大于 当前值  nums[stack[stack.length - 1]] > nums[i]
        // i == n 计算到最后一位处理
        while ( stack[stack.length - 1] != -1 
                && (i == n || nums[stack[stack.length - 1]] > nums[i])
              ) {
            // 子数组中任意值得最小值
            let h = nums[stack.pop()];
            // w 为子数组的长度
            let w = i - stack[stack.length - 1] - 1
            // h > (threshold / w)  等价于  w * h > threshold
            if (w * h > threshold) return w
        }
        stack.push(i)
    }
    return -1
};

来源:力扣(LeetCode)

链接:leetcode.cn/problems/su…

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。