Leetcode Problem

539. Minimum Time Difference

Given a list of 24-hour clock time points in "HH:MM" format, return the minimum minutes difference between any two time-points in the list. timePoints = ["23:59","00:00"]

approach to this question is that we will first convert all the hour time to minutes by multiplying hour with 60 and then adding minutes to it. we will put these values into the vector and sort that vector, and then easily find the minimum.

 int findMinDifference(vector<string>& timePoints) 
    {
        int n=timePoints.size();

        vector<int>v;

        for(int i=0;i<n;i++)
        {
            string s = timePoints[i];
            int time_H = stoi(s.substr(0,2))*60;
            int time_M = stoi(s.substr(3,2));
            if(time_H==0)
            {
                v.push_back(1440+time_M);
            }
            else
                v.push_back(time_H+time_M);
        }

        sort(v.begin(),v.end());

        int ans=INT_MAX;

        for(int i=1;i<v.size();i++)
        {
            ans=min(v[i]-v[i-1],ans);
        }


        ans=min(ans,v[0]-v[v.size()-1]+1440);

        return ans;
    }