leetcode-pp / 91alg-6-daily-check

91 算法第六期打卡仓库
28 stars 0 forks source link

【Day 34 】2022-01-14 - 1904. 你完成的完整对局数 #44

Open azl397985856 opened 2 years ago

azl397985856 commented 2 years ago

1904. 你完成的完整对局数

入选理由

暂无

题目地址

https://leetcode-cn.com/problems/the-number-of-full-rounds-you-have-played/

前置知识

给你两个字符串 startTime 和 finishTime ,均符合 "HH:MM" 格式,分别表示你 进入 和 退出 游戏的确切时间,请计算在整个游戏会话期间,你完成的 完整对局的对局数 。

例如,如果 startTime = "05:20" 且 finishTime = "05:59" ,这意味着你仅仅完成从 05:30 到 05:45 这一个完整对局。而你没有完成从 05:15 到 05:30 的完整对局,因为你是在对局开始后进入的游戏;同时,你也没有完成从 05:45 到 06:00 的完整对局,因为你是在对局结束前退出的游戏。

如果 finishTime 早于 startTime ,这表示你玩了个通宵(也就是从 startTime 到午夜,再从午夜到 finishTime)。

假设你是从 startTime 进入游戏,并在 finishTime 退出游戏,请计算并返回你完成的 完整对局的对局数 。

 

示例 1:

输入:startTime = "12:01", finishTime = "12:44" 输出:1 解释:你完成了从 12:15 到 12:30 的一个完整对局。 你没有完成从 12:00 到 12:15 的完整对局,因为你是在对局开始后的 12:01 进入的游戏。 你没有完成从 12:30 到 12:45 的完整对局,因为你是在对局结束前的 12:44 退出的游戏。

示例 2:

输入:startTime = "20:00", finishTime = "06:00" 输出:40 解释:你完成了从 20:00 到 00:00 的 16 个完整的对局,以及从 00:00 到 06:00 的 24 个完整的对局。 16 + 24 = 40

示例 3:

输入:startTime = "00:00", finishTime = "23:59" 输出:95 解释:除最后一个小时你只完成了 3 个完整对局外,其余每个小时均完成了 4 场完整对局。

 

提示:

startTime 和 finishTime 的格式为 HH:MM 00 <= HH <= 23 00 <= MM <= 59 startTime 和 finishTime 不相等

yan0327 commented 2 years ago

思路: 用一个分离,将时间换算成分钟,比较t1,t2。若t1打,则t2 +1440 关键是对t2取整, /15 再 *15 可得到答案

func numberOfRounds(loginTime string, logoutTime string) int {
    login := strings.Split(loginTime,":")
    logout := strings.Split(logoutTime,":")
    h1,_ := strconv.Atoi(login[0])
    m1,_ := strconv.Atoi(login[1])
    h2,_ := strconv.Atoi(logout[0])
    m2,_ := strconv.Atoi(logout[1])
    time1 := h1*60+m1
    time2 := h2*60+m2
    if time1 > time2{
        time2 += 1440
    }
    time2 = time2/15*15
    return (time2-time1)/15
}
func max(a,b int) int{
    if a > b{
        return a
    }
        return b
}

时间复杂度O(1) 空间复杂度:O(1)

ninghuang456 commented 2 years ago

 public int numberOfRounds(String loginTime, String logoutTime) {
        int loginH = Integer.valueOf(loginTime.substring(0,2));
        int loginM = Integer.valueOf(loginTime.substring(3));
        int logoutH = Integer.valueOf(logoutTime.substring(0,2));
        int logoutM = Integer.valueOf(logoutTime.substring(3));
        int loginInMinutes = 60*loginH+loginM;
        int logoutInMinutes = 60*logoutH+logoutM;

        if(logoutInMinutes < loginInMinutes) 
            logoutInMinutes += 24*60;

        loginInMinutes += 14;
        loginInMinutes -= loginInMinutes%15;    
        logoutInMinutes -= logoutInMinutes%15; 

        int res = (logoutInMinutes-loginInMinutes)/15;
        return res ==-1 ? 0 : res;  
    }
Frederickfan commented 2 years ago
class Solution:
    def numberOfRounds(self, startTime: str, finishTime: str) -> int:
        from datetime import datetime
        if 0 < int(startTime[3:]) < 15:
            startTime = startTime[:3] + "15"
        elif 15 < int(startTime[3:]) < 30:
            startTime = startTime[:3] + "30"
        elif 30 < int(startTime[3:]) < 45:
            startTime = startTime[:3] + "45"
        elif 45 < int(startTime[3:]) < 60:
            if startTime[:2] == "23":
                startTime = "00:00"
            else:
                startTime = str(int(startTime[:2]) + 1) + ":" + "00"
        if len(startTime) != 5:
            startTime = "0" + startTime
        if 0 < int(finishTime[3:]) < 15:
            finishTime = finishTime[:3] + "00"
        elif 15 < int(finishTime[3:]) < 30:
            finishTime = finishTime[:3] + "15"
        elif 30 < int(finishTime[3:]) < 45:
            finishTime = finishTime[:3] + "30"
        elif 45 < int(finishTime[3:]) < 60:
            finishTime = finishTime[:3] + "45"
        start = datetime(2021, 6, 19, int(startTime[:2]), int(startTime[3:]))
        finish = datetime(2021, 6, 19, int(finishTime[:2]), int(finishTime[3:]))        
        return (finish - start).seconds // 60 // 15
feifan-bai commented 2 years ago

