Python DFS (beat 60%)


  • 0
    S
        def dfs(curr_res, time, res_list):
            if len(curr_res) == 5:
                if curr_res > time:
                    res_list.append(curr_res)
                return
            elif len(curr_res) == 0:
                for c in time:
                    if '0' <= c <= '2':
                        dfs(curr_res+c, time, res_list)
            elif len(curr_res) == 4:
                for c in time:
                    if '0' <= c <= '9':
                        dfs(curr_res+c, time, res_list)
            elif len(curr_res) == 2:
                dfs(curr_res+':', time, res_list)
            elif len(curr_res) == 3:
                for c in time:
                    if '0' <= c <= '5':
                        dfs(curr_res+c, time, res_list)
            elif len(curr_res) == 1:
                if curr_res[0] <= '1':
                    for c in time:
                        if '0' <= c <= '9':
                            dfs(curr_res+c, time, res_list)
                else:
                    for c in time:
                        if '0' <= c <= '3':
                            dfs(curr_res+c, time, res_list)
        res = []
        dfs('', time, res)
        # print res
        if len(res) > 0:
            return min(res)
        else:
            return min(time)*2 + ':' + min(time)*2

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.