220. Contains Duplicate III Hard
1/**
2 * [220] Contains Duplicate III
3 *
4 * You are given an integer array nums and two integers indexDiff and valueDiff.
5 * Find a pair of indices (i, j) such that:
6 *
7 * i != j,
8 * abs(i - j) <= indexDiff.
9 * abs(nums[i] - nums[j]) <= valueDiff, and
10 *
11 * Return true if such pair exists or false otherwise.
12 *
13 * <strong class="example">Example 1:
14 *
15 * Input: nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
16 * Output: true
17 * Explanation: We can choose (i, j) = (0, 3).
18 * We satisfy the three conditions:
19 * i != j --> 0 != 3
20 * abs(i - j) <= indexDiff --> abs(0 - 3) <= 3
21 * abs(nums[i] - nums[j]) <= valueDiff --> abs(1 - 1) <= 0
22 *
23 * <strong class="example">Example 2:
24 *
25 * Input: nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
26 * Output: false
27 * Explanation: After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false.
28 *
29 *
30 * Constraints:
31 *
32 * 2 <= nums.length <= 10^5
33 * -10^9 <= nums[i] <= 10^9
34 * 1 <= indexDiff <= nums.length
35 * 0 <= valueDiff <= 10^9
36 *
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/contains-duplicate-iii/
41// discuss: https://leetcode.com/problems/contains-duplicate-iii/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46 pub fn contains_nearby_almost_duplicate(nums: Vec<i32>, index_diff: i32, value_diff: i32) -> bool {
47 false
48 }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn test_220() {
59 }
60}
61
Back
© 2025 bowen.ge All Rights Reserved.