Problems Solved:
- #3065 Minimum Operations to Exceed Threshold Value I
- #283 Move Zeroes
This continues my daily series (Day 17: Count-Below-Threshold + Inโplace Zero Push). Today I focused on two array problems: counting how many elements are below a threshold when we repeatedly remove the global minimum, and moving all zeros to the end while preserving order.
๐ก What I Learned
- For Minimum Operations to Exceed Threshold Value I, the operation โremove one occurrence of the smallest elementโ implies we must eliminate every value
< k
exactly once. Thus, after sorting, the answer is simply the count of values< k
. (Sorting is optional; we can also count directly.) - For Move Zeroes, implementing it by erasing zeros in place and pushing them to the back preserves order but causes O(nยฒ) behavior due to repeated middle erases on a vector/array. It still solves the problem correctly and demonstrates inโplace stability, but is not optimal versus the twoโpointer linear approach.
- Both tasks highlight how problem constraints map to simple invariants: removeโmin step โ all
< k
must go; stable inโplace reordering โ zeros migrate without disturbing nonโzero relative order.
๐งฉ #3065 โ Minimum Operations to Exceed Threshold Value I
C++ (Sort + Count Below k
)
class Solution {
public:
int minOperations(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
int i = 0;
while (i < nums.size() && nums[i] < k){
i += 1;
}
return i;
}
};
Python (Sort + Count Below k
)
class Solution:
def minOperations(self, nums: List[int], k: int) -> int:
nums.sort()
i = 0
while i < len(nums) and nums[i] < k:
i += 1
return i
Why it works: each operation always removes the current global minimum. As long as any element < k
remains, that minimum is < k
and must be removed once. Therefore, the minimal number of operations equals the number of elements < k
.
Time: O(n log n)
due to sort (a countingโonly variant can be O(n)
).
Space: O(1)
extra (inโplace sort).
๐งฉ #283 โ Move Zeroes
C++ (Inโplace erase + push_back)
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int start= 0, end = nums.size()-1;
while (start < end){
if (nums[start] == 0){
nums.push_back(nums[start]);
nums.erase(nums.begin() + start);
end--;
}else{
start++;
}
}
}
};
Python (Inโplace pop/append)
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
start = 0
end = len(nums)-1
while start < end:
if nums[start] == 0:
nums.append(nums[start])
nums.pop(start)
end -= 1
else:
start += 1
Why it works: whenever a zero is found, it is relocated to the end. Elements already processed keep their relative order, so stability is preserved.
Time: worstโcase O(nยฒ)
(due to middle erases/pops).
Space: O(1)
extra.
Note: A twoโpointer O(n) alternative writes nonโzeros forward then fills trailing zeros. I kept the implementations above consistent with todayโs code to reflect the approach I used.
๐ธ Achievements
- Implemented thresholdโremoval counting cleanly in both Python and C++.
- Demonstrated an inโplace, stable zeroโmover using erase/pop mechanics.
๐ฆ Complexity Recap
-
Minimum Operations to Exceed Threshold Value I:
O(n log n)
time (with sort),O(1)
extra space. -
Move Zeroes:
O(nยฒ)
time with erase/pop,O(1)
extra space. (Twoโpointer variant would beO(n)
.)
๐ฃ Join the Journey
Iโm solving and documenting problems daily in both Python and C++, covering arrays, linked lists, dynamic programming, and more. Follow along if youโre interested in systematic problem solving.
Links
- LinkedIn: https://www.linkedin.com/in/ertugrul-mutlu
- GitHub Series: https://github.com/Ertugrulmutlu/leetcode-series
Top comments (0)