समस्या
Count integers from left through right, including both endpoints, that have no equal neighboring digits. Use their usual decimal writing with no leading zeroes. Zero and all other single-digit numbers qualify.
Implement counting_numbers(left, right).
Input
left: The first allowed value.
right: The last allowed value.
Output
Return how many values satisfy the rule.
सीमाएँ
0 ≤ left ≤ right ≤ 10¹⁸. The range may be too large to inspect each integer individually.
उदाहरण
इनपुट:
left = 10
right = 15
अपेक्षित आउटपुट:
expected = 5
इनपुट:
long long left = 10;
long long right = 15;
अपेक्षित आउटपुट:
long long expected = 5;
इनपुट:
long left = 10;
long right = 15;
अपेक्षित आउटपुट:
long expected = 5;
व्याख्या
Only 11 has equal neighboring digits. The other five numbers qualify.
इनपुट:
left = 0
right = 9
अपेक्षित आउटपुट:
expected = 10
इनपुट:
long long left = 0;
long long right = 9;
अपेक्षित आउटपुट:
long long expected = 10;
इनपुट:
long left = 0;
long right = 9;
अपेक्षित आउटपुट:
long expected = 10;
व्याख्या
All ten values have just one digit and therefore no equal neighbors.
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
Count valid numbers up to each endpoint, then subtract to get the range answer.
संकेत 2
Digit DP fits because validity depends on adjacent digits and whether you have started the number.
संकेत 3
Leading zeros need special handling so that shorter numbers are counted correctly.
अपेक्षित जटिलता
O(k * 10 * states) time, O(k * states) extra space
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।