Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 817fb52

Browse files
committed
Refactor comments and clean up code in Minimum Operations solution
1 parent e9ed525 commit 817fb52

File tree

1 file changed

+5
-9
lines changed
  • Easy/3375. Minimum Operations to Make Array Values Equal to K

1 file changed

+5
-9
lines changed

‎Easy/3375. Minimum Operations to Make Array Values Equal to K/solution.php‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,28 @@ class Solution
2424
$target = 2 * $k - $num;
2525

2626
if (isset($freq[$target])) {
27+
2728
if ($num == $target) {
28-
// Use pairs within the same value
2929
$pairCount = intdiv($freq[$num], 2);
3030
$pairs += $pairCount;
3131
$freq[$num] -= $pairCount * 2;
3232
} elseif ($num < $target) {
33-
// Pair num with target
3433
$pairCount = min($freq[$num], $freq[$target]);
3534
$pairs += $pairCount;
3635
$freq[$num] -= $pairCount;
3736
$freq[$target] -= $pairCount;
3837
}
3938
}
4039
}
41-
42-
// After using valid pairs, each of the remaining non-k elements needs 1 op
4340
return $nonK - $pairs * 2;
4441
}
4542
}
46-
4743
$solution = new Solution();
4844

49-
$nums = [1, 2, 3, 4, 5];// Example input array
45+
$nums = [1, 2, 3, 4, 5];
5046

51-
$k = 3;// Example target value
47+
$k = 3;
5248

53-
$result = $solution->minOperations($nums, $k);// Call the function with the example input
49+
$result = $solution->minOperations($nums, $k);
5450

55-
echo $result;// Output: 4 (Explanation: The minimum number of operations required to make all elements equal to 3 is 4)
51+
echo $result;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /