我是Python和SWE的新手,所以请原谅简单的问题。面试官给了我以下编码挑战。我提出了以下解决方案。但我被通过了,因为它没有达到他们的表现标准。我想知道是否有人可以指点我如何在这个问题上做得更好,一般来说这样的问题。我已经找到了解决问题的其他答案,但我想要实现具体的答案。
以下是我收到的反馈意见:
问题 给定一组邮政编码范围(每个范围包括两者 他们的上限和下限), 提供一种算法,产生所需的最小范围数 表示与输入相同的覆盖范围。
Input: [[14,17], [4,7], [2,5], [10,12] , [15,16], [4,9], [11,13]]
Output: [[2,17]]
# Implementation
def zip_range_reducer(zip_range_list):
if not zip_range_list:
raise Exception("Empty list of ranges provided!")
reduced_zip_ranges = []
zip_range_list.sort()
while (zip_range_list):
no_overlap_ranges = []
reduced_zip_ranges.append(zip_range_list[0])
if len(zip_range_list) == 1:
return reduced_zip_ranges
zip_range_list.pop(0)
for zip_range in zip_range_list:
low, high = reduced_zip_ranges[-1][0], reduced_zip_ranges[-1][1]
range_check = range(low-1, high+2)
if zip_range[0] in range_check or zip_range[1] in range_check:
reduced_zip_ranges[-1][0] = min(reduced_zip_ranges[-1][0], zip_range[0])
reduced_zip_ranges[-1][1] = max(reduced_zip_ranges[-1][1], zip_range[1])
else:
no_overlap_ranges.append(zip_range)
zip_range_list = no_overlap_ranges
return reduced_zip_ranges
另外
答案 0 :(得分:0)
这是O(n)解决方案。 [不包括排序的复杂性]
def reduceRangeList(ranges):
# sorting the list based on the lower limit, and then based on size as tie breaker
# note we need the bigger ranges to come before the smaller ranges
ranges.sort(key= lambda pair : (pair[0], - pair[1]))
reduced= [] # new set of ranges are stored here
# we use a parent range to decide if a range is inside other ranges
parent= ranges[0]
# this will for sure be part of the solution,
# because it is the largest, leftmost range
reduced.append(ranges[0])
for x in ranges:
if parent[0] <= x[0] and x[1] <= parent[1]:
# this range is completely within another range, ignore!
continue
elif x[0] <= parent[1]:
# this range is partially inside the parent range
# so we set the parent to cover this two range
parent= [parent[0], x[1]]
else:
#this range is completely outside the parent range
parent= x
# If the range is completely or partially outside other ranges...
# I'm placing it here to avoid duplicate code
reduced.append(x)
return reduced
def main():
ranges= [[1,5], [2, 4], [6, 7], [2,7], [9,10]]
# ranges= [[1,5], [1,4], [2, 6]]
# ranges= [[1,2], [3,4], [5,6], [1,6], [6,7], [4, 8]]
reduced= reduceRangeList(ranges)
print(reduced)
if __name__ == '__main__':
main()