Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    1799 - Maximize Score After N Operations.

    Hard

    You are given nums, an array of positive integers of size 2 * n. You must perform n operations on this array.

    In the <code>i<sup>th</sup></code> operation (1-indexed) , you will:

    • Choose two elements, x and y.

    • Receive a score of i * gcd(x, y).

    • Remove x and y from nums.

    Return the maximum score you can receive after performing n operations.

    The function gcd(x, y) is the greatest common divisor of x and y.

    Example 1:

    Input: nums = 1,2

    Output: 1

    Explanation: The optimal choice of operations is:

    v(1 \* gcd(1, 2)) = 1

    Example 2:

    Input: nums = 3,4,6,8

    Output: 11

    Explanation: The optimal choice of operations is:

    (1 \* gcd(3, 6)) + (2 \* gcd(4, 8)) = 3 + 8 = 11

    Example 3:

    Input: nums = 1,2,3,4,5,6

    Output: 14

    Explanation: The optimal choice of operations is:

    (1 \* gcd(1, 5)) + (2 \* gcd(2, 4)) + (3 \* gcd(3, 6)) = 1 + 4 + 9 = 14

    Constraints:

    • 1 &lt;= n &lt;= 7

    • nums.length == 2 * n

    • <code>1 <= numsi<= 10<sup>6</sup></code>

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer maxScore(IntArray nums)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait