2670. Find the Distinct Difference Array Easy

@problem@discussion
#Array#Hash Table



1/**
2 * [2670] Find the Distinct Difference Array
3 *
4 * You are given a 0-indexed array nums of length n.
5 * The distinct difference array of nums is an array diff of length n such that diff[i] is equal to the number of distinct elements in the suffix nums[i + 1, ..., n - 1] subtracted from the number of distinct elements in the prefix nums[0, ..., i].
6 * Return the distinct difference array of nums.
7 * Note that nums[i, ..., j] denotes the subarray of nums starting at index i and ending at index j inclusive. Particularly, if i > j then nums[i, ..., j] denotes an empty subarray.
8 *  
9 * <strong class="example">Example 1:
10 * 
11 * Input: nums = [1,2,3,4,5]
12 * Output: [-3,-1,1,3,5]
13 * Explanation: For index i = 0, there is 1 element in the prefix and 4 distinct elements in the suffix. Thus, diff[0] = 1 - 4 = -3.
14 * For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1.
15 * For index i = 2, there are 3 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 3 - 2 = 1.
16 * For index i = 3, there are 4 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 4 - 1 = 3.
17 * For index i = 4, there are 5 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 5 - 0 = 5.
18 * 
19 * <strong class="example">Example 2:
20 * 
21 * Input: nums = [3,2,3,4,2]
22 * Output: [-2,-1,0,2,3]
23 * Explanation: For index i = 0, there is 1 element in the prefix and 3 distinct elements in the suffix. Thus, diff[0] = 1 - 3 = -2.
24 * For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1.
25 * For index i = 2, there are 2 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 2 - 2 = 0.
26 * For index i = 3, there are 3 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 3 - 1 = 2.
27 * For index i = 4, there are 3 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 3 - 0 = 3.
28 * 
29 *  
30 * Constraints:
31 * 
32 * 	1 <= n == nums.length <= 50
33 * 	1 <= nums[i] <= 50
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/find-the-distinct-difference-array/
39// discuss: https://leetcode.com/problems/find-the-distinct-difference-array/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn distinct_difference_array(nums: Vec<i32>) -> Vec<i32> {
45        vec![]
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_2670() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.