Karat course times
unknown
python
3 years ago
2.3 kB
10
Indexable
""" generate map of name -> list of times for name, times in map: sorted_times = sort times by asc order curr_head = 0 temp = [sorted_times[0]] for i, curr_end in sorted_times[1:]: get diff(sorted_times[curr_head], curr_end) if diff < 60: add curr_end to temp else if len(temp) > 1: # already have an interval break else: # reset interval temp = [curr_end] curr_head = i if len(temp) > 1: add {name: temp} to res return res """ def solve(pairs: List[List[str]]) -> Dict[str, List[str]]: name_times_map = {} for name, time in pairs: if name not in name_times_map: name_times_map[name] = [] name_times_map[name].append(time) name_intervals_map = {} for name, times in name_times_map.items(): sorted_times = sorted(times) curr_head_idx = 0 curr_interval = [sorted_times[0]] for curr_end_idx in range(1, len(sorted_times)): curr_head = sorted_times[curr_head_idx] curr_end = sorted_times[curr_end_idx] time_diff = time_diff_in_mins(curr_end, curr_head) if time_diff < 60: curr_interval.append(curr_end) elif len(curr_interval) > 1: break else: curr_interval = [curr_end] curr_head_idx = curr_end_idx if len(curr_interval) > 1: name_intervals_map[name] = curr_interval return name_intervals_map def time_diff_in_mins(x: str, y: str) -> float: x = int(x) y = int(y) hour_x = int(x / 100) hour_y = int(y / 100) min_x = x % 100 min_y = y % 100 return (hour_x * 60 + min_x) - (hour_y * 60 + min_y) testcase1 = [["James", "1300"], ["Martha", "1600"], ["Martha", "1620"], ["Martha", "1530"]] testcase2 = [["James", "1300"], ["James", "1330"], ["Martha", "1600"], ["Martha", "1620"], ["Martha", "1400"]] testcase3 = [["James", "1300"], ["Martha", "1600"], ["Martha", "1630"], ["Martha", "1800"], ["Martha", "1830"]] assert solve(testcase1) == {'Martha': ['1530', '1600', '1620']} assert solve(testcase2) == {'James': ['1300', '1330'], 'Martha': ['1600', '1620']} assert solve(testcase3) == {'Martha': ['1600', '1630']}
Editor is loading...