Who ever get "beats 100%"? I try so so hard, but failed, end with "beats 97%".


  • 0
    J

    I guess no one can improve the code for singe statement, but still cannot beat 100%. WHYWHY...

    class Solution {
      public:
        int findMinDifference(vector<string> &times) {
            if (times.size() > 60 * 24)
                return 0;
    
            array<int, 60 * 24 + 1> bucket{};
            for (const auto &s : times) {
                if (++bucket[timeToInt(s)] > 1)
                    return 0;
            }
    
            unsigned int min_diff = INT_MAX;
            unsigned int curr_len = INT_MAX;
            for (auto it = bucket.cbegin(); it != bucket.cend(); ++it) {
                if (*it) {
                    min_diff = min(min_diff, curr_len + 1);
                    curr_len = 0;
                } else {
                    ++curr_len;
                }
            }
    
            for (auto it = bucket.cbegin(); !(*it); ++it) {
                ++curr_len;
            }
            min_diff = min(min_diff, curr_len);
    
            return min_diff;
        }
    
        inline int timeToInt(const string &s) {
            int hour = (s[0] - '0') * 10 + (s[1] - '0');
            int minute = (s[3] - '0') * 10 + (s[4] - '0');
            return 60 * hour + minute;
        }
    };
    

Log in to reply
 

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