K (ngn/k), 37 bytes
{+/x{{(a~&\a)|a~|\a:10\x}(1+)/x+1}\0}
{
}
is a function with argument x
x{
}\0
applies the {}
on 0
x
times, preserving the intermediate results
(1+)
is the successor function
{
}(1+)/x+1
applies the successor function starting from x+1
until the {}
returns true
10\x
are the decimal digits of x
a:
assign to a
|\
is the max-scan (partial maxima) of a
&\
analogously, is the min-scan
a~|\a
does a
match its max-scan?
|
or
a~&\a
its min-scan?
+/
sum