Minimum Number of Operations to Convert Time

🏠 ⬅️ ➑️

You are given two strings current and correct representing two 24-hour times.

24-hour times are formatted as "HH:MM ", where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59.

In one operation you can increase the time current by 1, 5, 15, or 60 minutes. You can perform this operation any number of times.

Return the minimum number of operations needed to convert current to correct.

Example 1:

Input: current = "02:30 ", correct = "04:35 " Output: 3 Explanation: We can convert current to correct in 3 operations as follows: - Add 60 minutes to current. current becomes "03:30 ". - Add 60 minutes to current. current becomes "04:30 ". - Add 5 minutes to current. current becomes "04:35 ". It can be proven that it is not possible to convert current to correct in fewer than 3 operations.

Example 2:

Input: current = "11:00 ", correct = "11:01 " Output: 1 Explanation: We only have to add one minute to current, so the minimum number of operations needed is 1.

Constraints:

  • current and correct are in the format "HH:MM "
  • current <= correct

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    character(len=8) :: current, correct
    integer :: n_ops

    ! Example 1
    current = '02:30 '
    correct = '04:35 '
    write (*,*) 'Example 1:'
    call solve(current, correct, n_ops)
    write (*,'(A,I0)') 'Number of operations: ', n_ops

    ! Example 2
    current = '11:00 '
    correct = '11:01 '
    write (*,*) 'Example 2:'
    call solve(current, correct, n_ops)
    write (*,'(A,I0)') 'Number of operations: ', n_ops

contains

    subroutine solve(current, correct, n_ops)
        implicit none
        character(len=*), intent(in) :: current, correct
        integer, intent(out) :: n_ops
        integer :: hh_curr, mm_curr, hh_corr, mm_corr

        read (current, fmt='(I2, A, I2)') hh_curr, ':', mm_curr
        read (correct, fmt='(I2, A, I2)') hh_corr, ':', mm_corr

        if (hh_curr == hh_corr .and. mm_curr == mm_corr) then
            n_ops = 0
            return
        end if

        if (hh_curr < hh_corr .or. (hh_curr == hh_corr .and. mm_curr < mm_corr)) then
            n_ops = 1
            return
        end if

        if (mm_curr + 1 == mm_corr) then
            n_ops = 2
            return
        end if

        if (mm_curr + 5 == mm_corr) then
            n_ops = 3
            return
        end if

        if (mm_curr + 15 == mm_corr) then
            n_ops = 4
            return
        end if

        if (mm_curr + 60 == mm_corr) then
            n_ops = 5
            return
        end if

        stop 'Invalid input'
    end subroutine solve
end program main
❌ Compiled
❌ Executed
❌ Correct
!--------------------------------------------------------------------------------
! This is a Fortran implementation of the problem.
! The program takes two command line arguments: current and correct.
! The program outputs the minimum number of operations needed to convert current
! to correct.

program main
    implicit none

    character(len=5) :: current, correct
    integer :: n_operations

    ! Read the input from the command line
    read(*,*) current, correct

    ! Convert the strings to integers
    read(current,*) n_operations

    ! Calculate the number of operations needed to convert current to correct
    n_operations = calculate_n_operations(n_operations, correct)

    ! Print the result
    write(*,*) n_operations

contains

    !----------------------------------------------------------------------------
    ! This function calculates the minimum number of operations needed to convert
    ! current to correct.
    !----------------------------------------------------------------------------
    function calculate_n_operations(current, correct) result(n_operations)
        implicit none

        character(len=5), intent(in) :: current, correct
        integer :: n_operations

        ! Initialize the number of operations to 0
        n_operations = 0

        ! Loop until current is equal to correct
        do while (.not. equal_times(current, correct))
            ! Increment the number of operations
            n_operations = n_operations + 1

            ! Increment current by 1 minute
            current = increment_time(current)
        end do

    end function calculate_n_operations

    !----------------------------------------------------------------------------
    ! This function checks if two times are equal.
    !----------------------------------------------------------------------------
    function equal_times(time1, time2) result(equal)
        implicit none

        character(len=5), intent(in) :: time1, time2
        logical :: equal

        ! Check if the hours are equal
        if (time1(1:2) /= time2(1:2)) then
            equal = .false.
            return
        end if

        ! Check if the minutes are equal
        if (time1(4:5) /= time2(4:5)) then
            equal = .false.
            return
        end if

        ! If we reach this point, the times are equal
        equal = .true.

    end function equal_times

    !----------------------------------------------------------------------------
    ! This function increments a time by 1 minute.
    !----------------------------------------------------------------------------
    function increment_time(time) result(new_time)
        implicit none

        character(len=5), intent(in) :: time
        character(len=5) :: new_time

        ! Increment the minutes
        if (time(4:5) == "59") then
            new_time = "00:00"
        else
            new_time = time(1:4) // int_to_str(int(time(4:5)) + 1)
        end if

    end function increment_time

    !----------------------------------------------------------------------------
    ! This function converts an integer to a string.
    !----------------------------------------------------------------------------
    function int_to_str(n) result(str)
        implicit none

        integer, intent(in) :: n
        character(len=2) :: str

        write(str,*) n

    end function int_to_str

end program main
!--------------------------------------------------------------------------------
🌐 Data from online sources
def minimum_operations(current: str, correct: str) -> int:
    ch, cm = map(int, current.split(':'))
    th, tm = map(int, correct.split(':'))
    ct = ch * 60 + cm
    tt = th * 60 + tm

    operations = 0
    while ct < tt:
        if tt - ct >= 60:
            ct += 60
        elif tt - ct >= 15:
            ct += 15
        elif tt - ct >= 5:
            ct += 5
        else:
            ct += 1

        operations += 1

    return operations
The approach to solving this problem is fairly straightforward. First, we parse the input strings 'current' and 'correct' into hours and minutes. We then calculate the total minutes for both 'current' (ct) and 'correct' (tt).

We initialize a variable operations tokeep track of the minimum number of operations needed.

Using a while loop, we check if ct is smaller than tt indicating that we still need to perform operations. In each iteration, we choose the largest operation possible (60, 15, 5, or 1 minute(s)) based on the difference between tt and ct. After performing the operation, we increment the operations counter.

The final result is the value of the operations counter at the end of the loop.

🌐 Data from online sources
int minimumOperations(string current, string correct) {
    int ch = stoi(current.substr(0, 2)), cm = stoi(current.substr(3));
    int th = stoi(correct.substr(0, 2)), tm = stoi(correct.substr(3));
    int ct = ch * 60 + cm, tt = th * 60 + tm;

    int operations = 0;
    while (ct < tt) {
        if (tt - ct >= 60)
            ct += 60;
        else if (tt - ct >= 15)
            ct += 15;
        else if (tt - ct >= 5)
            ct += 5;
        else
            ct += 1;

        operations++;
    }
    return operations;
}
The approach to solving this problem is fairly straightforward. First, we parse the input strings 'current' and 'correct' into hours and minutes. We then calculate the total minutes for both 'current' (ct) and 'correct' (tt).

We initialize a variable operations tokeep track of the minimum number of operations needed.

Using a while loop, we check if ct is smaller than tt indicating that we still need to perform operations. In each iteration, we choose the largest operation possible (60, 15, 5, or 1 minute(s)) based on the difference between tt and ct. After performing the operation, we increment the operations counter.

The final result is the value of the operations counter at the end of the loop.