Find subset with K elements that are closest to eachother

后端 未结 6 872
误落风尘
误落风尘 2020-12-30 04:44

Given an array of integers size N, how can you efficiently find a subset of size K with elements that are closest to each other?

Le

6条回答
  •  被撕碎了的回忆
    2020-12-30 04:55

    After sorting, we can be sure that, if x1, x2, ... xk are the solution, then x1, x2, ... xk are contiguous elements, right?

    So,

    1. take the intervals between numbers
    2. sum these intervals to get the intervals between k numbers
    3. Choose the smallest of them

提交回复
热议问题