leetcode/3867: brute force#384
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds a brute-force Rust solution for LeetCode problem 3867, "Sum of GCD of Formed Pairs". The review feedback suggests optimizing the implementation by avoiding an unnecessary vector clone and simplifying the prefix maximum and GCD calculations into a single pass. Additionally, it recommends reusing the pre-calculated length variable in the loop range to improve readability.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| let mut prefix_max = nums.clone(); | ||
| let mut current_max = 0; | ||
| for n in prefix_max.iter_mut() { | ||
| if *n <= current_max { | ||
| *n = current_max; | ||
| continue; | ||
| } | ||
| current_max = *n; | ||
| } | ||
| let mut prefix_gcd: Vec<i32> = prefix_max | ||
| .into_iter() | ||
| .enumerate() | ||
| .map(|(i, x)| Self::gcd(nums[i], x)) | ||
| .collect(); |
There was a problem hiding this comment.
The current implementation clones the nums vector, performs an in-place modification to find prefix maximums, and then maps over it to compute the GCDs. This can be simplified into a single pass with Vec::with_capacity, avoiding the clone and reducing allocation overhead.
let mut current_max = 0;
let mut prefix_gcd = Vec::with_capacity(nums.len());
for &n in &nums {
current_max = current_max.max(n);
prefix_gcd.push(Self::gcd(n, current_max));
}| prefix_gcd.sort_unstable(); | ||
| let mut sum = 0_i64; | ||
| let n = prefix_gcd.len(); | ||
| for i in 0..prefix_gcd.len() / 2 { |
No description provided.