2334. Subarray With Elements Greater Than Varying Threshold Hard

@problem@discussion
#Array#Stack#Union Find#Monotonic Stack



1/**
2 * [2334] Subarray With Elements Greater Than Varying Threshold
3 *
4 * You are given an integer array nums and an integer threshold.
5 * Find any subarray of nums of length k such that every element in the subarray is greater than threshold / k.
6 * Return the size of any such subarray. If there is no such subarray, return -1.
7 * A subarray is a contiguous non-empty sequence of elements within an array.
8 *  
9 * Example 1:
10 * 
11 * Input: nums = [1,3,4,3,1], threshold = 6
12 * Output: 3
13 * Explanation: The subarray [3,4,3] has a size of 3, and every element is greater than 6 / 3 = 2.
14 * Note that this is the only valid subarray.
15 * 
16 * Example 2:
17 * 
18 * Input: nums = [6,5,6,5,8], threshold = 7
19 * Output: 1
20 * Explanation: The subarray [8] has a size of 1, and 8 > 7 / 1 = 7. So 1 is returned.
21 * Note that the subarray [6,5] has a size of 2, and every element is greater than 7 / 2 = 3.5. 
22 * Similarly, the subarrays [6,5,6], [6,5,6,5], [6,5,6,5,8] also satisfy the given conditions.
23 * Therefore, 2, 3, 4, or 5 may also be returned.
24 *  
25 * Constraints:
26 * 
27 * 	1 <= nums.length <= 10^5
28 * 	1 <= nums[i], threshold <= 10^9
29 * 
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/subarray-with-elements-greater-than-varying-threshold/
34// discuss: https://leetcode.com/problems/subarray-with-elements-greater-than-varying-threshold/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39    pub fn valid_subarray_size(nums: Vec<i32>, threshold: i32) -> i32 {
40        0
41    }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_2334() {
52    }
53}
54


Back
© 2025 bowen.ge All Rights Reserved.