Skip to content

Commit 9ab43c5

Browse files
committed
Time: 15 ms (5%), Space: 18 MB (50.5%) - LeetHub
1 parent c15c2de commit 9ab43c5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# time complexity: O(n)
2+
# space complexity: O(n)
3+
from typing import List
4+
5+
6+
class Solution:
7+
def minOperations(self, nums: List[int], k: int) -> int:
8+
total = sum(nums)
9+
remainder = total % k
10+
if remainder == 0:
11+
return 0
12+
13+
mods = []
14+
for num in nums:
15+
mods.append(num % k)
16+
mods.sort(reverse=True)
17+
18+
result = 0
19+
for m in mods:
20+
if remainder == 0:
21+
break
22+
take = min(remainder, m)
23+
result += take
24+
remainder -= take
25+
return result
26+
27+
28+
nums = [3, 9, 7]
29+
k = 5
30+
print(Solution().minOperations(nums, k))
31+
nums = [4, 1, 3]
32+
k = 4
33+
print(Solution().minOperations(nums, k))
34+
nums = [3, 2]
35+
k = 6
36+
print(Solution().minOperations(nums, k))

0 commit comments

Comments
 (0)