Skip to content

Commit df2e5fb

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 3190
1 parent 4ebc80e commit df2e5fb

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<p>
44
<img src="docs/images/logo_dark.png", alt="Logo", width="300" height="300" />
55
</p>
6-
💡 Awesome Python Leetcode
6+
💡 Awesome Python Leetcode
77
<br>
88
<span style="font-size: large">
99
Awesome Leetcode problems and solutions written in Python!
@@ -307,6 +307,7 @@
307307
- [2799 Count Complete Subarrays in an Array](https://leetcode.com/problems/count-complete-subarrays-in-an-array/description/)
308308
- [3005 Count Elements With Maximum Frequency](https://leetcode.com/problems/count-elements-with-maximum-frequency/description/)
309309
- [3136 Valid Word](https://leetcode.com/problems/valid-word/description/)
310+
- [3190 Find Minimum Operations to Make All Elements Divisible by Three](https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three/description/)
310311
- [3202 Find the Maximum Length of Valid Subsequence II](https://leetcode.com/problems/find-the-maximum-length-of-valid-subsequence-ii/description/)
311312
- [3318 Find X-Sum of All K-Long Subarrays I](https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-i/description/)
312313
- [3392 Count Subarrays of Length Three With a Condition](https://leetcode.com/problems/count-subarrays-of-length-three-with-a-condition/description/)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def minimumOperations(self, nums: List[int]) -> int:
8+
"""
9+
You are given an integer array nums. In one operation, you can add or subtract
10+
1 from any element of nums.
11+
12+
Return the minimum number of operations to make all elements of nums divisible
13+
by 3.
14+
"""
15+
# Time Complexity: O(n)
16+
# Space Complexity: O(1)
17+
num_ops = 0
18+
k = 3
19+
for num in nums:
20+
if num % k:
21+
remain = num % k
22+
num_ops += min(remain, k - remain)
23+
return num_ops
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._3190_find_minimum_operations_to_make_all_elements_divisible_by_three import ( # noqa: E501
6+
Solution,
7+
)
8+
9+
10+
@pytest.mark.parametrize(
11+
argnames=["nums", "expected"],
12+
argvalues=[
13+
([1, 2, 3, 4], 3),
14+
([3, 6, 9], 0),
15+
],
16+
)
17+
def test_func(nums: List[int], expected: int):
18+
"""Tests the solution of a LeetCode problem."""
19+
min_ops = Solution().minimumOperations(nums)
20+
assert min_ops == expected

0 commit comments

Comments
 (0)