题解 | #ranko的手表#

ranko的手表

https://www.nowcoder.com/practice/37275e85ae7c4453920eae6b9f7f45fc

import sys

class Solution:

    def solve(self, str1: str, str2:str) -> (int, int):
        hour1 = str1.split(":")[0]
        minute1 = str1.split(":")[1]

        hour2 = str2.split(":")[0]
        minute2 = str2.split(":")[1]

        # compare hour
        candidateHourList1 = self.candidateHourList(hour1)
        candidateHourList2 = self.candidateHourList(hour2)

        # compare minute
        candidateMinList1 = self.candidateMinuteList(minute1)
        candidateMinList2 = self.candidateMinuteList(minute2)

        # maketime cadidate
        cand1 = []
        for h1 in candidateHourList1:
            for m1 in candidateMinList1:
                cand1.append(h1 * 60 + m1)

        cand2 = []
        for h2 in candidateHourList2:
            for m2 in candidateMinList2:
                cand2.append(h2 * 60 + m2)


        maxdist = abs(cand1[0] - cand2[-1])
        mindist = 24 * 60
        ind1 = 0
        ind2 = 0
        while ind1 < len(cand1) and ind2 < len(cand2):
            if cand2[ind2] >= cand1[ind1]:
                mindist = min(mindist, abs(cand1[ind1] - cand2[ind2]))
            if cand1[ind1] == cand2[ind2]:
                break
            elif cand1[ind1] >= cand2[ind2]:
                ind2 += 1
            else:
                ind1 += 1
        
        return max(1, mindist), maxdist


    def candidateHourList(self, str: str) -> List[int]:
        if '?' not in str:
            return [int(str)]
        
        if str == '??':
            return [ind for ind in range(24)]

        if str[0] == '?':
            if str[1] >= '4':
                return [int(str[1]), int('1' + str[1])]
            else:
                return [int(str[1]), int('1' + str[1]), int('2' + str[1])]

        if str[1] == '?':
            if str[0] == '2':
                return [int(str[0]+'0'), int(str[0]+'1'), int(str[0]+'2'), int(str[0]+'3')]
            else:
                return [int(str[0])*10+ind for ind in range(10)]

        return []

    def candidateMinuteList(self, str: str) -> List[int]:
        if '?' not in str:
            return [int(str)]
        if str == '??':
            return [ind for ind in range(60)]

        if str[0] == '?':
            return [int(str[1])+ind*10 for ind in range(6)]

        if str[1] == '?':
            return [int(str[0])*10+ind for ind in range(10)]

        return []



message1 = input() # 输入行信息
message2 = input() # 输入行信息

solution = Solution()
mindist, maxdist = solution.solve(message1, message2)
print(mindist, maxdist)




# s1 = input()
# s2 = input()
 
# # 创建两个空列表存储可能的时间
# list1 = []
# list2 = []
 
# # 遍历0 - 60 * 24 分钟
# for i in range(0, 60 * 24):
 
#     hour = int(i //60) # 获取小时
#     minute = int(i % 60) # 获取分钟
     
#     # 十位数等于"?",或者与hour(mimute) // 10相等时,遍历值可取
#     # 十位数等于"?",或者与hour(mimute) % 10相等时,遍历值可取
#     if s1[0] == '?' or int(s1[0]) == hour // 10:
#         if s1[1] == '?' or int(s1[1]) == hour % 10:
#             if s1[3] == '?' or int(s1[3]) == minute // 10:
#                 if s1[4] == '?' or int(s1[4]) == minute % 10:
#                     list1.append(i)
     
#     if s2[0] == '?' or int(s2[0]) == hour // 10:
#         if s2[1] == '?' or int(s2[1]) == hour % 10:
#             if s2[3] == '?' or int(s2[3]) == minute // 10:
#                 if s2[4] == '?' or int(s2[4]) == minute % 10:
#                     list2.append(i)
 
# # 初始化最大值、最小值
# mintime = 60 * 24
# maxtime = 0
# # 遍历两个列表,取可能的组合,计算最大值、最小值
# for i in range(len(list1)):
#     for j in range(len(list2)):
#         if list1[i] < list2[j]:
#             mintime = min(mintime, list2[j] - list1[i])
#             maxtime = max(maxtime, list2[j] - list1[i])
 
# print(mintime, maxtime)

# print(list1)
# print(list2)

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务