Initialize a counter to keep track of 'good' numbers.
Iterate through all numbers from 1 to n.
For each number, check each digit:
a. If the digit is 0, 1, or 8, continue to the next digit.
b. If the digit is 2, 5, 6, or 9, mark the number as potentially 'good' and continue.
c. If the digit is 3, 4, or 7, mark the number as invalid and break the loop.
If the number is marked as potentially 'good' and no invalid digits were found, increment the counter.
Return the counter value, which represents the total number of 'good' numbers.
Dynamic Programming Approach to Count Good Numbers