We have to find the minimum number of digits required to make a given number, for example: 14 => 95 (9 + 5 = 14) is two digits which is the minimum to form 14.
int moves(int n) {
int m = 0; // Minimum count
while (n-9 >= 0) { // To place maximum number of 9's
n -= 9;
m++;
}
if (n == 0) { // If only nines made up the number
return m;
}
else {
m++;
return m;
}
}
I am getting a TLE (runtime time limit exceeded) by an online judge. How can I improve it or is there a better approach?
Take a number n as the input. An integer function smallest_digit(int n) takes 'n' as the input and returns the smallest digit in the given number. Now initialize min as the last digit of the given number. Iterate through the number and check if the number extracted is less than the minimum number.
The minimum is the first number listed as it is the lowest, and the maximum is the last number listed because it is the highest.
At first count number of different digits from 0-9. If different digits are not more than 4 then count the instance of each each digit. If each digits are even number then find the the 4 largest digit(all digits can equal). sort them decreasing order first and increasing order second.
If the count of 1 s is greater than or equal to 1, then the minimum number of digits required to be removed is 1. If the count of 2 s is greater than or equal to 2, then the minimum number of digits required to be removed will be 2 [Since (2 + 2) % 3 = 1]. If sum = 2: Those digits should be removed whose sum gives a remainder 2 on dividing by 3.
We have to find the minimum number of digits required to make a given number, for example: 14 => 95 (9 + 5 = 14) is two digits which is the minimum to form 14.
min_coins = [0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 2, 3, 3, 4, 4, 3, 4, 4, 5] min_coins [0] => 0 min_coins [1] => 1 min_coins [2] => 1 min_coins [3] => 2 . . . min_coins [18] => 5 If you understand the logic to calculate the minimum number of coins required, you can implement it in any programming language of your choice like C/C++, Java, etc.
Given an integer N, the task is to find the minimum number of digits required to generate a number having the sum of digits equal to N. The number with smallest number of digits having sum of digits equal to 18 is 99. 4-digit numbers like 8884, 6877, etc are the smallest in length having sum of digits equal to 28.
Your code starts by looking at how many times 9 fits into that number. This can be done way more easily:
int m = n/9;
This suffices since we do an integer division, in which the remainder is thrown away. Note that if n
would be float
or another floating type, this would not work.
The question left is if it is divisible by 9 or not. If not, we have one additional digit. This can be done by the modulo operator (made it verbose for ease of understanding):
bool divisible_by_nine = (n % 9 == 0);
Assuming that you might not know the modulo operator, it returns the remainder of an integer division, 47 % 9 = 2 since 47 / 9 = 5 remainder 2.
Without it, you would go with
int remainder = n - 9*m;
bool divisible = (remainder == 0);
Combined:
int required_digits(int number)
{
bool divisible = (number % 9 == 0);
return number/9 + (divisible ? 0 : 1);
}
Or in a single line, depending on how verbose you want it to be:
int required_digits(int number)
{
return number/9 + (number % 9 == 0 ? 0 : 1);
}
Since there isn't any loop, this is in Θ(1) and thus should work in your required time limit.
(Technically, the processor might as well handle the division somewhat like you did internally, but it is very efficient at that. To be absolutely correct, I'd have to add "assuming that division is a constant time operation".)
Your solution works fine. You can try the shorter:
return (n%9==0)? n/9 : n/9 +1 ;
Shorter, but less easy to read...
Or a compromise:
if (n%9==0) // n can be divided by 9
return n/9;
else
return n/9+1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With