Skip to content

Commit 4dda814

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 17.7 MB (90.21%)
1 parent 02d047b commit 4dda814

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<p>You are given an integer array <code>nums</code>. In one operation, you can add or subtract 1 from <strong>any</strong> element of <code>nums</code>.</p>
2+
3+
<p>Return the <strong>minimum</strong> number of operations to make all elements of <code>nums</code> divisible by 3.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<div class="example-block">
9+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4]</span></p>
10+
11+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
12+
13+
<p><strong>Explanation:</strong></p>
14+
15+
<p>All array elements can be made divisible by 3 using 3 operations:</p>
16+
17+
<ul>
18+
<li>Subtract 1 from 1.</li>
19+
<li>Add 1 to 2.</li>
20+
<li>Subtract 1 from 4.</li>
21+
</ul>
22+
</div>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<div class="example-block">
27+
<p><strong>Input:</strong> <span class="example-io">nums = [3,6,9]</span></p>
28+
29+
<p><strong>Output:</strong> <span class="example-io">0</span></p>
30+
</div>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Constraints:</strong></p>
34+
35+
<ul>
36+
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
37+
<li><code>1 &lt;= nums[i] &lt;= 50</code></li>
38+
</ul>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def minimumOperations(self, nums: List[int]) -> int:
3+
return sum(min(x % 3, 3 - x % 3) for x in nums)

0 commit comments

Comments
 (0)