Skip to content

leetcode/3867: brute force#384

Merged
tony84727 merged 1 commit into
mainfrom
leetcode/3867
Jul 16, 2026
Merged

leetcode/3867: brute force#384
tony84727 merged 1 commit into
mainfrom
leetcode/3867

Conversation

@tony84727

Copy link
Copy Markdown
Owner

No description provided.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +5 to +18
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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since n is already defined as prefix_gcd.len(), you can use n / 2 directly in the loop range instead of re-evaluating prefix_gcd.len() / 2.

Suggested change
for i in 0..prefix_gcd.len() / 2 {
for i in 0..n / 2 {

@tony84727
tony84727 enabled auto-merge July 16, 2026 14:02
@tony84727
tony84727 merged commit 515717d into main Jul 16, 2026
4 checks passed
@tony84727
tony84727 deleted the leetcode/3867 branch July 16, 2026 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant