Dada la string str que almacena la hora en el formato de 24 horas como «HH: MM» . La tarea es encontrar los minutos mínimos que deben agregarse para hacer que el tiempo sea palindrómico.
Ejemplos:
Entrada: str = “05:39”
Salida: 11
Explicación: Se necesitan 11 minutos para que el valor de los minutos se convierta en 50, 05:50 es un tiempo palindrómicoEjemplos:
Entrada: str = “13:31”
Salida: 0
Explicación: Dado que 13:31 ya es palindrómico, por lo tanto, se requieren 0 minutos
Enfoque:
La idea es incrementar con avidez el valor de los minutos hasta que el valor del tiempo se convierta en palíndromo. Ejecute un ciclo while para incrementar el valor de los minutos y verifique simultáneamente si el valor de la hora y el valor de los minutos forman un palíndromo o no.
Al incrementar los valores de minutos y horas, asegúrese de verificar la condición base cuando el valor de los minutos es 60 y el valor de la hora es 24 .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to get the required minutes int get_palindrome_time(string str) { int hh, mm; // Storing hour and minute value // in integral form hh = (str[0] - 48) * 10 + (str[1] - 48); mm = (str[3] - 48) * 10 + (str[4] - 48); int requiredTime = 0; // Keep iterating till first digit // hour becomes equal to second // digit of minute and second digit // of hour becomes equal to first // digit of minute while (hh % 10 != mm / 10 || hh / 10 != mm % 10) { ++mm; // If mins is 60, increase hour, and // reinitilialized to 0 if (mm == 60) { mm = 0; ++hh; } // If hours is 60, reinitialized to 0 if (hh == 24) hh = 0; ++requiredTime; } // Return the required time return requiredTime; } // Driver Code int main() { // Given Time as a string string str = "05:39"; // Function Call cout << get_palindrome_time(str) << endl; }
Java
// Java program for the above approach class GFG{ // Function to get the required minutes public static int get_palindrome_time(String str) { int hh, mm; // Storing hour and minute value // in integral form hh = (str.charAt(0) - 48) * 10 + (str.charAt(1) - 48); mm = (str.charAt(3) - 48) * 10 + (str.charAt(4) - 48); int requiredTime = 0; // Keep iterating till first digit // hour becomes equal to second // digit of minute and second digit // of hour becomes equal to first // digit of minute while (hh % 10 != mm / 10 || hh / 10 != mm % 10) { ++mm; // If mins is 60, increase hour, and // reinitilialized to 0 if (mm == 60) { mm = 0; ++hh; } // If hours is 60, reinitialized to 0 if (hh == 24) hh = 0; ++requiredTime; } // Return the required time return requiredTime; } // Driver code public static void main(String[] args) { // Given Time as a string String str = "05:39"; // Function Call System.out.println(get_palindrome_time(str)); } } // This code is contributed by divyeshrabadiya07
Python3
# Python3 program for the above approach # Function to get the required minutes def get_palindrome_time(str): # Storing hour and minute value # in integral form hh = ((ord(str[0]) - 48) * 10 + (ord(str[1]) - 48)) mm = ((ord(str[3]) - 48) * 10 + (ord(str[4]) - 48)) requiredTime = 0 # Keep iterating till first digit # hour becomes equal to second # digit of minute and second digit # of hour becomes equal to first # digit of minute while (hh % 10 != mm // 10 or hh // 10 != mm % 10): mm += 1 # If mins is 60, increase hour, and # reinitilialized to 0 if (mm == 60): mm = 0 hh += 1 # If hours is 60, reinitialized to 0 if (hh == 24): hh = 0 requiredTime += 1; # Return the required time return requiredTime if __name__=="__main__": # Given Time as a string str = "05:39"; # Function call print(get_palindrome_time(str)); # This code is contributed by rutvik_56
C#
// C# program for the above approach using System; class GFG{ // Function to get the required minutes public static int get_palindrome_time(string str) { int hh, mm; // Storing hour and minute value // in integral form hh = (str[0] - 48) * 10 + (str[1] - 48); mm = (str[3] - 48) * 10 + (str[4] - 48); int requiredTime = 0; // Keep iterating till first digit // hour becomes equal to second // digit of minute and second digit // of hour becomes equal to first // digit of minute while (hh % 10 != mm / 10 || hh / 10 != mm % 10) { ++mm; // If mins is 60, increase hour, // and reinitilialized to 0 if (mm == 60) { mm = 0; ++hh; } // If hours is 60, reinitialized to 0 if (hh == 24) hh = 0; ++requiredTime; } // Return the required time return requiredTime; } // Driver code public static void Main(string[] args) { // Given Time as a string string str = "05:39"; // Function Call Console.Write(get_palindrome_time(str)); } } // This code is contributed by rutvik_56
Javascript
<script> // Javascript program for the above approach // Function to get the required minutes function get_palindrome_time(str) { let hh, mm; // Storing hour and minute value // in letegral form hh = (str[0].charCodeAt() - 48) * 10 + (str[1].charCodeAt() - 48); mm = (str[3].charCodeAt() - 48) * 10 + (str[4].charCodeAt() - 48); let requiredTime = 0; // Keep iterating till first digit // hour becomes equal to second // digit of minute and second digit // of hour becomes equal to first // digit of minute while (hh % 10 != Math.floor(mm / 10) || Math.floor(hh / 10) != mm % 10) { ++mm; // If mins is 60, increase hour, and // reinitilialized to 0 if (mm == 60) { mm = 0; ++hh; } // If hours is 60, reinitialized to 0 if (hh == 24) hh = 0; ++requiredTime; } // Return the required time return requiredTime; } // Driver Code // Given Time as a string let str = "05:39"; // Function Call document.write(get_palindrome_time(str.split(''))); </script>
11
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)