3328. Apply Operations to Make Sum of Array Greater Than or Equal to k
My accepted Python solution to LeetCode problem 3328, Apply Operations to Make Sum of Array Greater Than or Equal to k, running in 3ms.
- Difficulty: Medium
- Python
- Runtime 3ms
- Memory 17.2MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 3ms, memory 17.2MB, accepted 2026-01-02.
class Solution:
def minOperations(self, k: int) -> int:
if k <= 1:
return 0
# We start with [1], can increment or duplicate
# If we increment to v, then duplicate d times: sum = (d+1)*v
# Operations = (v-1) + d
# Want min (v-1) + d such that (d+1)*v >= k
result = float('inf')
# Try each possible final value v
for v in range(1, k + 1):
# Need (d+1) * v >= k, so d >= ceil(k/v) - 1
d = (k + v - 1) // v - 1 # This is ceil(k/v) - 1
d = max(0, d)
ops = (v - 1) + d
result = min(result, ops)
# Optimization: once ops starts increasing, we can stop
if v - 1 > result:
break
return result