The Number of Full Rounds You Have Played

Problem Description

Ah, the age-old question: how many rounds have you played? No, not the rounds of golf you keep pretending to play on weekends, but the rounds of your favorite game that you actually enjoy! The problem at hand is to determine the number of full 15-minute rounds you’ve played between your login and logout times.

Imagine you log in to your favorite game at 2:05 PM and log out at 2:50 PM. You might think, “Hey, I played for 45 minutes!” But wait! How many full 15-minute rounds did you actually play? Spoiler alert: it’s not as straightforward as it seems.

Code Solution


class Solution {
 public:
  int numberOfRounds(string loginTime, string logoutTime) {
    const int start = getMinutes(loginTime);
    int finish = getMinutes(logoutTime);
    if (start > finish)
      finish += 60 * 24;
    return max(0, finish / 15 - (start + 14) / 15);
  }

 private:
  int getMinutes(const string& time) {
    const int h = stoi(time.substr(0, 2));
    const int m = stoi(time.substr(3));
    return 60 * h + m;
  }
};

Approach

The approach here is as simple as pie (or should I say, as simple as counting your rounds). The code first converts the login and logout times into minutes. If your login time is later than your logout time (because you decided to play until the wee hours), it adds a full day’s worth of minutes to the logout time. Then, it calculates the number of full 15-minute intervals between the two times. Voilà! You have your answer.

Time and Space Complexity

Complexity Type Complexity
Time Complexity O(1)
Space Complexity O(1)

Real-World Example

Let’s say you logged in at 1:50 PM and logged out at 2:30 PM. You might think you played for 40 minutes, but in reality, you played for 2 full rounds (15 minutes each) and a little extra time. So, the code will help you realize that you’re not as good at counting as you thought!

Similar Problems

If you enjoyed this problem, you might also want to check out these related challenges: