3850. Count Sequences to K
Description
You are given an integer array nums, and an integer k.
Start with an initial value val = 1 and process nums from left to right. At each index i, you must choose exactly one of the following actions:
- Multiply
valbynums[i]. - Divide
valbynums[i]. - Leave
valunchanged.
After processing all elements, val is considered equal to k only if its final rational value exactly equals k.
Return the count of distinct sequences of choices that result in val == k.
Note: Division is rational (exact), not integer division. For example, 2 / 4 = 1 / 2.
Β
Example 1:
Input: nums = [2,3,2], k = 6
Output: 2
Explanation:
The following 2 distinct sequences of choices result in val == k:
| Sequence | Operation on nums[0] | Operation on nums[1] | Operation on nums[2] | Final val |
|---|---|---|---|---|
| 1 | Multiply: val = 1 * 2 = 2 | Multiply: val = 2 * 3 = 6 | Leave val unchanged | 6 |
| 2 | Leave val unchanged | Multiply: val = 1 * 3 = 3 | Multiply: val = 3 * 2 = 6 | 6 |
Example 2:
Input: nums = [4,6,3], k = 2
Output: 2
Explanation:
The following 2 distinct sequences of choices result in val == k:
| Sequence | Operation on nums[0] | Operation on nums[1] | Operation on nums[2] | Final val |
|---|---|---|---|---|
| 1 | Multiply: val = 1 * 4 = 4 | Divide: val = 4 / 6 = 2 / 3 | Multiply: val = (2 / 3) * 3 = 2 | 2 |
| 2 | Leave val unchanged | Multiply: val = 1 * 6 = 6 | Divide: val = 6 / 3 = 2 | 2 |
Example 3:
Input: nums = [1,5], k = 1
Output: 3
Explanation:
The following 3 distinct sequences of choices result in val == k:
| Sequence | Operation on nums[0] | Operation on nums[1] | Final val |
|---|---|---|---|
| 1 | Multiply: val = 1 * 1 = 1 | Leave val unchanged | 1 |
| 2 | Divide: val = 1 / 1 = 1 | Leave val unchanged | 1 |
| 3 | Leave val unchanged | Leave val unchanged | 1 |
Β
Constraints:
1 <= nums.length <= 191 <= nums[i] <= 61 <= k <= 1015
Solutions
Solution 1: Memoization Search
We define a function \(\text{dfs}(i, p, q)\) that represents the number of different choice sequences when processing at index \(i\) with the current rational value being \(\frac{p}{q}\). Initially, \(\text{dfs}(0, 1, 1)\) represents starting from the initial value of \(1\).
For each index \(i\), we have three choices:
- Keep it unchanged, i.e., \(\text{dfs}(i + 1, p, q)\).
- Multiply by \(nums[i]\), i.e., \(\text{dfs}(i + 1, p \cdot nums[i], q)\).
- Divide by \(nums[i]\), i.e., \(\text{dfs}(i + 1, p, q \cdot nums[i])\).
To avoid excessively large numbers, we simplify the numerator and denominator after each multiplication or division. Finally, when \(i\) equals \(n\), if \(\frac{p}{q}\) exactly equals \(k\), we return \(1\); otherwise, we return \(0\).
The time complexity is \(O(n^4 + \log k)\), and the space complexity is \(O(n^4)\), where \(n\) is the length of the array \(\textit{nums}\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
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 37 | |
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 37 38 39 40 41 42 43 44 45 46 47 | |
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 37 | |