Minimize a Number After Removing Digits

Quick Overview

Practice minimizing a decimal number by deleting exactly a specified number of digits without rearranging the rest. Handle repeated digits, leading zeros, and very long inputs.

Minimize a Number After Removing Digits

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Minimize a Number After Removing Digits Given a decimal string `num` representing a nonnegative integer and an integer `k`, delete exactly `k` digits. Keep the relative order of the digits that remain, and return the smallest possible resulting number as a decimal string. The returned string must have no leading zeros. Return `"0"` when the remaining digits are all zeros or when all digits were deleted. ### Function `string removeKdigits(string num, int k)` ### Example 1 ```text Input: num = "540321", k = 2 Output: "321" ``` Deleting `5` and `4` leaves `"0321"`, which is written as `"321"`. ### Example 2 ```text Input: num = "1111", k = 3 Output: "1" ``` ### Constraints - `1 <= k <= num.length <= 100000`. - `num` contains only decimal digits. - `num` has no leading zeros unless it is exactly `"0"`. ### Clarifications Delete occurrences, not all copies of a chosen digit. Formatting away leading zeros does not change the requirement to delete exactly `k` original digits. Do not rearrange the retained digits. The number can be much larger than a machine integer. ### What a Strong Answer Covers - Exactly the required number of deletions, including repeated digits. - Leading zeros and deleting the entire input. - Why no other retained sequence produces a smaller value. - Time and storage bounds for a hundred-thousand-digit input.

Overview: Practice minimizing a decimal number by deleting exactly a specified number of digits without rearranging the rest. Handle repeated digits, leading zeros, and very long inputs.

Amazon logo
Amazon
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Minimize a Number After Removing Digits

Given a decimal string num representing a nonnegative integer and an integer k, delete exactly k digits. Keep the relative order of the digits that remain, and return the smallest possible resulting number as a decimal string.

The returned string must have no leading zeros. Return "0" when the remaining digits are all zeros or when all digits were deleted.

Function

string removeKdigits(string num, int k)

Example 1

Input: num = "540321", k = 2
Output: "321"

Deleting 5 and 4 leaves "0321", which is written as "321".

Example 2

Input: num = "1111", k = 3
Output: "1"

Constraints

  • 1 <= k <= num.length <= 100000 .
  • num contains only decimal digits.
  • num has no leading zeros unless it is exactly "0" .

Clarifications

Delete occurrences, not all copies of a chosen digit. Formatting away leading zeros does not change the requirement to delete exactly k original digits. Do not rearrange the retained digits. The number can be much larger than a machine integer.

What a Strong Answer Covers Guidance

  • Exactly the required number of deletions, including repeated digits.
  • Leading zeros and deleting the entire input.
  • Why no other retained sequence produces a smaller value.
  • Time and storage bounds for a hundred-thousand-digit input.