C++ O(n) with hash-table


  • 0
    D
    class Solution {
    public:
        int findMinDifference(vector<string>& timePoints) {
            int hashtable[1440]={0};
            for(size_t i=0;i<timePoints.size();i++){
                int a=(timePoints[i][0]-'0')*10+timePoints[i][1]-'0';
                int b=(timePoints[i][3]-'0')*10+timePoints[i][4]-'0';
                int x=a*60+b;
                hashtable[x]++;
            }
            int ret=0x7fff;
            int start=-1,end=-10000;
            int diff=0x7fff;
            for(size_t i=0;i<1440;i++){
                if(hashtable[i]>=2)return 0;
                if(hashtable[i]==1&&start!=-1){
                    diff=i-end;
                    end=i;
                    if(diff<ret)ret=diff;
                }
                if(hashtable[i]==1&&start==-1){start=i;end=i;}//first num
            }
            if((start-end)+1440<ret)ret=(start-end)+1440;
            return ret;
        }
    };
    

Log in to reply
 

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