📅  最后修改于: 2023-12-03 15:26:28.690000             🧑  作者: Mango
给定一个长度为n的数组nums,找到最长的子数组,使得相邻元素至少有一个公共数字。如果存在多个最长子数组,则返回其中任意一个。
输入: [1,2,3,4,5,6]
输出: [1,2,3,4,5,6]
输入: [1,2,3,4,5,1,2,3]
输出: [1,2,3,4,5,1,2,3]
首先,题目中要求相邻元素至少有一个公共数字,那么我们想到可以维护一个公共数字的集合,比如pub_set。每次找到相邻两个元素,将他们的pub_set取交集,得到新的pub_set,然后比较新旧pub_set的大小。
我们需要用到一个dict来维护每个数字出现的位置。key是数字,value是出现的位置的列表。
算法时间复杂度O(n),空间复杂度O(n)。
def longest_common_sequence(nums):
pos_dict = {}
pub_set = set(nums)
for i, num in enumerate(nums):
if num in pos_dict:
pub_set &= set(pos_dict[num])
pos_dict[num] = pos_dict.get(num, [])
pos_dict[num].append(i)
max_len = 0
max_range = []
start, end = 0, -1
for i, num in enumerate(nums):
if i == 0 or nums[i] != nums[i-1]:
pub_set &= set(pos_dict[num])
if i == len(nums)-1 or nums[i] != nums[i+1]:
for pos in pub_set:
if pos > end:
end = pos
start = end - len(pub_set) + 1
break
if end-start+1 > max_len:
max_len = end-start+1
max_range = [start, end]
return nums[max_range[0]:max_range[1]+1]
测试代码:
assert longest_common_sequence([1,2,3,4,5,6]) == [1,2,3,4,5,6]
assert longest_common_sequence([1,2,3,4,5,1,2,3]) == [1,2,3,4,5,1,2,3]
assert longest_common_sequence([1,2,3,4,1,2,3,5]) == [1,2,3,4,1,2,3]
assert longest_common_sequence([1,2,3,4,1,5,6,7]) == [1,2,3,4,1]
assert longest_common_sequence([1,2,3,4,5,6,5]) == [5,6,5]