3098. Find the Sum of Subsequence Powers
Description
You are given an integer array nums
of length n
, and a positive integer k
.
The power of a subsequence is defined as the minimum absolute difference between any two elements in the subsequence.
Return the sum of powers of all subsequences of nums
which have length equal to k
.
Since the answer may be large, return it modulo 109 + 7
.
Example 1:
Input: nums = [1,2,3,4], k = 3
Output: 4
Explanation:
There are 4 subsequences in nums
which have length 3: [1,2,3]
, [1,3,4]
, [1,2,4]
, and [2,3,4]
. The sum of powers is |2 - 3| + |3 - 4| + |2 - 1| + |3 - 4| = 4
.
Example 2:
Input: nums = [2,2], k = 2
Output: 0
Explanation:
The only subsequence in nums
which has length 2 is [2,2]
. The sum of powers is |2 - 2| = 0
.
Example 3:
Input: nums = [4,3,-1], k = 2
Output: 10
Explanation:
There are 3 subsequences in nums
which have length 2: [4,3]
, [4,-1]
, and [3,-1]
. The sum of powers is |4 - 3| + |4 - (-1)| + |3 - (-1)| = 10
.
Constraints:
2 <= n == nums.length <= 50
-108 <= nums[i] <= 108
2 <= k <= n
Solutions
Solution 1: Memoization Search
Given the problem involves the minimum difference between elements of a subsequence, we might as well sort the array $\textit{nums}$, which facilitates the calculation of the minimum difference between subsequence elements.
Next, we design a function $dfs(i, j, k, mi)$, representing the value of the energy sum when processing the $i$-th element, the last selected element is the $j$-th element, $k$ more elements need to be selected, and the current minimum difference is $mi$. Therefore, the answer is $dfs(0, n, k, +\infty)$ (If the last selected element is the $n$-th element, it indicates that no element has been selected before).
The execution process of the function $dfs(i, j, k, mi)$ is as follows:
- If $i \geq n$, it means all elements have been processed. If $k = 0$, return $mi$; otherwise, return $0$.
- If the remaining number of elements $n - i$ is less than $k$, return $0$.
- Otherwise, we can choose not to select the $i$-th element, and the energy sum obtained is $dfs(i + 1, j, k, mi)$.
- We can also choose to select the $i$-th element. If $j = n$, it means no element has been selected before, then the energy sum obtained is $dfs(i + 1, i, k - 1, mi)$; otherwise, the energy sum obtained is $dfs(i + 1, i, k - 1, \min(mi, \textit{nums}[i] - \textit{nums}[j]))$.
- We add up the above results and return the result modulo $10^9 + 7$.
To avoid repeated calculations, we can use memoization, saving the results that have already been calculated.
The time complexity is $O(n^4 \times k)$, and the space complexity is $O(n^4 \times k)$. Here, $n$ is the length of the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|