🗓 Daily LeetCode Progress – Day 17

-daily-leetcode-progress-–-day-17

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++.

treshold python

treshold  cpp

  • Demonstrated an in‑place, stable zero‑mover using erase/pop mechanics.

zeroes python

zeroes cpp

📦 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 be O(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

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
why-is-gtm-still-the-most-important-product-marketing-kpi?

Why is GTM still the most important product marketing KPI?

Next Post
java-vs-kotlin-for-android-app-development

Java vs Kotlin for Android App Development

Related Posts
鸿蒙next应用国际化:时间与日期格式化

鸿蒙Next应用国际化:时间与日期格式化

本文旨在深入探讨华为鸿蒙HarmonyOS Next系统(截止目前API12)在应用国际化中时间与日期格式化方面的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。 在全球化的应用场景中,正确处理时间与日期的格式化是提供优质用户体验的关键因素之一。不同地区和语言对于时间与日期的表示方式存在显著差异,鸿蒙Next系统提供了丰富的功能来满足这种多样化的需求。本文将详细介绍时间日期格式化选项、相对时间格式化、时间段格式化,以及常见时间日期格式化问题及解决方案,抛砖引玉。 一、时间日期格式化选项 (一)日期显示格式(dateStyle) 格式取值与示例 full:显示完整的日期信息,包括年、月、日、星期。例如,在中文环境下可能显示为“2023年10月15日 星期日”。 long:显示较为详细的日期,通常包含年、月、日和星期的缩写。如“2023年10月15日 周日”。 medium:显示适中的日期格式,一般有年、月、日。例如“2023-10-15”。 short:显示简洁的日期,可能只包含月、日和年的部分信息。比如“10/15/23”(在某些地区格式)。 根据区域和语言选择格式 开发者可以使用 DateTimeFormat 类,根据用户所在区域的语言和文化习惯选择合适的 dateStyle 进行日期格式化。例如:…
Read More