思路

  1. 将时间转化为分钟数
  2. 计算开始和结束时间距离,按15分钟的Range分区
  3. 考虑开始大于结束时间的情况,即通宵 代码
    class Solution:
    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
        t0 = 60 * int(loginTime[:2]) + int(loginTime[3:])
        t1 = 60 * int(logoutTime[:2]) + int(logoutTime[3:])
        if t1 < t0:
            t1 += 1440
        t1 = t1 // 15 * 15
        return max(0, (t1 - t0)) // 15

复杂度分析

zwmanman commented 2 years ago

class Solution(object): def numberOfRounds(self, loginTime, logoutTime): """ :type loginTime: str :type logoutTime: str :rtype: int """ flag = 0 loginArr = map(int, loginTime.split(':')) logoutArr = map(int, logoutTime.split(':'))

    loginInt = loginArr[0] * 60 + loginArr[1]
    logoutInt = logoutArr[0] * 60 + logoutArr[1]

    if logoutInt < loginInt:
        flag = 1

    loginArr[1], loginArr[0] = self.updateinmin(loginArr[1], loginArr[0] )
    logoutArr[1] = self.updateoutmin(logoutArr[1])

    loginInt = loginArr[0] * 60 + loginArr[1]
    if flag:
        logoutInt += 24 * 60

    print(loginInt)
    print(logoutInt)
    return max(0,(logoutInt - loginInt) // 15)

def updateinmin(self, minute, hour):

    if minute > 0 and minute <= 15:
        minute = 15
    elif minute > 15 and minute <= 30:
        minute = 30
    elif minute > 30 and minute <= 45:
        minute = 45
    elif minute > 45 and minute <=60:
        minute = 0
        hour += 1

    return minute, hour

def updateoutmin(self, minute):

    if minute >= 0 and minute < 15:
        minute = 0
    elif minute >= 15 and minute < 30:
        minute = 15
    elif minute >= 30 and minute < 45:
        minute = 30
    elif minute >= 45:
        minute = 45

    return minute
laofuWF commented 2 years ago
# cal start/end time in minutes
# round start to next start time
# round end time to previous end time
# if start time < end time: (end - start) / 15
# return (24 * 60 - (start - end)) / 15

# note: edge cases where start time < end time and rounded start time >= rounded end time 
# (22:46, 23:01), (22:50, 22:56), this should return 0

class Solution:
    def numberOfRounds(self, startTime: str, finishTime: str) -> int:
        start_minutes = self.to_minutes(startTime)
        end_minutes = self.to_minutes(finishTime)

        rounded_start = self.to_next_quarter(start_minutes)
        rounded_end = self.to_prev_quarter(end_minutes)

        if start_minutes < end_minutes and rounded_start >= rounded_end:
            return 0

        if rounded_start < rounded_end:
            return (rounded_end - rounded_start) // 15

        return (24 * 60 - rounded_start + rounded_end) // 15

    def to_minutes(self, time):
        hours = int(time[0:2]) * 60
        minutes = int(time[3:])

        return hours + minutes

    def to_next_quarter(self, minutes):
        return ((minutes + 14) // 15) * 15

    def to_prev_quarter(self, minutes):
        return (minutes // 15) * 15
CoreJa commented 2 years ago

思路

这个题轻微超时了(3分钟),有点菜,一开始把开始时间和结束时间都提前取整了,导致整个代码又臭又长,条件判断还错中复杂。说起来现在越来越怠惰了,昨天上一天课啥都不想干

代码

class Solution:
    # 朴素思路:把开始时间和结束时间分别提取出来,对于分钟进行取整处理(开始时间向后取整,结束时间向前取整),
    # 然后根据小时的不同情况去判断,算总分钟数(可以看作时间戳),最后返回结果。
    def numberOfRounds1(self, loginTime: str, logoutTime: str) -> int:
        login_h, login_m = loginTime.split(":")
        logout_h, logout_m = logoutTime.split(":")
        login_h = int(login_h)
        logout_h = int(logout_h)
        login_m_origin = int(login_m)
        logout_m_origin = int(logout_m)
        login_m = ((int(login_m) - 1) // 15 + 1) * 15
        logout_m = int(logout_m) // 15 * 15

        timestamp_login = login_h * 60 + login_m
        if logout_h > login_h:
            timestamp_logout = logout_h * 60 + logout_m
        elif logout_h < login_h:
            timestamp_logout = (24 + logout_h) * 60 + logout_m
        else:
            if logout_m_origin >= login_m_origin:
                timestamp_logout = logout_h * 60 + logout_m
            else:
                timestamp_logout = (24 + logout_h) * 60 + logout_m
        ans = (timestamp_logout - timestamp_login) // 15
        return ans if ans > 0 else 0

    # 朴素思路优化:既然本来都要算总分钟数,那就不要提前取整,直接获得总分钟数,然后调整开始时间(向后取整),
    # 最后除15即可
    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
        start = int(loginTime[:2]) * 60 + int(loginTime[3:])
        end = int(logoutTime[:2]) * 60 + int(logoutTime[3:])
        if end < start:
            end += 24 * 60
        if start % 15:
            start = (start // 15 + 1) * 15
        return (end - start) // 15 if end > start else 0
AlduinLeung commented 2 years ago

其实这个题只需要注意向前取整和向后取整的问题即可

/**
 * @param {string} loginTime
 * @param {string} logoutTime
 * @return {number}
 */
var numberOfRounds = function(loginTime, logoutTime) {
    const toMinutes =(mins)=>{
        let [h,m] = mins.split(":");
        return Number(h)*60+Number(m);
    }

    let startTime = toMinutes(loginTime);
    let endTime = toMinutes(logoutTime);

    if(startTime > endTime){endTime = endTime +1440;}
    // floor 向后取整,取/15的前一个整数,作为完成的时间
    // ceil 向前取整, 取/15以后的后一个整数,作为开始的时间
    return  Math.max(0, Math.floor(endTime/15) - Math.ceil(startTime/15))
};
JudyZhou95 commented 2 years ago

思路

换算成分钟数。注意隔天的情况和区间取整。

代码

class Solution:
    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:

        login_h, login_m = int(loginTime[:2]), int(loginTime[3:])
        logout_h, logout_m = int(logoutTime[:2]), int(logoutTime[3:])

        login = login_h*60 + login_m
        logout = logout_h*60 + logout_m 

        logout = logout+24*60 if logout < login else logout

        login += (15-login%15) if login%15 != 0 else 0
        logout-=logout%15

        time_diff = logout-login

        return max(time_diff//15, 0)

复杂度

TC: O(1) SC: O(1)

moirobinzhang commented 2 years ago

Code:

public class Solution { public int NumberOfRounds(string loginTime, string logoutTime) { string[] loginTimes = loginTime.Split(":"); string[] logoutTimes = logoutTime.Split(":");

    int startHour = Convert.ToInt32(loginTimes[0]);
    int startMinute = Convert.ToInt32(loginTimes[1]);
    int logoutHour = Convert.ToInt32(logoutTimes[0]);
    int logoutMinute = Convert.ToInt32(logoutTimes[1]);

    int startTime = startHour * 60 + startMinute;
    int endTime = logoutHour * 60 + logoutMinute;
    if (startTime > endTime)
        endTime += 24 * 60;        
    endTime = (endTime /15) * 15;

    return (endTime - startTime) / 15; 
}

}

cszys888 commented 2 years ago
class Solution:
    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
        loginTime_int = int(loginTime[0:2])*100 + int(loginTime[3:5])
        logoutTime_int = int(logoutTime[0:2])*100 + int(logoutTime[3:5])

        if loginTime_int > logoutTime_int:
            logoutTime_int += 2400

        loginhr = loginTime_int // 100
        loginmin = loginTime_int % 100
        logouthr = logoutTime_int // 100 - 1
        logoutmin = logoutTime_int % 100 + 60

        loginmin_count = loginmin // 15
        if loginmin % 15 != 0:
            loginmin_count += 1
        logoutmin_count = logoutmin // 15
        return max(logoutmin_count - loginmin_count + (logouthr - loginhr)*4, 0)

time complexity: O(1) space complexity: O(1)

falconruo commented 2 years ago

思路:

模拟法

复杂度分析:

  1. 时间复杂度: O(1)
  2. 空间复杂度: O(1)

代码(C++):

class Solution {
public:
    int numberOfRounds(string startTime, string finishTime) {
        int start = stoi(startTime.substr(0, 2)) * 60 + stoi(startTime.substr(3, 5));
        int finish = stoi(finishTime.substr(0, 2)) * 60 + stoi(finishTime.substr(3, 5));
        if (start > finish) finish += 1440;
        finish = finish / 15 * 15;
        return max(0, (finish - start) / 15);
    }
};
zhangzz2015 commented 2 years ago

思路

关键点

代码

C++ Code:


class Solution {
public:
    int numberOfRounds(string loginTime, string logoutTime) {

        ///  start time    [1 15] --> index 1   [16 30]  index 2    
        ///  end time      [15 29]--> index 0   [30 44]  index 1; 
        //            [30  ]             
        int start = stoTime(loginTime);  
        int end = stoTime(logoutTime); 
        if(start>end)
            end += 60*24; 

        int startIndex = ceil((start-1e-9)/15.); 
        int endIndex = floor((end-15+1e-9)/15.); 

        return endIndex >= startIndex? endIndex - startIndex+1: 0; 

    }

    int stoTime(string s)
    {
        string tmp = s.substr(0, 2); 
        int hour = stoi(tmp); 
        tmp = s.substr(3, 2); 
        int min = stoi(tmp);
        return 60*hour + min ; 
    }
};
Tao-Mao commented 2 years ago

Code

class Solution {
    public int numberOfRounds(String loginTime, String logoutTime) {
        String[] time1 = loginTime.split(":");
        String[] time2 = logoutTime.split(":");
        int h1 = Integer.parseInt(time1[0]);
        int h2 = Integer.parseInt(time2[0]);
        int m1 = Integer.parseInt(time1[1]);
        int m2 = Integer.parseInt(time2[1]);
        int res = 0;
        if (h1 < h2) {
            res += (h2 - h1 -1) * 4;
            res += (60 - m1)/15;
            res += (m2)/15;
        }
        else {
            if (h1 == h2 && m1 < m2) {
                if (m1 == 0) m1 = 0;
                else if(m1 <= 15) m1 = 15;
                else if (m1 <= 30) m1 = 30;
                else if (m1 <= 45) m1 = 45;
                else {return 0;}
                if(m2 < 15) m2= 0;
                else if (m2 < 30) m2 = 15;
                else if (m2 < 45) m2 = 30;
                else {m2 = 45;}
                if (m1 > m2) return 0;
                return (m2-m1)/15;
            }
            else {
                res += (24 - h1 -1) * 4;
                res += (60 - m1)/15;
                res += h2 * 4 + m2/15;    
            }

        }
        return res;
     }
}
ZacheryCao commented 2 years ago

Idea

Simulation

Code

class Solution:
    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
        login = loginTime.split(":")
        logout=logoutTime.split(":")
        login = [int(login[0]), int(login[1])]
        logout = [int(logout[0]), int(logout[1])]
        ans = 0
        if login[0] > logout[0] or (login[0] == logout[0] and login[1]>logout[1]):
            logout[0] += 24
        ans += logout[0] * 4 - login[0] * 4
        ans += logout[1]//15 - (login[1]//15 + int(login[1]%15>0))
        return max(ans, 0)

Complexity

Time: O(1) Space: O(1)

Yark-yao commented 2 years ago

思路

将时间转换成分钟(如果结束时间小于开始时间,则说明通宵了,给结束时间加上24小时),开始时间向后调整至可以被15整除,结束时间向前调整;

代码

function numberOfRounds(loginTime: string, logoutTime: string): number {
    // 时间,转换成数字格式
    let loginNum = _covertTime(loginTime)
    let logoutNum = _covertTime(logoutTime)
    if(logoutNum<loginNum){
        // 通宵了,加24小时
        logoutNum += 24*60
    }
    // 游戏是每15分钟开始一次,开始时间需要向后调整,结束时间需要向前调整
    if(loginNum%15 !== 0 ){
        loginNum = loginNum + (15 - loginNum%15)
    }
    if(logoutNum%15 !== 0){
        logoutNum = logoutNum - logoutNum%15
    }
    if(logoutNum<loginNum){
        return 0
    }
    return (logoutNum-loginNum)/15
};

function _covertTime(time:string){
    const hour = Number(time.slice(0,2))
    const min =  Number(time.slice(3,5))
    return hour*60 + min
}

复杂

时间:O(1) 空间:O(1)

Tesla-1i commented 2 years ago
class Solution:
    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
        loginTime_int = int(loginTime[0:2])*100 + int(loginTime[3:5])
        logoutTime_int = int(logoutTime[0:2])*100 + int(logoutTime[3:5])

        if loginTime_int > logoutTime_int:
            logoutTime_int += 2400

        loginhr = loginTime_int // 100
        loginmin = loginTime_int % 100
        logouthr = logoutTime_int // 100 - 1
        logoutmin = logoutTime_int % 100 + 60

        loginmin_count = loginmin // 15
        if loginmin % 15 != 0:
            loginmin_count += 1
        logoutmin_count = logoutmin // 15
        return max(logoutmin_count - loginmin_count + (logouthr - loginhr)*4, 0)
hdyhdy commented 2 years ago

思路:模拟时间

func numberOfRounds(loginTime string, logoutTime string) int {
    inHour := 0
    inMin := 0
    outHour := 0
    outMin := 0
    for i := 0; loginTime[i] != ':'; i ++ {
        inHour = inHour * 10 + int(loginTime[i] - '0')
        outHour = outHour * 10 + int(logoutTime[i] - '0')
    }
    for i := 3; i < len(loginTime); i ++ {
        inMin = inMin * 10 + int(loginTime[i] - '0')
        outMin = outMin * 10 + int(logoutTime[i] - '0')
    }
    ansHour := 0
    if inHour > outHour  || inHour == outHour && inMin > outMin{
        ansHour = 24 - inHour + outHour
    }else {
        ansHour = outHour - inHour 
    }
    if ansHour ==1 {
        return (60 -inMin) / 15 + outMin / 15
    }else if ansHour == 0 {
        return (outMin - inMin)/15*15/15
    }else {
        return (ansHour - 1) * 4 +  (60 -inMin) / 15 + outMin / 15
    } 
}

时间复杂度:1 空间复杂度:1

xuhzyy commented 2 years ago

思路

模拟

代码

class Solution:
    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
        loginTime = loginTime.split(':')
        logoutTime = logoutTime.split(':')
        loginH, loginM = int(loginTime[0]), int(loginTime[1])
        logoutH, logoutM = int(logoutTime[0]), int(logoutTime[1])
        loginM += 60 * loginH
        logoutM += 60 * logoutH
        if logoutM < loginM:
            logoutM += 60 * 24
        countIn = loginM // 15
        countIn += 0 if loginM % 15 == 0 else 1
        countOut = logoutM // 15
        return max(0, countOut - countIn)

复杂度分析

linyang4 commented 2 years ago

思路

把时间转换成分钟后再计算

代码

var numberOfRounds = function(loginTime, logoutTime) {
    const [startHour, startMin] = loginTime.split(':').map(num => Number(num))
    const [endHour, endMin] = logoutTime.split(':').map(num => Number(num))
    const startMins = startHour * 60 + startMin
    let endMins = endHour * 60 + endMin
    if (endMins < startMins) {
        endMins += 24 * 60
    }
    return Math.max(0, Math.floor(endMins / 15) - Math.ceil(startMins / 15))
};

复杂度

CodingProgrammer commented 2 years ago

思路

模拟

代码

class Solution {
    public int numberOfRounds(String startTime, String finishTime) {
        int startMM = Integer.parseInt(startTime.substring(0,2)) * 60 + Integer.parseInt(startTime.substring(3));
        int finishMM = Integer.parseInt(finishTime.substring(0,2)) * 60 + Integer.parseInt(finishTime.substring(3));
        if (startMM > finishMM) {
            finishMM += 1440;
        }
        finishMM = finishMM/15*15;
        return Math.max(0,finishMM-startMM)/15;
    }
}

复杂度

shamworld commented 2 years ago
var numberOfRounds = function(startTime, finishTime) {
    //将时间拆成数组
   let start=startTime.split(":").map(Number);
   let finish=finishTime.split(":").map(Number);
    //时间转换成分钟
   let startMM=start[0]*60+start[1];
   let finishMM=finish[0]*60+finish[1];
    //结束时间小于开始时间,通宵加一天60*24
   if(startMM>finishMM)finishMM+=1440;
   return Math.max(0,Math.floor(finishMM/15)-Math.ceil(startMM/15))
};
devosend commented 2 years ago

代码

class Solution:
    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:

        login_h, login_m = int(loginTime[:2]), int(loginTime[3:])
        logout_h, logout_m = int(logoutTime[:2]), int(logoutTime[3:])

        login = login_h*60 + login_m
        logout = logout_h*60 + logout_m 

        logout = logout+24*60 if logout < login else logout

        login += (15-login%15) if login%15 != 0 else 0
        logout-=logout%15

        time_diff = logout-login

        return max(time_diff//15, 0)

复杂度分析

iambigchen commented 2 years ago

思路

分别对开始时间的分钟数,和结束时间的分钟数做处理。开始时间分钟数如果没有到15,按15算。结束时间分钟数则反过来。

如果开始时间分钟数大于45时,小时数需要加1。在返回结果时,有可能出现负数情况,负数则表示一次都完不成。

关键点

代码

JavaScript Code:


/**
 * @param {string} loginTime
 * @param {string} logoutTime
 * @return {number}
 */
var numberOfRounds = function(startTime, finishTime) {
    let [sh, sm] = startTime.split(':').map(e => Number(e))
    let [eh, em] = finishTime.split(':').map(e => Number(e))

    let d = 0
    if (sh * 60 + sm > eh * 60 + em) d++
    if (sm > 0 && sm <= 15) {
        sm = 15
    } else if (sm > 15 && sm <= 30) {
        sm = 30
    } else if (sm > 30 && sm <= 45) {
        sm = 45
    } else if (sm > 45 && sm <= 60) {
        sm = 0
        sh++
    }
    if (em >= 0 && em < 15) {
        em = 0
    } else if (em >= 15 && em < 30) {
        em = 15
    } else if (em >= 30 && em < 45) {
        em = 30
    } else if (em >= 45 && em < 60) {
        em = 45
    }

    let st = sh * 60 + sm
    let et = eh * 60 + em
    et += d * 24 * 60
    return Math.max(0, (et - st) / 15)
};

复杂度分析

令 n 为数组长度。

Moin-Jer commented 2 years ago

思路


模拟

代码


class Solution {
    public int numberOfRounds(String loginTime, String logoutTime) {
        String[] loginTimes = loginTime.split(":");
        String[] logoutTimes = logoutTime.split(":");
        int start = Integer.parseInt(loginTimes[0]) * 60 + Integer.parseInt(loginTimes[1]);
        int end = Integer.parseInt(logoutTimes[0]) * 60 + Integer.parseInt(logoutTimes[1]);
        if (start > end) {
            end += 24 * 60;
        }
        end = end / 15 * 15;
        int ans = (end - start) / 15;
        return ans;
    }
}

复杂度分析


rzhao010 commented 2 years ago

Thoughts

  1. turn the input time into int value for cauculating, don't forget The he time may be over 24 hours
  2. assume the start time is the start of first game
  3. assume the endtime is the end of last game

Code

    public int numberOfRounds(String loginTime, String logoutTime) {
        // turn time into minutes
        int startMM = Integer.parseInt(loginTime.substring(0, 2)) * 60 
                        + Integer.parseInt(loginTime.substring(3, 5));
        int finishMM = Integer.parseInt(logoutTime.substring(0, 2)) * 60 
                        + Integer.parseInt(logoutTime.substring(3, 5));

        // see if the time exceed 1 day
        if (startMM > finishMM) {
            finishMM += 24 * 60;
        }

        // use startMM as the start for first full round of game
        // if startMM % 15 == 0, means it is the real start time for first roung
        if (startMM % 15 != 0) {
            // then we need to find the start time for the next round
            // +15 to the next time perioe, and reduce the time for the last roung
            startMM += 15 - startMM % 15;
        }

        // use finishMM as the time for the end of last game
        // ifd funishMM % 15 == 0, it is the exact time for the last round, 
        // if not, we track back to the game end time before the last round
        // which is finishMM - Finish % 15
        finishMM -= finishMM % 15;

        // incase finishMM < startMM
        int res = Math.max(0, finishMM - startMM) / 15;
        return res;
    }

Complexity Time: O(1) Space: O(1)

Flower-F commented 2 years ago

解题思路

模拟

代码

/**
 * @param {string} loginTime
 * @param {string} logoutTime
 * @return {number}
 */
var numberOfRounds = function(loginTime, logoutTime) {
    let [startHour, startMinute] = getTime(loginTime);
    let [endHour, endMinute] = getTime(logoutTime);
    let day = 0;

    if (startHour * 60 + startMinute > endHour * 60 + endMinute) {
        day++;
    }

    if (startMinute > 0 && startMinute < 15) {
        startMinute = 15;
    } else if (startMinute > 15 && startMinute < 30) {
        startMinute = 30;
    } else if (startMinute > 30 && startMinute < 45) {
        startMinute = 45;
    } else if (startMinute > 45 && startMinute < 60) {
        startMinute = 0;
        startHour++;
    }

    if (endMinute > 0 && endMinute < 15) {
        endMinute = 0;
    } else if (endMinute > 15 && endMinute < 30) {
        endMinute = 15;
    } else if (endMinute > 30 && endMinute < 45) {
        endMinute = 30;
    } else if (endMinute > 45 && endMinute < 60) {
        endMinute = 45;
    }

    const startTime = startHour * 60 + startMinute;
    const endTime = endHour * 60 + endMinute + 24 * 60 * day;

    // return (endTime - startTime) / 15; // 注意此处时间有可能小于零
    return Math.max(0, endTime - startTime) / 15;

    function getTime(time) {
        const arr = time.split(':');
        arr[0] = parseInt(arr[0]);
        arr[1] = parseInt(arr[1]);
        return arr;
    }
};

复杂度:

时间:O(1) 空间:O(1)

tangjy149 commented 2 years ago

思路

将时间取出,为了简单,将其转为分钟进行计算。endTime/15*15是筛选不完整对局

代码

class Solution {
public:
    int numberOfRounds(string loginTime, string logoutTime) {
        int startTime=stoi(loginTime.substr(0,2))*60+stoi(loginTime.substr(3,5));
        int endTime=stoi(logoutTime.substr(0,2))*60+stoi(logoutTime.substr(3,5));
        if(startTime>=endTime){
            endTime+=24*60;
        }
        return (endTime/15*15-startTime)/15;
    }
};
Toms-BigData commented 2 years ago

【Day 34】1904. 你完成的完整对局数

思路

模拟

golang代码

func numberOfRounds(loginTime string, logoutTime string) (ans int) {
    array1:=strings.Split(loginTime,":")
    array2:=strings.Split(logoutTime, ":")
    in1,_:=strconv.Atoi(array1[0])
    in2,_:=strconv.Atoi(array1[1])
    out1,_:=strconv.Atoi(array2[0])
    out2,_:=strconv.Atoi(array2[1])
    if (in1>out1)||(in1 == out1 && in2>out2){
        out1+=24
    }
    if in2%15==0{
        ans+=1
    }else {
        in2 = in2-(in2%15)
    }
    out2 = out2-(out2%15)
    ans += (out1-in1)*4+(out2-in2)/15-1
    if ans<0{
        ans = 0
    }
    return

复杂度

时间:O(1) 空间:O(1)

yingliucreates commented 2 years ago
const numberOfRounds = function(startTime, finishTime) {
  let start=60*parseInt(startTime.slice(0,2))+parseInt(startTime.slice(3))
  let end=60*parseInt(finishTime.slice(0,2))+parseInt(finishTime.slice(3))
  if(start>end) end+=24*60
  return Math.max(0, (Math.floor(end/15) - Math.ceil(start/15)))
};
honeymeng-hub commented 2 years ago

class Solution { public: int numberOfRounds(string startTime, string finishTime) { int start = stoi(startTime.substr(0, 2)) 60 + stoi(startTime.substr(3, 5)); int finish = stoi(finishTime.substr(0, 2)) 60 + stoi(finishTime.substr(3, 5)); if (start > finish) finish += 1440; finish = finish / 15 * 15; return max(0, (finish - start) / 15); } };

alongchong commented 2 years ago
class Solution {
    public int numberOfRounds(String startTime, String finishTime) {

        int startMM  = Integer.parseInt(startTime.substring(0,2)) * 60 + Integer.parseInt(startTime.substring(3,5));
        int finishMM = Integer.parseInt(finishTime.substring(0,2)) * 60 + Integer.parseInt(finishTime.substring(3,5));
        if(startMM > finishMM) finishMM += 24 * 60; // 多加上一天的分钟
        if(startMM % 15 != 0) startMM += 15 - startMM % 15; 
        finishMM -= finishMM % 15;
        int res = Math.max(0, finishMM - startMM) / 15;  // 防止startMM 大于 finishMM
        return res;

    }
}
ZJP1483469269 commented 2 years ago
class Solution {
    public int numberOfRounds(String startTime, String finishTime) {
        int startHH = Integer.parseInt(startTime.substring(0,2));
        int startMM = Integer.parseInt(startTime.substring(3));
        int endHH = Integer.parseInt(finishTime.substring(0,2));
        int endMM = Integer.parseInt(finishTime.substring(3));
        int a=0,b=0;
        if(startHH ==endHH) {
            if(startMM<endMM){
                if(endMM- startMM<15)return 0;
                if(endMM%15==0){
                    b=0;
                }else b=1;
                return Math.max(0,(60-startMM)/15-(60-endMM)/15-b);
            }
            else return 92+(60-startMM)/15+endMM/15;
        }
        else{
            return get_hours(startHH,endHH)*4+get_MM(startMM,endMM);
        }

    }
    public int get_hours(int startHH,int endHH){
        if((startHH+1)%24<=endHH){
            return endHH-(startHH+1)%24;
        }else{
            return (24-startHH-1)+endHH;
        }
    }
    public int get_MM(int startMM,int endMM){
        return (60-startMM)/15 + (endMM)/15;
    }
}
charlestang commented 2 years ago

思路

模拟。就是情况比较复杂。因为输入时间绝不可能相等,要把情况分成两种。

  1. 开始 < 结束 a. 在同一个小时 b. 不在同一个小时
  2. 开始 > 结束

几种情况分别统计处理后,就可以计算出结果。题目没什么,就是边界情况太多。比较耗费脑力。

代码

class Solution:
    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
        sth, stm = map(int, loginTime.split(':'))
        enh, enm = map(int, logoutTime.split(':'))
        ans = 0
        if sth < enh or (sth == enh and stm < enm): # 开始时间 < 结束时间
            if sth == enh:
                # 计算分钟覆盖的比赛
                for i in range(4):
                    st = i * 15
                    en = (i + 1) * 15
                    if stm <= st and enm >= en:
                        ans += 1
            else:
                hour = enh - sth - 1
                ans = (60 - stm) // 15 + hour * 4 + enm // 15
        else: # 开始时间 > 结束时间
            ans += (24 - sth - 1) * 4 + (60 - stm) // 15
            ans += enh * 4 + enm // 15
        return ans

时间和空间复杂度都是 O(1)

superduduguan commented 2 years ago

class Solution: def numberOfRounds(self, loginTime: str, logoutTime: str) -> int: t0 = 60 int(loginTime[:2]) + int(loginTime[3:]) t1 = 60 int(logoutTime[:2]) + int(logoutTime[3:]) if t1 < t0: t1 += 1440 t1 = t1 // 15 * 15 return max(0, (t1 - t0)) // 15

LannyX commented 2 years ago

代码

class Solution {
    public int numberOfRounds(String loginTime, String logoutTime) {
        int inHH = Integer.parseInt(loginTime.substring(0,2));
        int outHH = Integer.parseInt(logoutTime.substring(0,2));
        int inMin = Integer.parseInt(loginTime.substring(3));
        int outMin = Integer.parseInt(logoutTime.substring(3));

        if(inHH == outHH && outMin - inMin > 0 && outMin - inMin < 15){
            return 0;
        }
        int inIdx = inHH * 4 + (inMin + 14)/15;
        int outIdx = outHH * 4 + outMin /15;

        if(outIdx < inIdx){
            return 96 - inIdx + outIdx;
        }
        return outIdx - inIdx;

    }
}

复杂度分析

haixiaolu commented 2 years ago

思路

模拟

代码 / Python

class Solution:
    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
        start = int(loginTime[:2]) * 60 + int(loginTime[3:])
        finish = int(logoutTime[:2]) * 60 + int(logoutTime[3:])

        if finish < start:
            finish += 24 * 60

        finish = finish // 15 * 15
        return (finish - start) // 15 if finish > start else 0 

复杂度分析

zhiyuanpeng commented 2 years ago
class Solution:
    def numberOfRounds(self, startTime: str, finishTime: str) -> int:
        sh, sm = map(int, startTime.split(":"))
        eh, em = map(int, finishTime.split(":"))
        d = 0
        if sh * 60 + sm > eh * 60 + em: d += 1
        if 0 < sm <= 15:
            sm = 15
        elif 15 < sm <= 30:
            sm = 30
        elif 30 < sm <= 45:
            sm = 45
        elif 45 < sm <= 60:
            sm = 0
            sh += 1
        if 0 <= em < 15:
            em = 0
        elif 15 <= em < 30:
            em = 15
        elif 30 <= em < 45:
            em = 30
        elif 45 <= em < 60:
            em = 45
        st = sh * 60 + sm
        et = eh * 60 + em
        if d == 1: et += 24 * 60
        return max(0, (et - st)) // 15
junbuer commented 2 years ago

思路

代码

class Solution(object):
    def numberOfRounds(self, loginTime, logoutTime):
        """
        :type loginTime: str
        :type logoutTime: str
        :rtype: int
        """
        time1 = loginTime.split(":")
        time2 = logoutTime.split(":")
        start = int(loginTime[:2]) * 60  + int(loginTime[3:])
        end = int(logoutTime[:2]) * 60  + int(logoutTime[3:])

        if end < start:
            end += 24 * 60
        if start % 15 != 0:
            start += (15 - start % 15)

        return (end - start) // 15 if end - start >= 15 else 0

复杂度分析

Tomtao626 commented 2 years ago

思路

tongxw commented 2 years ago

思路

  1. 把时间转化为分钟数字
  2. 想象一个x坐标轴,以15为刻度,问login和logout之间有几个刻度:logout/15向下取整(坐标轴靠左),login/15向上取整(坐标轴靠右),最后求差即可

    class Solution {
    public int numberOfRounds(String loginTime, String logoutTime) {
        int login = convert(loginTime);
        int logout = convert(logoutTime);
        if (login > logout) {
            logout += 24 * 60;
        }
    
        int round = (int)Math.floor((double)logout / 15) - (int)Math.ceil((double)login / 15);
        return round < 0 ? 0 : round;
    }
    
    private int convert(String time) {
        String[] times = time.split(":");
        int minutes = 0;
        try {
            minutes = Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return minutes;
    }
    }

    TC /SC : O(1)

zol013 commented 2 years ago
class Solution:
    def numberOfRounds(self, startTime: str, finishTime: str) -> int:
        start_hour, start_minute = (int(x) for x in startTime.split(':'))
        start_time = start_hour * 60 + start_minute
        finish_hour, finish_minute = (int(x) for x in finishTime.split(':'))
        finish_time = finish_hour * 60 + finish_minute
        if 0 <= finish_time - start_time < 15: return 0
        return finish_time // 15 - (start_time + 14)// 15 + (start_time > finish_time) * 24 * 60 // 15
        # finish_time // 15 is the floor function, we convert finish time to same/earlier 15 min count
        #(start_time + 14) // 15 is the ceiling function, we convert start time to same/ later 15 min count
biscuit279 commented 2 years ago

思路:转换成分钟就方便le

class Solution(object):
    def numberOfRounds(self, loginTime, logoutTime):
        """
        :type loginTime: str
        :type logoutTime: str
        :rtype: int
        """
        start_h, start_m = int(loginTime.split(':')[0]), int(loginTime.split(':')[1])
        finish_h, finish_m = int(logoutTime.split(':')[0]), int(logoutTime.split(':')[1])
        t0 = start_h * 60 + start_m
        t1 = finish_h * 60 + finish_m
        if t0 >= t1:
            t1 += 60*24
        t1 = t1 // 15 * 15
        return max(0,(t1-t0)) // 15

复杂度:O(1)

ginnydyy commented 2 years ago

Problem

https://leetcode.com/problems/the-number-of-full-rounds-you-have-played/

Notes

Solution

class Solution {
    public int numberOfRounds(String startTime, String finishTime) {
        int startMinutes = parseTime(startTime);
        int finishMinutes = parseTime(finishTime);   

        int roundedStart = getStart(startMinutes);
        int roundedFinish = getFinish(finishMinutes);

        // compare the minutes before rounded
        // rounded finish could be less than rounded start, diff may be negative
        // so need to get the max between 0 and diff as the result, i.e. 12:58 to 12:59
        if(startMinutes < finishMinutes){
            return Math.max(0, (roundedFinish - roundedStart) / 15); 
        }

        return (roundedFinish + 60 * 24 - roundedStart) /15;
    }

    // round to the next or current round as the start round
    private int getStart(int startMinutes){
        return (startMinutes + 14) / 15 * 15;
    }

    // round to the previous or current round as the finish round
    private int getFinish(int finishMinutes){
        return finishMinutes / 15 * 15;        
    }

    // parse string time to int minutes
    private int parseTime(String time){
        String[] parsedTime = time.split(":");
        int hour = Integer.parseInt(parsedTime[0]);
        int minute = Integer.parseInt(parsedTime[1]);
        return hour * 60 + minute;
    }
}

Complexity

ZZRebas commented 2 years ago

思路

先对齐开始、结束得时间,再计算时间差,再除以15得对局数

代码(Python)

def fun(startTime,finishTime):
    h_s=int(startTime.split(':')[0])
    m_s=int(startTime.split(':')[1])
    h_e=int(finishTime.split(':')[0])
    m_e=int(finishTime.split(':')[1])

    if 0<m_s<15 : m_s=15
    elif 15<m_s<30 : m_s=30
    elif 30<m_s<45 : m_s=45
    elif 45<m_s<=59 :
        m_s=0
        h_s=h_s+1

    if 0<m_e<15 : m_e=0
    elif 15<m_e<30 : m_e=15
    elif 30<m_e<45 : m_e=30
    elif 45<m_e<=59 :
        m_e=45
    # print(h_s,m_s)
    # print(h_e,m_e)
    start=h_s*60+m_s
    end=h_e*60+m_e
    if start>end:
        count=end//15+(24*60-start)//15
    else:
        count=(end-start)//15
    print(count)

startTime = "20:00"
finishTime = "06:00"
fun(startTime,finishTime)   #40

复杂度分析

Riuusee commented 2 years ago

思路

改成分钟模式,再具体处理

代码

class Solution {
    public int numberOfRounds(String loginTime, String logoutTime) {
        int login = Integer.parseInt(loginTime.substring(0,2))*60 + Integer.parseInt(loginTime.substring(3,5));
        int logout = Integer.parseInt(logoutTime.substring(0,2))*60 + Integer.parseInt(logoutTime.substring(3,5));

        int res = 0;
        if (logout < login) logout += 1440;
        logout -= logout%15;
        if (login%15 != 0) login += 15-login%15;

        if (login > logout) return 0;
        res = (logout - login) /15;

        return res;
    }
}

复杂度分析

jiaqiliu37 commented 2 years ago
class Solution:
    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
        a, b = (int(x) for x in loginTime.split(':'))
        c, d = (int(x) for x in logoutTime.split(':'))
        if a > c or (a == c and b > d):
            c += 24

        e = round(b/15)
        f = round((60-d)/15)
        if b/15 >e:
            e += 1
        if (60-d)/15 >f:
            f += 1

        ans = (c - a) * 4 + 4 - e - f

        return max(ans, 0)

Time complexity O(1) Space complexity O(1)

wenlong201807 commented 2 years ago

代码块


var numberOfRounds = function (startTime, finishTime) {
  //将时间拆成数组
  let start = startTime.split(':').map(Number);
  let finish = finishTime.split(':').map(Number);
  //时间转换成分钟
  let startMM = start[0] * 60 + start[1];
  let finishMM = finish[0] * 60 + finish[1];
  //结束时间小于开始时间,通宵加一天60*24
  if (startMM > finishMM) finishMM += 1440;

  return Math.max(0, Math.floor(finishMM / 15) - Math.ceil(startMM / 15));
};

时间复杂度和空间复杂度

Myleswork commented 2 years ago

思路

其实就是计算in和out两个时间之间有多少个15分钟,当然需要注意一点需要把out的时间规范到前一个结束时间。可以通过整除15后再乘15的方法规范

代码

class Solution {
public:
    int str2int(string str){
        int res = 0;
        int i = 0;
        while(i<str.length()){
            res = res*10 + (str[i]-48);
            i++;
        }
        return res;
    }
    int numberOfRounds(string loginTime, string logoutTime) {
        int in = 60*str2int(loginTime.substr(0,2)) + str2int(loginTime.substr(3,5));
        int out = 60*str2int(logoutTime.substr(0,2)) + str2int(logoutTime.substr(3,5));
        if (out<in) out+=(24*60);
        out = out/15*15;
        return max(0,out-in)/15;
     }
};

复杂度分析

时间复杂度:O(1)

空间复杂度:O(1)

KennethAlgol commented 2 years ago

思路

计算有多少个15分钟

class Solution {
    public int numberOfRounds(String startTime, String finishTime) {
        int startMinutes = getMinutes(startTime);
        int endMinutes = getMinutes(finishTime);
        return calculateRounds(startMinutes, endMinutes);
    }

    private int getMinutes(String time) {
        String[] array = time.split(":");
        int[] t = new int[2];
        t[0] = Integer.parseInt(array[0]);
        t[1] = Integer.parseInt(array[1]);
        return t[0] * 60 + t[1];
    }

    private int calculateRounds(int start, int end) {
        if (end < start) {
            end += 24 * 60;
        }
        int res = (int) Math.floor(end / 15D) - (int) Math.ceil(start / 15D);
        if(res < 0){
            return 0;
        }else{
            return res;
        }
    }
}

复杂度分析

时间复杂度 O(1) 空间复杂度 O(1)