【发布时间】:2020-06-24 05:40:20
【问题描述】:
问题:给定一个表示编号的整数(n)。最初的粒子
给定这些粒子的大小数组
这些粒子可以进入任意数量的模拟(可能没有)
在一个模拟中,两个粒子结合起来产生另一个粒子,其大小为它们之间的大小之差(可能为 0)。
找到可以形成的最小粒子。
constraints
n<=1000
size<=1e9
Example 1
3
30 10 8
Output
2
Explaination- 10 - 8 is the smallest we can achive
Example 2
4
1 2 4 8
output
1
explanation
We cannot make another 1 so as to get 0 so smallest without any simulation is 1
example 3
5
30 27 26 10 6
output
0
30-26=4
10-6 =4
4-4 =0
我的想法:我只能想到显然会超时的蛮力解决方案。任何人都可以通过这种方法来帮助我吗?我认为这与动态规划有关
【问题讨论】:
-
时间限制是多少?
-
@Happypig375 O(N^2) 将是最优的
-
@Saqlain 考虑使用优先队列。
-
@vivek_23 我考虑过优先级队列并每次都在 2 个最大的元素上应用该操作,但在某些情况下(例如我给出的示例 3)会失败)
-
@vivek_23 我在这里找到了它:leetcode.com/discuss/interview-question/334981/…
标签: algorithm math dynamic-programming