Skip to content

Commit 4af24eb

Browse files
committed
+ problem 1799
1 parent 51f7bfc commit 4af24eb

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# 1799. Maximize Score After N Operations
2+
You are given `nums`, an array of positive integers of size `2 * n`. You must perform `n` operations on this array.
3+
4+
In the <code>i<sup>th</sup></code> operation **(1-indexed)**, you will:
5+
* Choose two elements, `x` and `y`.
6+
* Receive a score of `i * gcd(x, y)`.
7+
* Remove `x` and `y` from `nums`.
8+
9+
Return *the maximum score you can receive after performing* `n` *operations*.
10+
11+
The function `gcd(x, y)` is the greatest common divisor of `x` and `y`.
12+
13+
#### Example 1:
14+
<pre>
15+
<strong>Input:</strong> nums = [1,2]
16+
<strong>Output:</strong> 1
17+
<strong>Explanation:</strong> The optimal choice of operations is:
18+
(1 * gcd(1, 2)) = 1
19+
</pre>
20+
21+
#### Example 2:
22+
<pre>
23+
<strong>Input:</strong> nums = [3,4,6,8]
24+
<strong>Output:</strong> 11
25+
<strong>Explanation:</strong> The optimal choice of operations is:
26+
(1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11
27+
</pre>
28+
29+
#### Example 3:
30+
<pre>
31+
<strong>Input:</strong> nums = [1,2,3,4,5,6]
32+
<strong>Output:</strong> 14
33+
<strong>Explanation:</strong> The optimal choice of operations is:
34+
(1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14
35+
</pre>
36+
37+
#### Constraints:
38+
* `1 <= n <= 7`
39+
* `nums.length == 2 * n`
40+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
41+
42+
## Solutions (Python)
43+
44+
### 1. Solution
45+
```Python
46+
import math
47+
from functools import cache
48+
49+
50+
class Solution:
51+
def maxScore(self, nums: List[int]) -> int:
52+
@cache
53+
def gcd(x: int, y: int) -> int:
54+
return math.gcd(x, y)
55+
56+
n = len(nums) // 2
57+
pairmask = [[] for _ in range(n + 1)]
58+
dp = [0] * (1 << len(nums))
59+
60+
for num in range(len(dp)):
61+
if bin(num).count('1') % 2 == 0:
62+
pairmask[bin(num).count('1') // 2].append(num)
63+
64+
for i in range(1, n + 1):
65+
for j in range(len(nums)):
66+
x = nums[j]
67+
for k in range(j + 1, len(nums)):
68+
y = nums[k]
69+
for prevmask in pairmask[i - 1]:
70+
mask = (1 << j) | (1 << k)
71+
if prevmask & mask == 0:
72+
dp[prevmask | mask] = max(
73+
dp[prevmask | mask], dp[prevmask] + i * gcd(x, y))
74+
75+
return dp[-1]
76+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# 1799. N 次操作后的最大分数和
2+
给你 `nums` ,它是一个大小为 `2 * n` 的正整数数组。你必须对这个数组执行 `n` 次操作。
3+
4+
在第 `i` 次操作时(操作编号从 **1** 开始),你需要:
5+
* 选择两个元素 `x``y`
6+
* 获得分数 `i * gcd(x, y)`
7+
*`x``y``nums` 中删除。
8+
9+
请你返回 `n` 次操作后你能获得的分数和最大为多少。
10+
11+
函数 `gcd(x, y)``x``y` 的最大公约数。
12+
13+
#### 示例 1:
14+
<pre>
15+
<strong>输入:</strong> nums = [1,2]
16+
<strong>输出:</strong> 1
17+
<strong>解释:</strong> 最优操作是:
18+
(1 * gcd(1, 2)) = 1
19+
</pre>
20+
21+
#### 示例 2:
22+
<pre>
23+
<strong>输入:</strong> nums = [3,4,6,8]
24+
<strong>输出:</strong> 11
25+
<strong>解释:</strong> 最优操作是:
26+
(1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11
27+
</pre>
28+
29+
#### 示例 3:
30+
<pre>
31+
<strong>输入:</strong> nums = [1,2,3,4,5,6]
32+
<strong>输出:</strong> 14
33+
<strong>解释:</strong> 最优操作是:
34+
(1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14
35+
</pre>
36+
37+
#### 提示:
38+
* `1 <= n <= 7`
39+
* `nums.length == 2 * n`
40+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
41+
42+
## 题解 (Python)
43+
44+
### 1. 题解
45+
```Python
46+
import math
47+
from functools import cache
48+
49+
50+
class Solution:
51+
def maxScore(self, nums: List[int]) -> int:
52+
@cache
53+
def gcd(x: int, y: int) -> int:
54+
return math.gcd(x, y)
55+
56+
n = len(nums) // 2
57+
pairmask = [[] for _ in range(n + 1)]
58+
dp = [0] * (1 << len(nums))
59+
60+
for num in range(len(dp)):
61+
if bin(num).count('1') % 2 == 0:
62+
pairmask[bin(num).count('1') // 2].append(num)
63+
64+
for i in range(1, n + 1):
65+
for j in range(len(nums)):
66+
x = nums[j]
67+
for k in range(j + 1, len(nums)):
68+
y = nums[k]
69+
for prevmask in pairmask[i - 1]:
70+
mask = (1 << j) | (1 << k)
71+
if prevmask & mask == 0:
72+
dp[prevmask | mask] = max(
73+
dp[prevmask | mask], dp[prevmask] + i * gcd(x, y))
74+
75+
return dp[-1]
76+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import math
2+
from functools import cache
3+
4+
5+
class Solution:
6+
def maxScore(self, nums: List[int]) -> int:
7+
@cache
8+
def gcd(x: int, y: int) -> int:
9+
return math.gcd(x, y)
10+
11+
n = len(nums) // 2
12+
pairmask = [[] for _ in range(n + 1)]
13+
dp = [0] * (1 << len(nums))
14+
15+
for num in range(len(dp)):
16+
if bin(num).count('1') % 2 == 0:
17+
pairmask[bin(num).count('1') // 2].append(num)
18+
19+
for i in range(1, n + 1):
20+
for j in range(len(nums)):
21+
x = nums[j]
22+
for k in range(j + 1, len(nums)):
23+
y = nums[k]
24+
for prevmask in pairmask[i - 1]:
25+
mask = (1 << j) | (1 << k)
26+
if prevmask & mask == 0:
27+
dp[prevmask | mask] = max(
28+
dp[prevmask | mask], dp[prevmask] + i * gcd(x, y))
29+
30+
return dp[-1]

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,7 @@
11561156
[1796][1796l]|[Second Largest Digit in a String][1796] |![rs]
11571157
[1797][1797l]|[Design Authentication Manager][1797] |![py]
11581158
[1798][1798l]|[Maximum Number of Consecutive Values You Can Make][1798] |![rs]
1159+
[1799][1799l]|[Maximize Score After N Operations][1799] |![py]
11591160
[1800][1800l]|[Maximum Ascending Subarray Sum][1800] |![rb]&nbsp;&nbsp;![rs]
11601161
[1801][1801l]|[Number of Orders in the Backlog][1801] |![rs]
11611162
[1802][1802l]|[Maximum Value at a Given Index in a Bounded Array][1802] |![rs]
@@ -2793,6 +2794,7 @@
27932794
[1796]:Problemset/1796-Second%20Largest%20Digit%20in%20a%20String/README.md#1796-second-largest-digit-in-a-string
27942795
[1797]:Problemset/1797-Design%20Authentication%20Manager/README.md#1797-design-authentication-manager
27952796
[1798]:Problemset/1798-Maximum%20Number%20of%20Consecutive%20Values%20You%20Can%20Make/README.md#1798-maximum-number-of-consecutive-values-you-can-make
2797+
[1799]:Problemset/1799-Maximize%20Score%20After%20N%20Operations/README.md#1799-maximize-score-after-n-operations
27962798
[1800]:Problemset/1800-Maximum%20Ascending%20Subarray%20Sum/README.md#1800-maximum-ascending-subarray-sum
27972799
[1801]:Problemset/1801-Number%20of%20Orders%20in%20the%20Backlog/README.md#1801-number-of-orders-in-the-backlog
27982800
[1802]:Problemset/1802-Maximum%20Value%20at%20a%20Given%20Index%20in%20a%20Bounded%20Array/README.md#1802-maximum-value-at-a-given-index-in-a-bounded-array
@@ -4424,6 +4426,7 @@
44244426
[1796l]:https://leetcode.com/problems/second-largest-digit-in-a-string/
44254427
[1797l]:https://leetcode.com/problems/design-authentication-manager/
44264428
[1798l]:https://leetcode.com/problems/maximum-number-of-consecutive-values-you-can-make/
4429+
[1799l]:https://leetcode.com/problems/maximize-score-after-n-operations/
44274430
[1800l]:https://leetcode.com/problems/maximum-ascending-subarray-sum/
44284431
[1801l]:https://leetcode.com/problems/number-of-orders-in-the-backlog/
44294432
[1802l]:https://leetcode.com/problems/maximum-value-at-a-given-index-in-a-bounded-array/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,7 @@
11561156
[1796][1796l]|[字符串中第二大的数字][1796] |![rs]
11571157
[1797][1797l]|[设计一个验证系统][1797] |![py]
11581158
[1798][1798l]|[你能构造出连续值的最大数目][1798] |![rs]
1159+
[1799][1799l]|[N 次操作后的最大分数和][1799] |![py]
11591160
[1800][1800l]|[最大升序子数组和][1800] |![rb]&nbsp;&nbsp;![rs]
11601161
[1801][1801l]|[积压订单中的订单总数][1801] |![rs]
11611162
[1802][1802l]|[有界数组中指定下标处的最大值][1802] |![rs]
@@ -2793,6 +2794,7 @@
27932794
[1796]:Problemset/1796-Second%20Largest%20Digit%20in%20a%20String/README_CN.md#1796-字符串中第二大的数字
27942795
[1797]:Problemset/1797-Design%20Authentication%20Manager/README_CN.md#1797-设计一个验证系统
27952796
[1798]:Problemset/1798-Maximum%20Number%20of%20Consecutive%20Values%20You%20Can%20Make/README_CN.md#1798-你能构造出连续值的最大数目
2797+
[1799]:Problemset/1799-Maximize%20Score%20After%20N%20Operations/README_CN.md#1799-n-次操作后的最大分数和
27962798
[1800]:Problemset/1800-Maximum%20Ascending%20Subarray%20Sum/README_CN.md#1800-最大升序子数组和
27972799
[1801]:Problemset/1801-Number%20of%20Orders%20in%20the%20Backlog/README_CN.md#1801-积压订单中的订单总数
27982800
[1802]:Problemset/1802-Maximum%20Value%20at%20a%20Given%20Index%20in%20a%20Bounded%20Array/README_CN.md#1802-有界数组中指定下标处的最大值
@@ -4424,6 +4426,7 @@
44244426
[1796l]:https://leetcode.cn/problems/second-largest-digit-in-a-string/
44254427
[1797l]:https://leetcode.cn/problems/design-authentication-manager/description/
44264428
[1798l]:https://leetcode.cn/problems/maximum-number-of-consecutive-values-you-can-make/
4429+
[1799l]:https://leetcode.cn/problems/maximize-score-after-n-operations/
44274430
[1800l]:https://leetcode.cn/problems/maximum-ascending-subarray-sum/
44284431
[1801l]:https://leetcode.cn/problems/number-of-orders-in-the-backlog/
44294432
[1802l]:https://leetcode.cn/problems/maximum-value-at-a-given-index-in-a-bounded-array/

0 commit comments

Comments
 (0)