Skip to content

Commit 015269d

Browse files
Add files via upload
1 parent 3f73fd8 commit 015269d

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 3190. Find Minimum Operations to Make All Elements Divisible by Three
2+
# Solved
3+
# Easy
4+
# Topics
5+
# premium lock icon
6+
# Companies
7+
# Hint
8+
# You are given an integer array nums. In one operation, you can add or subtract 1 from any element of nums.
9+
10+
# Return the minimum number of operations to make all elements of nums divisible by 3.
11+
12+
13+
14+
# Example 1:
15+
16+
# Input: nums = [1,2,3,4]
17+
18+
# Output: 3
19+
20+
# Explanation:
21+
22+
# All array elements can be made divisible by 3 using 3 operations:
23+
24+
# Subtract 1 from 1.
25+
# Add 1 to 2.
26+
# Subtract 1 from 4.
27+
# Example 2:
28+
29+
# Input: nums = [3,6,9]
30+
31+
# Output: 0
32+
33+
34+
35+
# Constraints:
36+
37+
# 1 <= nums.length <= 50
38+
# 1 <= nums[i] <= 50
39+
40+
class Solution:
41+
def minimumOperations(self, nums: List[int]) -> int:
42+
x = 0
43+
for i in range(len(nums)):
44+
x += min(nums[i] % 3,3 - (nums[i] % 3))
45+
return x

0 commit comments

Comments
 (0)