Skip to content

1904. The Number of Full Rounds You Have Played 👎

  • Time: $O(1)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
  public int numberOfRounds(String loginTime, String logoutTime) {
    final int start = getMinutes(loginTime);
    int finish = getMinutes(logoutTime);
    if (start > finish)
      finish += 60 * 24;
    return Math.max(0, finish / 15 - (start + 14) / 15);
  }

  private int getMinutes(final String s) {
    return 60 * Integer.valueOf(s.substring(0, 2)) + Integer.valueOf(s.substring(3));
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
  def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
    start = self._getMinutes(loginTime)
    finish = self._getMinutes(logoutTime)
    if start > finish:
      finish += 60 * 24

    return max(0, finish // 15 - (start + 14) // 15)

  def _getMinutes(self, time: str) -> int:
    h, m = map(int, time.split(':'))
    return 60 * h + m