C++ easy solution, sort


  • 0
    J
    class Solution {
    public:
       
        int findMinDifference(vector<string>& timePoints) {
            vector<HourMin> time;
            for(int i = 0;i<timePoints.size();i++)
            {
                string timePoint = timePoints[i];
                int pos = timePoint.find(':');
                string hour = timePoint.substr(0,pos);
                string min = timePoint.substr(pos+1);
                HourMin  item(stoll(hour),stoll(min));
                time.push_back(item);
            }
            sort(time.begin(),time.end(),[](HourMin a, HourMin b){ return a.hour!=b.hour?a.hour<b.hour:a.min<b.min;});
            HourMin first = time[0];
            first.hour+=24;
            time.push_back(first);
            
            int minMinutes = INT_MAX;
            HourMin pre = time[0];
            for(int i = 1;i<time.size();i++)
            {
                HourMin curr = time[i];
                minMinutes = min(minMinutes,calculate(curr,pre));
                pre = curr;
            }    
            return minMinutes;     
        }
    private:
         struct HourMin
        {
            int hour;
            int min;
            HourMin(int _hour,int _min){hour = _hour;min=_min;}
        };
        int calculate(HourMin curr,HourMin pre)
        {
            return curr.hour*60+curr.min - (pre.hour*60+pre.min);
        };
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.