[英]Divide a list into sublists such that difference between any 2 elements in a sublist should not exceed k
Can anyone provide an approach for this.谁能为此提供一种方法。
Divide a list into sublists such that the absolute difference between any 2 elements in a sublist should not exceed a 'k' value.sublists can be formed irrespective of order.将列表划分为子列表,使得子列表中任意 2 个元素之间的绝对差不应超过“k”值。子列表的形成与顺序无关。 A sublist can be of any number of elements.子列表可以包含任意数量的元素。 The number of sublists should be minimal子列表的数量应该最少
Example:
arr=[1,5,4,6,8,9,2]
k=3
sublists generated are [[2,1],[5,4,6],[8,9]]
Example 2:
arr=[1,13,6,8,9,3,5]
k=4
sublists generated are [[1,3,5],[6,8],[13,9]]
We need to return the minimal number of sublist where the difference between 2 lists must not exceed k.我们需要返回最小数量的子列表,其中 2 个列表之间的差异不得超过 k。 an element can be in only 1 sublist一个元素只能在 1 个子列表中
I have tried my best to solve it, but couldn't.我已尽力解决它,但不能。 Any help is appreciated任何帮助表示赞赏
Thanks谢谢
# easier version
arr = sorted(arr)
lst = []
sublist = [arr.pop(0)]
while len(arr) > 0:
item = arr.pop(0)
if item - sublist[0] >= k:
lst.append(sublist)
sublist = [item]
else:
sublist.append(item)
lst.append(sublist)
print(lst)
# shorter version
arr = sorted(arr)
lst = [[arr.pop(0)]]
while len(arr) > 0:
item = arr.pop(0)
lst.append([item]) if item - lst[-1][0] >= k else lst[-1].append(item)
print(lst)
Here is a simple approach using the sorted array:这是使用排序数组的简单方法:
NB.注意。 I used a strict threshold here, update the code to use "<=" if equality is accepted.我在这里使用了严格的阈值,更新代码以在接受相等性时使用“<=”。
def split(arr, k):
out = []
for i in sorted(arr):
if not out:
out = [[i]]
continue
if out[-1] and i-out[-1][0] < k: # use <= to accept equality
out[-1].append(i)
else:
out.append([i])
return out
example:例子:
split([1,5,4,6,8,9,2], 3)
# [[1, 2], [4, 5, 6], [8, 9]]
split([1,13,6,8,9,3,5], 4)
# [[1, 3], [5, 6, 8], [9], [13]]
def ex(arr, k):
arr = sorted(arr)
new_lst = []
sub_lst = [arr[0]]
for i in range(1, len(arr)):
if arr[i] - sub_lst[0] < k:
sub_lst.append(arr[i])
else:
new_lst.append(sub_lst)
sub_lst = [arr[i]]
new_lst.append(sub_lst)
return new_lst
arr = [1, 13, 6, 8, 9, 3, 5]
k = 4
print(ex(arr, k))
Little explanation: We are sorting the list.小解释:我们正在对列表进行排序。 Than we check for the first element the matching items.比我们检查匹配项的第一个元素。 Because the list is sorted, they will be right next to him in the list.因为列表是排序的,所以他们将在列表中紧挨着他。 If the next element isnt matching, we can "close" this sub_lst, append it to the new_lst, and than initialize the sub_lst with the next item.如果下一个元素不匹配,我们可以“关闭”这个 sub_lst,append 到 new_lst,然后用下一个项目初始化 sub_lst。 Once we finished, we need to append sub_lst to new_lst because we didn't get in the if statement there.完成后,我们需要将 append sub_lst 转换为 new_lst,因为我们没有进入那里的 if 语句。
Example:例子:
import random
k=10
random.seed(10)
randomList = []
listOfSublist = []
for i in range(0,10):
n = random.randint(1,100)
randomList.append(n)
print(randomList)
# uncomment this to test with the example
# randomList = [1,13,6,8,9,3,5]
# k = 4
randomList.sort()
print(randomList)
n = 0
for i in range(len(randomList)-1):
if randomList[i+1] - randomList[n] >= k: # when difference exceed k, create sublist
listOfSublist.append( randomList[n:i+1])
n=i+1
listOfSublist.append( randomList[n:len(randomList)]) # handle the end of the list
print(listOfSublist)
I believe there is a solution without sorting, and you can improve this by deleting the numbers you pick from randomList at each step, in case memory is an issue.我相信有一个不排序的解决方案,您可以通过删除每一步从 randomList 中选择的数字来改进它,以防 memory 出现问题。
output: output:
[74, 5, 55, 62, 74, 2, 27, 60, 63, 36]
[2, 5, 27, 36, 55, 60, 62, 63, 74, 74]
[[2, 5], [27, 36], [55, 60, 62, 63], [74, 74]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.