|
| 1 | +# [2941. Maximum GCD-Sum of a Subarray](https://leetcode.cn/problems/maximum-gcd-sum-of-a-subarray) |
| 2 | + |
| 3 | +[English Version](/solution/2900-2999/2941.Maximum%20GCD-Sum%20of%20a%20Subarray/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>You are given an array of integers <code>nums</code> and an integer <code>k</code>.</p> |
| 10 | + |
| 11 | +<p>The <strong>gcd-sum</strong> of an array <code>a</code> is calculated as follows:</p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li>Let <code>s</code> be the sum of all the elements of <code>a</code>.</li> |
| 15 | + <li>Let <code>g</code> be the <strong>greatest common divisor</strong> of all the elements of <code>a</code>.</li> |
| 16 | + <li>The gcd-sum of <code>a</code> is equal to <code>s * g</code>.</li> |
| 17 | +</ul> |
| 18 | + |
| 19 | +<p>Return <em>the <strong>maximum gcd-sum</strong> of a subarray of</em> <code>nums</code> <em>with at least</em> <code>k</code> <em>elements.</em></p> |
| 20 | + |
| 21 | +<p> </p> |
| 22 | +<p><strong class="example">Example 1:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +<strong>Input:</strong> nums = [2,1,4,4,4,2], k = 2 |
| 26 | +<strong>Output:</strong> 48 |
| 27 | +<strong>Explanation:</strong> We take the subarray [4,4,4], the gcd-sum of this array is 4 * (4 + 4 + 4) = 48. |
| 28 | +It can be shown that we can not select any other subarray with a gcd-sum greater than 48.</pre> |
| 29 | + |
| 30 | +<p><strong class="example">Example 2:</strong></p> |
| 31 | + |
| 32 | +<pre> |
| 33 | +<strong>Input:</strong> nums = [7,3,9,4], k = 1 |
| 34 | +<strong>Output:</strong> 81 |
| 35 | +<strong>Explanation:</strong> We take the subarray [9], the gcd-sum of this array is 9 * 9 = 81. |
| 36 | +It can be shown that we can not select any other subarray with a gcd-sum greater than 81.</pre> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong>Constraints:</strong></p> |
| 40 | + |
| 41 | +<ul> |
| 42 | + <li><code>n == nums.length</code></li> |
| 43 | + <li><code>1 <= n <= 10<sup>5</sup></code></li> |
| 44 | + <li><code>1 <= nums[i] <= 10<sup>6</sup></code></li> |
| 45 | + <li><code>1 <= k <= n</code></li> |
| 46 | +</ul> |
| 47 | + |
| 48 | +## 解法 |
| 49 | + |
| 50 | +<!-- 这里可写通用的实现逻辑 --> |
| 51 | + |
| 52 | +<!-- tabs:start --> |
| 53 | + |
| 54 | +### **Python3** |
| 55 | + |
| 56 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 57 | + |
| 58 | +```python |
| 59 | +class Solution: |
| 60 | + def maxGcdSum(self, nums: List[int], k: int) -> int: |
| 61 | + s = list(accumulate(nums, initial=0)) |
| 62 | + f = [] |
| 63 | + ans = 0 |
| 64 | + for i, v in enumerate(nums): |
| 65 | + g = [] |
| 66 | + for j, x in f: |
| 67 | + y = gcd(x, v) |
| 68 | + if not g or g[-1][1] != y: |
| 69 | + g.append((j, y)) |
| 70 | + f = g |
| 71 | + f.append((i, v)) |
| 72 | + for j, x in f: |
| 73 | + if i - j + 1 >= k: |
| 74 | + ans = max(ans, (s[i + 1] - s[j]) * x) |
| 75 | + return ans |
| 76 | +``` |
| 77 | + |
| 78 | +### **Java** |
| 79 | + |
| 80 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 81 | + |
| 82 | +```java |
| 83 | +class Solution { |
| 84 | + public long maxGcdSum(int[] nums, int k) { |
| 85 | + int n = nums.length; |
| 86 | + long[] s = new long[n + 1]; |
| 87 | + for (int i = 1; i <= n; ++i) { |
| 88 | + s[i] = s[i - 1] + nums[i - 1]; |
| 89 | + } |
| 90 | + List<int[]> f = new ArrayList<>(); |
| 91 | + long ans = 0; |
| 92 | + for (int i = 0; i < n; ++i) { |
| 93 | + List<int[]> g = new ArrayList<>(); |
| 94 | + for (var e : f) { |
| 95 | + int j = e[0], x = e[1]; |
| 96 | + int y = gcd(x, nums[i]); |
| 97 | + if (g.isEmpty() || g.get(g.size() - 1)[1] != y) { |
| 98 | + g.add(new int[] {j, y}); |
| 99 | + } |
| 100 | + } |
| 101 | + f = g; |
| 102 | + f.add(new int[] {i, nums[i]}); |
| 103 | + for (var e : f) { |
| 104 | + int j = e[0], x = e[1]; |
| 105 | + if (i - j + 1 >= k) { |
| 106 | + ans = Math.max(ans, (s[i + 1] - s[j]) * x); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return ans; |
| 111 | + } |
| 112 | + |
| 113 | + private int gcd(int a, int b) { |
| 114 | + return b == 0 ? a : gcd(b, a % b); |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### **C++** |
| 120 | + |
| 121 | +```cpp |
| 122 | +class Solution { |
| 123 | +public: |
| 124 | + long long maxGcdSum(vector<int>& nums, int k) { |
| 125 | + int n = nums.size(); |
| 126 | + long long s[n + 1]; |
| 127 | + s[0] = 0; |
| 128 | + for (int i = 1; i <= n; ++i) { |
| 129 | + s[i] = s[i - 1] + nums[i - 1]; |
| 130 | + } |
| 131 | + vector<pair<int, int>> f; |
| 132 | + long long ans = 0; |
| 133 | + for (int i = 0; i < n; ++i) { |
| 134 | + vector<pair<int, int>> g; |
| 135 | + for (auto [j, x] : f) { |
| 136 | + int y = gcd(x, nums[i]); |
| 137 | + if (g.empt() || g.back().second != y) { |
| 138 | + g.emplace_back(j, y); |
| 139 | + } |
| 140 | + } |
| 141 | + f = move(g); |
| 142 | + f.emplace_back(i, nums[i]); |
| 143 | + for (auto [j, x] : f) { |
| 144 | + if (i - j + 1 >= k) { |
| 145 | + ans = max(ans, (s[i + 1] - s[j]) * x); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + return ans; |
| 150 | + } |
| 151 | +}; |
| 152 | +``` |
| 153 | +
|
| 154 | +### **Go** |
| 155 | +
|
| 156 | +```go |
| 157 | +func maxGcdSum(nums []int, k int) int64 { |
| 158 | + n := len(nums) |
| 159 | + s := make([]int64, n+1) |
| 160 | + s[0] = 0 |
| 161 | + for i, x := range nums { |
| 162 | + s[i+1] = s[i] + int64(x) |
| 163 | + } |
| 164 | + type pair [2]int |
| 165 | + var f []pair |
| 166 | + var ans int64 |
| 167 | + for i := 0; i < n; i++ { |
| 168 | + var g []pair |
| 169 | + for _, p := range f { |
| 170 | + j, x := p[0], p[1] |
| 171 | + y := int(gcd(int(x), nums[i])) |
| 172 | + if len(g) == 0 || g[len(g)-1][1] != y { |
| 173 | + g = append(g, pair{j, y}) |
| 174 | + } |
| 175 | + } |
| 176 | + f = g |
| 177 | + f = append(f, pair{i, nums[i]}) |
| 178 | + for _, p := range f { |
| 179 | + j, x := p[0], p[1] |
| 180 | + if i-j+1 >= k { |
| 181 | + ans = max(ans, (s[i+1]-s[j])*int64(x)) |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + return ans |
| 186 | +} |
| 187 | +
|
| 188 | +func gcd(a, b int) int { |
| 189 | + if b == 0 { |
| 190 | + return a |
| 191 | + } |
| 192 | + return gcd(b, a%b) |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +### **TypeScript** |
| 197 | + |
| 198 | +```ts |
| 199 | +function maxGcdSum(nums: number[], k: number): number { |
| 200 | + const n: number = nums.length; |
| 201 | + const s: number[] = Array(n + 1).fill(0); |
| 202 | + for (let i = 1; i <= n; i++) { |
| 203 | + s[i] = s[i - 1] + nums[i - 1]; |
| 204 | + } |
| 205 | + |
| 206 | + let f: [number, number][] = []; |
| 207 | + let ans: number = 0; |
| 208 | + |
| 209 | + for (let i = 0; i < n; ++i) { |
| 210 | + const g: [number, number][] = []; |
| 211 | + for (const [j, x] of f) { |
| 212 | + const y: number = gcd(x, nums[i]); |
| 213 | + if (g.length === 0 || g.at(-1)[1] !== y) { |
| 214 | + g.push([j, y]); |
| 215 | + } |
| 216 | + } |
| 217 | + f = g; |
| 218 | + f.push([i, nums[i]]); |
| 219 | + for (const [j, x] of f) { |
| 220 | + if (i - j + 1 >= k) { |
| 221 | + ans = Math.max(ans, (s[i + 1] - s[j]) * x); |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + return ans; |
| 227 | +} |
| 228 | + |
| 229 | +function gcd(a: number, b: number): number { |
| 230 | + return b === 0 ? a : gcd(b, a % b); |
| 231 | +} |
| 232 | +``` |
| 233 | + |
| 234 | +### **TypeScript** |
| 235 | + |
| 236 | +```ts |
| 237 | +function maxGcdSum(nums: number[], k: number): number { |
| 238 | + const n: number = nums.length; |
| 239 | + const s: number[] = Array(n + 1).fill(0); |
| 240 | + for (let i = 1; i <= n; i++) { |
| 241 | + s[i] = s[i - 1] + nums[i - 1]; |
| 242 | + } |
| 243 | + |
| 244 | + let f: [number, number][] = []; |
| 245 | + let ans: number = 0; |
| 246 | + |
| 247 | + for (let i = 0; i < n; ++i) { |
| 248 | + const g: [number, number][] = []; |
| 249 | + for (const [j, x] of f) { |
| 250 | + const y: number = gcd(x, nums[i]); |
| 251 | + if (g.length === 0 || g.at(-1)[1] !== y) { |
| 252 | + g.push([j, y]); |
| 253 | + } |
| 254 | + } |
| 255 | + f = g; |
| 256 | + f.push([i, nums[i]]); |
| 257 | + for (const [j, x] of f) { |
| 258 | + if (i - j + 1 >= k) { |
| 259 | + ans = Math.max(ans, (s[i + 1] - s[j]) * x); |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + return ans; |
| 265 | +} |
| 266 | + |
| 267 | +function gcd(a: number, b: number): number { |
| 268 | + return b === 0 ? a : gcd(b, a % b); |
| 269 | +} |
| 270 | +``` |
| 271 | + |
| 272 | +### **...** |
| 273 | + |
| 274 | +``` |
| 275 | +
|
| 276 | +``` |
| 277 | + |
| 278 | +<!-- tabs:end --> |
0 commit comments