From f22e99e4dc21ff09bbe2831b0866e5923887c8a9 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 12 Jun 2025 12:46:02 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3574 No.3574.Maximize Subarray GCD Score --- .../README.md | 186 +++++++++++++++++- .../README_EN.md | 186 +++++++++++++++++- .../Solution.cpp | 32 +++ .../Solution.go | 41 ++++ .../Solution.java | 32 +++ .../Solution.py | 22 +++ .../Solution.ts | 42 ++++ 7 files changed, 533 insertions(+), 8 deletions(-) create mode 100644 solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.cpp create mode 100644 solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.go create mode 100644 solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.java create mode 100644 solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.py create mode 100644 solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.ts diff --git a/solution/3500-3599/3574.Maximize Subarray GCD Score/README.md b/solution/3500-3599/3574.Maximize Subarray GCD Score/README.md index a7f098c128e6b..a5bc97772b296 100644 --- a/solution/3500-3599/3574.Maximize Subarray GCD Score/README.md +++ b/solution/3500-3599/3574.Maximize Subarray GCD Score/README.md @@ -100,32 +100,210 @@ tags: -### 方法一 +### 方法一:枚举 + 数学 + +我们注意到,题目中数组的长度 $n \leq 1500$,因此,我们可以枚举所有的子数组。对于每个子数组,计算其 GCD 分数,找出最大值即为答案。 + +由于每个数最多只能翻倍一次,那么子数组的 GCD 最多也只能乘以 $2$,因此,我们需要统计子数组中每个数的因子 $2$ 的个数的最小值,以及这个最小值的出现次数。如果次数大于 $k$,则 GCD 分数为 GCD,否则 GCD 分数为 GCD 乘以 $2$。 + +因此,我们可以预处理每个数的因子 $2$ 的个数,然后在枚举子数组时,维护当前子数组的 GCD、最小因子 $2$ 的个数以及其出现次数即可。 + +时间复杂度 $O(n^2 \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。 #### Python3 ```python - +class Solution: + def maxGCDScore(self, nums: List[int], k: int) -> int: + n = len(nums) + cnt = [0] * n + for i, x in enumerate(nums): + while x % 2 == 0: + cnt[i] += 1 + x //= 2 + ans = 0 + for l in range(n): + g = 0 + mi = inf + t = 0 + for r in range(l, n): + g = gcd(g, nums[r]) + if cnt[r] < mi: + mi = cnt[r] + t = 1 + elif cnt[r] == mi: + t += 1 + ans = max(ans, (g if t > k else g * 2) * (r - l + 1)) + return ans ``` #### Java ```java - +class Solution { + public long maxGCDScore(int[] nums, int k) { + int n = nums.length; + int[] cnt = new int[n]; + for (int i = 0; i < n; ++i) { + for (int x = nums[i]; x % 2 == 0; x /= 2) { + ++cnt[i]; + } + } + long ans = 0; + for (int l = 0; l < n; ++l) { + int g = 0; + int mi = 1 << 30; + int t = 0; + for (int r = l; r < n; ++r) { + g = gcd(g, nums[r]); + if (cnt[r] < mi) { + mi = cnt[r]; + t = 1; + } else if (cnt[r] == mi) { + ++t; + } + ans = Math.max(ans, (r - l + 1L) * (t > k ? g : g * 2)); + } + } + return ans; + } + + private int gcd(int a, int b) { + return b == 0 ? a : gcd(b, a % b); + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long maxGCDScore(vector& nums, int k) { + int n = nums.size(); + vector cnt(n); + for (int i = 0; i < n; ++i) { + for (int x = nums[i]; x % 2 == 0; x /= 2) { + ++cnt[i]; + } + } + + long long ans = 0; + for (int l = 0; l < n; ++l) { + int g = 0; + int mi = INT32_MAX; + int t = 0; + for (int r = l; r < n; ++r) { + g = gcd(g, nums[r]); + if (cnt[r] < mi) { + mi = cnt[r]; + t = 1; + } else if (cnt[r] == mi) { + ++t; + } + long long score = static_cast(r - l + 1) * (t > k ? g : g * 2); + ans = max(ans, score); + } + } + + return ans; + } +}; ``` #### Go ```go +func maxGCDScore(nums []int, k int) int64 { + n := len(nums) + cnt := make([]int, n) + for i, x := range nums { + for x%2 == 0 { + cnt[i]++ + x /= 2 + } + } + + ans := 0 + for l := 0; l < n; l++ { + g := 0 + mi := math.MaxInt32 + t := 0 + for r := l; r < n; r++ { + g = gcd(g, nums[r]) + if cnt[r] < mi { + mi = cnt[r] + t = 1 + } else if cnt[r] == mi { + t++ + } + length := r - l + 1 + score := g * length + if t <= k { + score *= 2 + } + ans = max(ans, score) + } + } + + return int64(ans) +} + +func gcd(a, b int) int { + for b != 0 { + a, b = b, a%b + } + return a +} +``` +#### TypeScript + +```ts +function maxGCDScore(nums: number[], k: number): number { + const n = nums.length; + const cnt: number[] = Array(n).fill(0); + + for (let i = 0; i < n; ++i) { + let x = nums[i]; + while (x % 2 === 0) { + cnt[i]++; + x /= 2; + } + } + + let ans = 0; + for (let l = 0; l < n; ++l) { + let g = 0; + let mi = Number.MAX_SAFE_INTEGER; + let t = 0; + for (let r = l; r < n; ++r) { + g = gcd(g, nums[r]); + if (cnt[r] < mi) { + mi = cnt[r]; + t = 1; + } else if (cnt[r] === mi) { + t++; + } + const len = r - l + 1; + const score = (t > k ? g : g * 2) * len; + ans = Math.max(ans, score); + } + } + + return ans; +} + +function gcd(a: number, b: number): number { + while (b !== 0) { + const temp = b; + b = a % b; + a = temp; + } + return a; +} ``` diff --git a/solution/3500-3599/3574.Maximize Subarray GCD Score/README_EN.md b/solution/3500-3599/3574.Maximize Subarray GCD Score/README_EN.md index ec6a646052a3a..b7b456756b916 100644 --- a/solution/3500-3599/3574.Maximize Subarray GCD Score/README_EN.md +++ b/solution/3500-3599/3574.Maximize Subarray GCD Score/README_EN.md @@ -96,32 +96,210 @@ tags: -### Solution 1 +### Solution 1: Enumeration + Mathematics + +We notice that the length of the array in this problem is $n \leq 1500$, so we can enumerate all subarrays. For each subarray, calculate its GCD score and find the maximum value as the answer. + +Since each number can be doubled at most once, the GCD of a subarray can be multiplied by at most $2$. Therefore, we need to count the minimum number of factors of $2$ among all numbers in the subarray, as well as the number of times this minimum occurs. If the count is greater than $k$, the GCD score is the GCD itself; otherwise, the GCD score is the GCD multiplied by $2$. + +Thus, we can preprocess the number of factors of $2$ for each number, and when enumerating subarrays, maintain the current subarray's GCD, the minimum number of factors of $2$, and the number of times this minimum occurs. + +The time complexity is $O(n^2 \times \log n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. #### Python3 ```python - +class Solution: + def maxGCDScore(self, nums: List[int], k: int) -> int: + n = len(nums) + cnt = [0] * n + for i, x in enumerate(nums): + while x % 2 == 0: + cnt[i] += 1 + x //= 2 + ans = 0 + for l in range(n): + g = 0 + mi = inf + t = 0 + for r in range(l, n): + g = gcd(g, nums[r]) + if cnt[r] < mi: + mi = cnt[r] + t = 1 + elif cnt[r] == mi: + t += 1 + ans = max(ans, (g if t > k else g * 2) * (r - l + 1)) + return ans ``` #### Java ```java - +class Solution { + public long maxGCDScore(int[] nums, int k) { + int n = nums.length; + int[] cnt = new int[n]; + for (int i = 0; i < n; ++i) { + for (int x = nums[i]; x % 2 == 0; x /= 2) { + ++cnt[i]; + } + } + long ans = 0; + for (int l = 0; l < n; ++l) { + int g = 0; + int mi = 1 << 30; + int t = 0; + for (int r = l; r < n; ++r) { + g = gcd(g, nums[r]); + if (cnt[r] < mi) { + mi = cnt[r]; + t = 1; + } else if (cnt[r] == mi) { + ++t; + } + ans = Math.max(ans, (r - l + 1L) * (t > k ? g : g * 2)); + } + } + return ans; + } + + private int gcd(int a, int b) { + return b == 0 ? a : gcd(b, a % b); + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long maxGCDScore(vector& nums, int k) { + int n = nums.size(); + vector cnt(n); + for (int i = 0; i < n; ++i) { + for (int x = nums[i]; x % 2 == 0; x /= 2) { + ++cnt[i]; + } + } + + long long ans = 0; + for (int l = 0; l < n; ++l) { + int g = 0; + int mi = INT32_MAX; + int t = 0; + for (int r = l; r < n; ++r) { + g = gcd(g, nums[r]); + if (cnt[r] < mi) { + mi = cnt[r]; + t = 1; + } else if (cnt[r] == mi) { + ++t; + } + long long score = static_cast(r - l + 1) * (t > k ? g : g * 2); + ans = max(ans, score); + } + } + + return ans; + } +}; ``` #### Go ```go +func maxGCDScore(nums []int, k int) int64 { + n := len(nums) + cnt := make([]int, n) + for i, x := range nums { + for x%2 == 0 { + cnt[i]++ + x /= 2 + } + } + + ans := 0 + for l := 0; l < n; l++ { + g := 0 + mi := math.MaxInt32 + t := 0 + for r := l; r < n; r++ { + g = gcd(g, nums[r]) + if cnt[r] < mi { + mi = cnt[r] + t = 1 + } else if cnt[r] == mi { + t++ + } + length := r - l + 1 + score := g * length + if t <= k { + score *= 2 + } + ans = max(ans, score) + } + } + + return int64(ans) +} + +func gcd(a, b int) int { + for b != 0 { + a, b = b, a%b + } + return a +} +``` +#### TypeScript + +```ts +function maxGCDScore(nums: number[], k: number): number { + const n = nums.length; + const cnt: number[] = Array(n).fill(0); + + for (let i = 0; i < n; ++i) { + let x = nums[i]; + while (x % 2 === 0) { + cnt[i]++; + x /= 2; + } + } + + let ans = 0; + for (let l = 0; l < n; ++l) { + let g = 0; + let mi = Number.MAX_SAFE_INTEGER; + let t = 0; + for (let r = l; r < n; ++r) { + g = gcd(g, nums[r]); + if (cnt[r] < mi) { + mi = cnt[r]; + t = 1; + } else if (cnt[r] === mi) { + t++; + } + const len = r - l + 1; + const score = (t > k ? g : g * 2) * len; + ans = Math.max(ans, score); + } + } + + return ans; +} + +function gcd(a: number, b: number): number { + while (b !== 0) { + const temp = b; + b = a % b; + a = temp; + } + return a; +} ``` diff --git a/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.cpp b/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.cpp new file mode 100644 index 0000000000000..6fd7582b838d4 --- /dev/null +++ b/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + long long maxGCDScore(vector& nums, int k) { + int n = nums.size(); + vector cnt(n); + for (int i = 0; i < n; ++i) { + for (int x = nums[i]; x % 2 == 0; x /= 2) { + ++cnt[i]; + } + } + + long long ans = 0; + for (int l = 0; l < n; ++l) { + int g = 0; + int mi = INT32_MAX; + int t = 0; + for (int r = l; r < n; ++r) { + g = gcd(g, nums[r]); + if (cnt[r] < mi) { + mi = cnt[r]; + t = 1; + } else if (cnt[r] == mi) { + ++t; + } + long long score = static_cast(r - l + 1) * (t > k ? g : g * 2); + ans = max(ans, score); + } + } + + return ans; + } +}; diff --git a/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.go b/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.go new file mode 100644 index 0000000000000..cf3cdd672f14b --- /dev/null +++ b/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.go @@ -0,0 +1,41 @@ +func maxGCDScore(nums []int, k int) int64 { + n := len(nums) + cnt := make([]int, n) + for i, x := range nums { + for x%2 == 0 { + cnt[i]++ + x /= 2 + } + } + + ans := 0 + for l := 0; l < n; l++ { + g := 0 + mi := math.MaxInt32 + t := 0 + for r := l; r < n; r++ { + g = gcd(g, nums[r]) + if cnt[r] < mi { + mi = cnt[r] + t = 1 + } else if cnt[r] == mi { + t++ + } + length := r - l + 1 + score := g * length + if t <= k { + score *= 2 + } + ans = max(ans, score) + } + } + + return int64(ans) +} + +func gcd(a, b int) int { + for b != 0 { + a, b = b, a%b + } + return a +} \ No newline at end of file diff --git a/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.java b/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.java new file mode 100644 index 0000000000000..b08819f08fcc9 --- /dev/null +++ b/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.java @@ -0,0 +1,32 @@ +class Solution { + public long maxGCDScore(int[] nums, int k) { + int n = nums.length; + int[] cnt = new int[n]; + for (int i = 0; i < n; ++i) { + for (int x = nums[i]; x % 2 == 0; x /= 2) { + ++cnt[i]; + } + } + long ans = 0; + for (int l = 0; l < n; ++l) { + int g = 0; + int mi = 1 << 30; + int t = 0; + for (int r = l; r < n; ++r) { + g = gcd(g, nums[r]); + if (cnt[r] < mi) { + mi = cnt[r]; + t = 1; + } else if (cnt[r] == mi) { + ++t; + } + ans = Math.max(ans, (r - l + 1L) * (t > k ? g : g * 2)); + } + } + return ans; + } + + private int gcd(int a, int b) { + return b == 0 ? a : gcd(b, a % b); + } +} \ No newline at end of file diff --git a/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.py b/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.py new file mode 100644 index 0000000000000..30ecc6596dac8 --- /dev/null +++ b/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.py @@ -0,0 +1,22 @@ +class Solution: + def maxGCDScore(self, nums: List[int], k: int) -> int: + n = len(nums) + cnt = [0] * n + for i, x in enumerate(nums): + while x % 2 == 0: + cnt[i] += 1 + x //= 2 + ans = 0 + for l in range(n): + g = 0 + mi = inf + t = 0 + for r in range(l, n): + g = gcd(g, nums[r]) + if cnt[r] < mi: + mi = cnt[r] + t = 1 + elif cnt[r] == mi: + t += 1 + ans = max(ans, (g if t > k else g * 2) * (r - l + 1)) + return ans diff --git a/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.ts b/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.ts new file mode 100644 index 0000000000000..75f999cd231b8 --- /dev/null +++ b/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.ts @@ -0,0 +1,42 @@ +function maxGCDScore(nums: number[], k: number): number { + const n = nums.length; + const cnt: number[] = Array(n).fill(0); + + for (let i = 0; i < n; ++i) { + let x = nums[i]; + while (x % 2 === 0) { + cnt[i]++; + x /= 2; + } + } + + let ans = 0; + for (let l = 0; l < n; ++l) { + let g = 0; + let mi = Number.MAX_SAFE_INTEGER; + let t = 0; + for (let r = l; r < n; ++r) { + g = gcd(g, nums[r]); + if (cnt[r] < mi) { + mi = cnt[r]; + t = 1; + } else if (cnt[r] === mi) { + t++; + } + const len = r - l + 1; + const score = (t > k ? g : g * 2) * len; + ans = Math.max(ans, score); + } + } + + return ans; +} + +function gcd(a: number, b: number): number { + while (b !== 0) { + const temp = b; + b = a % b; + a = temp; + } + return a; +}