Posted on

Description

Submission

class Solution {
public:
    int numberOfRounds(string startTime, string finishTime) {
        int h1 = stoi(startTime.substr(0, 2));
        int m1 = stoi(startTime.substr(3, 2));
        int h2 = stoi(finishTime.substr(0, 2));
        int m2 = stoi(finishTime.substr(3, 2));
        
        if(h1 > h2 || (h1 == h2 && m1 > m2)) {
            h2 += 24; 
        }
        int ret = 0;
        if(h1 == h2) {
            if(m1 >= m2) return 0;
            for(int i = 0; i < 60; i += 15) {
                if(m1 <= i && m2 >= i + 15) ++ret;
            } 
        } else if(h1 < h2) {
            // h1 < h2
            if(m1 == 0) ret += 4;
            else if(m1 <= 15) ret += 3;
            else if(m1 <= 30) ret += 2;
            else if(m1 <= 45) ret += 1;
                   
            if(m2 >= 45) ret += 3;
            else if(m2 >= 30) ret += 2;
            else if(m2 >= 15) ret += 1;
            ret += 4 * (h2 - h1 - 1);
        }
        
        return ret;
    }
};
    

Leave a Reply

Your email address will not be published. Required fields are marked *