2834. Find the minimum sum of a beautiful array
Question description
Given two positive integers n and target, the goal is to find a length of n array that satisfies the following conditions:
- An array consists of two different positive integers.
- There are no two different subscripts
iandjmakenums[i] + nums[j] == target。
Returns the smallest possible sum of a beautiful array that meets the conditions, and performs the10^9 + 7Take the mold.
test case
Example 1:
- Input: n = 2, target = 3
- Output: 4
Example 2:
- Input: n = 3, target = 3
- Output: 8
Example 3:
- Input: n = 1, target = 1
- Output: 1
Original idea
Plan
The initial solution is to start with the smallest number and check each number one by one whether it can be added to the array, while ensuring that there are no two numbers whose sum is equal to target。
- from
1Start trying to add numbers to the array one by one. - For each number, check if adding it to a number already in the array gives
target。 - If not, add it to the array.
- Continue this process until the array length reaches
n。
code
1 | def minimumPossibleSum(n, target): |
Complexity analysis
- Time complexity: O(n^2), because the addition of each number requires traversing the selected number set.
- Space complexity: O(n) for storing the selected set of numbers.
greedy optimization
Plan
Optimization strategy:
- Avoid the use of collections:
- Introduce the "avoid" collection to store all the results obtained by adding the selected number
targetnumber. - This allows a quick check to see if the new number will result in a sum of
targetsituation.
- Introduce the "avoid" collection to store all the results obtained by adding the selected number
- direct inspection:
- Each time a new number is selected, only check if it is in the "avoid" set.
- Numbers not in the set are considered safe and can be added directly.
- Dynamic update avoid collection:
- When new numbers are added to the beautiful array, the corresponding
target - 新数字Also added to the "Avoid" collection. - This ensures that any possible composition with new numbers
targetnumbers will be avoided in the future.
- When new numbers are added to the beautiful array, the corresponding
- Avoid the use of collections:
Optimized time complexity:
- Only one set check is required per number.
- The time complexity is reduced to O(n), which significantly improves the algorithm efficiency.
code
1 | def minimumPossibleSum_optimized(n, target): |
Complexity analysis
- Time complexity: O(n) since each number only needs to be checked once.
- Space complexity: O(n) for storing selected numbers and avoiding collections of numbers.
mathematical methods
Plan
In response to the above problems, we adopted a more efficient mathematical method to solve this problem. This method reduces the necessary amount of calculations by analyzing the mathematical nature of the problem, and is particularly suitable for processing large-scale data.
Problem decomposition:
- First, we break the problem into two parts. Since the numbers in the array are all unique, and the sum of two different numbers cannot be equal to
target, we select first starting with the smallest number until we can no longer select any more numbers without violating the rules of sum.
- First, we break the problem into two parts. Since the numbers in the array are all unique, and the sum of two different numbers cannot be equal to
Select the first half of the number:
- in
1Arrivetarget-1Within the range, some numbers cannot appear at the same time. For example, iftargetYes6, then1and5、2and4cannot occur at the same time because their sum is equal to6. However,3(whentargetis an even number) or3and2(whentargetis an odd number) can be selected. - This means we are free to choose from
1Arrivemof numbers, among whichm = min(⌊target/2⌋, n). For this part of the numbers, we can directly use the summation formula of the arithmetic sequence to calculate their sum, that ism * (m + 1) / 2。
- in
Select the second half of the number:
- Once we select the former
mnumbers, the remaining number that needs to be selected isn - m. Since we have chosen1Arrivem, we now need to start fromtargetStart selecting the remaining numbers. - if
ngreater thanm, then we will start fromtargetStart continuous selectionn - mnumber. The sum of these numbers can be calculated using the summation formula of an arithmetic sequence:(2 * target + n - m - 1) * (n - m) / 2。
- Once we select the former
Calculate the sum and take the modulo:
- We add the sum of the two parts and do the
10^9 + 7Take modulo to get the final answer.
- We add the sum of the two parts and do the
realize
- First part sum: from
1Arrivemin(target // 2, n)of and. - Second part and (if required): from
targetstart, choosen - min(target // 2, n)sum of numbers. - Add the sums of these two parts to get the minimum sum of a beautiful array that meets the conditions.
Select less than
target // 2number:- As we start from 1 and gradually increase the number until
target // 2, these numbers cannot be added to other numbers in the array to gettarget。 - For example, if
targetis 10, thentarget // 2Yes 5. In this case, any two numbers between 1 and 5 will never add up to 10. - Therefore, this part of the selection is safe, and since we need the minimum sum, we start from 1 and increase one by one.
- As we start from 1 and gradually increase the number until
**When
ngreater thantarget // 2**:- if
ngreater thantarget // 2, which means only select less thantarget // 2There are not enough numbers to fill the array. - In this case we need to keep selecting more numbers, but to avoid and for
targetThe combination of we need fromtargetStart choosing yourself. - We continue to increase one by one until the array length reaches
n。
- if
Calculate the sum:
- The first part is from 1 to
min(target // 2, n)of and. - The second part (if needed) is from
targetGo ahead and choose the restn - min(target // 2, n)number. - Finally, adding the sum of these two parts is the minimum sum we are looking for.
- The first part is from 1 to
This is a typical example of solving problems through mathematical methods, which avoids complex programming logic and provides a more concise and efficient solution.
code
1 | def minimumPossibleSum_math_approach(n, target): |
Complexity analysis
- Time complexity: O(1) since the result is calculated directly.
- Space complexity: O(1), only a fixed number of variables are used.