Problema
Split a duration into hours, minutes, and seconds. Write units as h, m, and s, in that order, with one space between units. Omit any zero-valued unit, except that zero seconds is written as 0s.
Implement format_duration(seconds).
Input
seconds: The total duration in seconds.
Output
Return the formatted duration, for example 2h 2m 5s.
Restrições
0 ≤ seconds ≤ 1,000,000,000. Hours may exceed 23; this is a duration, not a time of day.
Exemplos
Entrada:
seconds = 7325
Saída esperada:
expected = "2h 2m 5s"
Entrada:
long long seconds = 7325;
Saída esperada:
std::string expected = "2h 2m 5s";
Entrada:
long seconds = 7325;
Saída esperada:
string expected = "2h 2m 5s";
Explicação
7325 = 2 × 3600 + 2 × 60 + 5.
Entrada:
seconds = 120
Saída esperada:
expected = "2m"
Entrada:
long long seconds = 120;
Saída esperada:
std::string expected = "2m";
Entrada:
long seconds = 120;
Saída esperada:
string expected = "2m";
Explicação
120 seconds is exactly 2 minutes. Omit the zero hours and seconds.
Como abordar
Tente sozinho primeiro e depois abra uma dica de cada vez.
Dica 1
Break the total seconds into larger units first, then format only the nonzero parts.
Dica 2
Keep hours, minutes, and seconds in that order, with spaces between included units.
Dica 3
If every larger unit is zero, still make sure the output is exactly 0s when the duration is zero.
Complexidade esperada
O(1) time, O(1) extra space
Referências
Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.