|
| 1 | +/** |
| 2 | + * [1906] Minimum Absolute Difference Queries |
| 3 | + * |
| 4 | + * The minimum absolute difference of an array a is defined as the minimum value of |a[i] - a[j]|, where 0 <= i < j < a.length and a[i] != a[j]. If all elements of a are the same, the minimum absolute difference is -1. |
| 5 | + * |
| 6 | + * For example, the minimum absolute difference of the array [5,<u>2</u>,<u>3</u>,7,2] is |2 - 3| = 1. Note that it is not 0 because a[i] and a[j] must be different. |
| 7 | + * |
| 8 | + * You are given an integer array nums and the array queries where queries[i] = [li, ri]. For each query i, compute the minimum absolute difference of the subarray nums[li...ri] containing the elements of nums between the 0-based indices li and ri (inclusive). |
| 9 | + * Return an array ans where ans[i] is the answer to the i^th query. |
| 10 | + * A subarray is a contiguous sequence of elements in an array. |
| 11 | + * The value of |x| is defined as: |
| 12 | + * |
| 13 | + * x if x >= 0. |
| 14 | + * -x if x < 0. |
| 15 | + * |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * |
| 19 | + * Input: nums = [1,3,4,8], queries = [[0,1],[1,2],[2,3],[0,3]] |
| 20 | + * Output: [2,1,4,1] |
| 21 | + * Explanation: The queries are processed as follows: |
| 22 | + * - queries[0] = [0,1]: The subarray is [<u>1</u>,<u>3</u>] and the minimum absolute difference is |1-3| = 2. |
| 23 | + * - queries[1] = [1,2]: The subarray is [<u>3</u>,<u>4</u>] and the minimum absolute difference is |3-4| = 1. |
| 24 | + * - queries[2] = [2,3]: The subarray is [<u>4</u>,<u>8</u>] and the minimum absolute difference is |4-8| = 4. |
| 25 | + * - queries[3] = [0,3]: The subarray is [1,<u>3</u>,<u>4</u>,8] and the minimum absolute difference is |3-4| = 1. |
| 26 | + * |
| 27 | + * Example 2: |
| 28 | + * |
| 29 | + * Input: nums = [4,5,2,2,7,10], queries = [[2,3],[0,2],[0,5],[3,5]] |
| 30 | + * Output: [-1,1,1,3] |
| 31 | + * Explanation: The queries are processed as follows: |
| 32 | + * - queries[0] = [2,3]: The subarray is [2,2] and the minimum absolute difference is -1 because all the |
| 33 | + * elements are the same. |
| 34 | + * - queries[1] = [0,2]: The subarray is [<u>4</u>,<u>5</u>,2] and the minimum absolute difference is |4-5| = 1. |
| 35 | + * - queries[2] = [0,5]: The subarray is [<u>4</u>,<u>5</u>,2,2,7,10] and the minimum absolute difference is |4-5| = 1. |
| 36 | + * - queries[3] = [3,5]: The subarray is [2,<u>7</u>,<u>10</u>] and the minimum absolute difference is |7-10| = 3. |
| 37 | + * |
| 38 | + * |
| 39 | + * Constraints: |
| 40 | + * |
| 41 | + * 2 <= nums.length <= 10^5 |
| 42 | + * 1 <= nums[i] <= 100 |
| 43 | + * 1 <= queries.length <= 2 * 10^4 |
| 44 | + * 0 <= li < ri < nums.length |
| 45 | + * |
| 46 | + */ |
| 47 | +pub struct Solution {} |
| 48 | + |
| 49 | +// problem: https://leetcode.com/problems/minimum-absolute-difference-queries/ |
| 50 | +// discuss: https://leetcode.com/problems/minimum-absolute-difference-queries/discuss/?currentPage=1&orderBy=most_votes&query= |
| 51 | + |
| 52 | +// submission codes start here |
| 53 | + |
| 54 | +impl Solution { |
| 55 | + pub fn min_difference(nums: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i32> { |
| 56 | + vec![] |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// submission codes end |
| 61 | + |
| 62 | +#[cfg(test)] |
| 63 | +mod tests { |
| 64 | + use super::*; |
| 65 | + |
| 66 | + #[test] |
| 67 | + #[ignore] |
| 68 | + fn test_1906_example_1() { |
| 69 | + let nums = vec![4, 5, 2, 2, 7, 10]; |
| 70 | + let queries = vec![vec![2, 3], vec![0, 2], vec![0, 5], vec![3, 5]]; |
| 71 | + |
| 72 | + let result = vec![2, 1, 4, 1]; |
| 73 | + |
| 74 | + assert_eq!(Solution::min_difference(nums, queries), result); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + #[ignore] |
| 79 | + fn test_1906_example_2() { |
| 80 | + let nums = vec![4, 5, 2, 2, 7, 10]; |
| 81 | + let queries = vec![vec![2, 3], vec![0, 2], vec![0, 5], vec![3, 5]]; |
| 82 | + |
| 83 | + let result = vec![-1, 1, 1, 3]; |
| 84 | + |
| 85 | + assert_eq!(Solution::min_difference(nums, queries), result); |
| 86 | + } |
| 87 | +} |
0 commit comments