1799. Maximize Score After N Operations Hard

@problem@discussion
#Array#Math#Dynamic Programming#Backtracking#Bit Manipulation#Number Theory#Bitmask



1/**
2 * [1799] Maximize Score After N Operations
3 *
4 * You are given nums, an array of positive integers of size 2 * n. You must perform n operations on this array.
5 * In the i^th operation (1-indexed), you will:
6 * 
7 * 	Choose two elements, x and y.
8 * 	Receive a score of i * gcd(x, y).
9 * 	Remove x and y from nums.
10 * 
11 * Return the maximum score you can receive after performing n operations.
12 * The function gcd(x, y) is the greatest common divisor of x and y.
13 *  
14 * Example 1:
15 * 
16 * Input: nums = [1,2]
17 * Output: 1
18 * Explanation: The optimal choice of operations is:
19 * (1 * gcd(1, 2)) = 1
20 * 
21 * Example 2:
22 * 
23 * Input: nums = [3,4,6,8]
24 * Output: 11
25 * Explanation: The optimal choice of operations is:
26 * (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11
27 * 
28 * Example 3:
29 * 
30 * Input: nums = [1,2,3,4,5,6]
31 * Output: 14
32 * Explanation: The optimal choice of operations is:
33 * (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14
34 * 
35 *  
36 * Constraints:
37 * 
38 * 	1 <= n <= 7
39 * 	nums.length == 2 * n
40 * 	1 <= nums[i] <= 10^6
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/maximize-score-after-n-operations/
46// discuss: https://leetcode.com/problems/maximize-score-after-n-operations/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn max_score(nums: Vec<i32>) -> i32 {
52        0
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_1799() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.