Remove K Digits

To solve this coding challenge, we need to identify how to strategically remove
k
digits from the string
num
, representing a non-negative integer, to produce the smallest possible integer. The solution leverages a greedy algorithm to remove digits in a way that minimizes the final number. Let's break down the steps for this solution in detail and provide a suitable pseudocode.

# Explanation

Clarifying the Problem Statement
  1. We start with a string
    num
    representing a non-negative integer.
  2. We also have an integer
    k
    which denotes the number of digits we need to remove from
    num
    .
  3. Our goal is to form the smallest possible integer after removing these
    k
    digits.
  4. We need to ensure that there are no leading zeros in our final result unless the entire result is just "0".
  5. Approach to Solve the Problem
  6. We will use a stack to implement the greedy strategy for solving this problem. The stack helps in maintaining the digits of the resulting smallest number efficiently.
  7. Traverse each digit of the given string
    num
    . For each digit, we compare it with the top of the stack (the most recently added digit). If the current digit is smaller than the top of the stack and we still have some digits to remove (
    k > 0
    ), we pop the stack to remove the top digit. This ensures that smaller digits are prioritized and larger digits are removed if they are not the most optimal choice.
  8. Once every digit has been processed, there could still be remaining digits to remove (
    k > 0
    ). In this case, we trim the digits from the end of the stack.
  9. After trimming the necessary digits, we concatenate the stack to form our final result.
  10. Ensure to strip any leading zeros from the result. If the final result is empty, return "0".
  11. # Pseudocode

                                                
    # Main function to remove k digits to form the smallest number
    function removeKdigits(num: string, k: int) -> string:
    # Initialize an empty stack to store digits of the resulting number
    stack = []
    
    # Iterate over each digit in the given number string `num`
    for digit in num:
    # While there are still digits to remove (`k > 0`) and the stack is not empty
    # and the last digit in the stack is greater than the current digit
    while k > 0 and stack is not empty and stack[-1] > digit:
    # Pop the last digit from the stack (remove it) and decrement k
    stack.pop()
    k -= 1
    # Push the current digit onto the stack
    stack.append(digit)
    
    # After processing all digits, if k is still greater than 0, remove the last k digits
    while k > 0:
    stack.pop()
    k -= 1
    
    # Join the stack into a string and remove any leading zeros
    result = "".join(stack).lstrip("0")
    
    # If the result is empty, return "0"
    if result == "":
    return "0"
    
    # Return the resulting smallest number
    return result
    
                                            

    # Step-by-Step Explanation/Detailed Steps in Pseudocode

  12. Create an empty list
    stack
    to hold the digits of the resulting number.
  13. Traverse each character
    digit
    in the input string
    num
    .
    • Inside the loop, while
      k
      is greater than 0,
      stack
      is not empty, and the last element in
      stack
      is greater than
      digit
      :
      • Remove the last element from
        stack
        (this ensures a smaller number will form).
      • Decrease
        k
        by 1 to account for the removed digit.
    • Append the current
      digit
      to the
      stack
      .
  14. Once the entire string
    num
    has been processed,
    k
    might still be greater than 0. If so, remove the last
    k
    digits from the stack by popping
    k
    times.
  15. Join all elements of
    stack
    to form a string and remove leading zeroes using
    lstrip("0")
    .
  16. If the resulting string is empty, return "0".
  17. Otherwise, return the processed string that represents the smallest number possible.