Skip to content

Latest commit

 

History

History
54 lines (48 loc) · 1.77 KB

91alg_day34_lc1904.md

File metadata and controls

54 lines (48 loc) · 1.77 KB

Idea

  • String to int[]
  • If pass midnight, change to call function(loginTime, "24:00") and function("00:00", logoutTime)
  • Two different situations:
    • Login logout time in different hours: rounds in full hours + rounds before full hour + rounds after full hour
    • Login logout time within same hour: rounds in ("00:00", logoutTime) - rounds in ("00:00", loginTime)

Code

class Solution {
    public int numberOfRounds(String loginTime, String logoutTime) {
        int[] login = new int[]{Integer.parseInt(loginTime.substring(0,2)),Integer.parseInt(loginTime.substring(3,5))};
        int[] logout = new int[]{Integer.parseInt(logoutTime.substring(0,2)),Integer.parseInt(logoutTime.substring(3,5))};
        if((logout[0]<login[0]) || ((logout[0]==login[0])&&(logout[1]<login[1]))){
            // more than one day
            return numberOfRounds(loginTime, "24:00")+numberOfRounds("00:00", logoutTime);
        }else{
            int res = 0;
            // compeleted hour = 4
            int hours = logout[0]-login[0];
            if(hours>0){
                res += (hours-1)*4;
                // minutes
                if(login[1]%15>0){
                    res += (4-login[1]/15-1);
                }else{
                    res += (4-login[1]/15);
                }
                res += logout[1]/15;
            }else{
                int min = 0;
                if(login[1]%15>0){
                    min = login[1]/15+1;
                }else{
                    min = login[1]/15;
                }
                res = logout[1]/15-min;
            }
            if(res<0){
                return 0;
            }
            return res;
        }
        
    }
}

Complexity Analysis

  • Time Complexity: O(n) // n rounds?
  • Space Complexity: O(1) // ?