मध्यम 0 हल किए गए 0% स्वीकृति

Subtract Digits to Reach Zero

Use as few digit subtractions as possible to reach zero.

आप क्या सीखेंगे

  • Model a process as states and moves
  • Compute the minimum steps to zero
  • Compare DP with a greedy shortcut

समस्या

अनुवाद दिखाएँ

At each step, choose a nonzero decimal digit currently present in the number and subtract that digit. Digits are taken from the new value after every step. Find the minimum number of steps needed to reach zero.

Implement removing_digits(n).

Input

n: The positive starting number.

Output

Return the fewest subtraction steps.

सीमाएँ

1 ≤ n ≤ 1,000,000. Subtracting zero is not a useful move and is excluded.

उदाहरण

उदाहरण 1
इनपुट:
n = 14

अपेक्षित आउटपुट:
expected = 3
इनपुट:
long long n = 14;

अपेक्षित आउटपुट:
long long expected = 3;
इनपुट:
long n = 14;

अपेक्षित आउटपुट:
long expected = 3;

व्याख्या

14 → 10 → 9 → 0 uses three steps. Neither possible first subtraction can reach zero in only two steps.

उदाहरण 2
इनपुट:
n = 7

अपेक्षित आउटपुट:
expected = 1
इनपुट:
long long n = 7;

अपेक्षित आउटपुट:
long long expected = 1;
इनपुट:
long n = 7;

अपेक्षित आउटपुट:
long expected = 1;

व्याख्या

Subtract the only digit, 7, to reach 0 immediately.

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Each number depends on the best answer for smaller numbers reachable by subtracting one digit.

संकेत 2

Dynamic programming over all values up to n avoids repeated work.

संकेत 3

Remember that the available digits change after every subtraction, and zero is never a valid choice.

अपेक्षित जटिलता

O(n * d) time, O(n) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।