简体   繁体   English

所有数组元素除以每个数组元素时的商数之和

[英]Sum of quotients when all array elements is divided by each array elements

Came across a question in the coding test, suppose we have array, arr = [4,3,8] . 在编码测试中遇到一个问题,假设我们有数组, arr = [4,3,8] Dividing array by 4 gives 1(4/4) + 0(3/4) + 2(8/4) = 3. Similarly by 3 gives 4 and by 8 gives 1. So the output is 3+4+1 = 8. O(n2) solution gave TLE, so I tried to do better. 将数组除以4得到1(4/4)+ 0(3/4)+ 2(8/4)=3。类似地,将3除以4得到4,而将8除以1。因此输出为3 + 4 + 1 = 8 O(n2)解决方案赋予了TLE,所以我试图做得更好。

I thought of sorting and using lower bound. 我想到了排序和使用下限。 Lower Bound of arr[i]*k , for k = 1,2,3... , till arr[i]*k<=max(arr) , will give me number of elements greater than the multiple taken, this will add in the final result. arr[i]*k下界,对于k = 1,2,3... ,直到arr[i]*k<=max(arr) ,将使我的元素数量大于所取的倍数,这将使添加最终结果。 But this gave me wrong answer. 但这给了我错误的答案。 How can I solve this problem efficiently, any suggestions are welcome. 如何有效解决此问题,欢迎提出任何建议。

If I understand your question correctly, you want (in this example) three arrays which are the orignal array divided (with whole number division) by each of its three elements and then sum over them. 如果我正确理解了您的问题,则(在此示例中)您需要三个数组,它们是原始数组除以三个元素中的每个元素(用整数除法),然后求和。

arr = [4,3,8]
sum([el//i for i in arr for el in arr])

will give you the wanted result. 会给你想要的结果。 For understanding, the resulting list is: 为了理解,结果列表为:

In [8]: [el//i for i in arr for el in arr]
Out[8]: [1, 0, 2, 1, 1, 2, 0, 0, 1]

Summing over this gives the result 8 . 总结得出结果8 If you really need the three arrays seperate and not concatenated in this way, I will edit this answer. 如果您确实需要以这种方式分开而不是将三个数组分开,那么我将编辑此答案。

EDIT So I misunderstood the question, sorry. 编辑所以我误解了这个问题,对不起。 If you are allowed to use numpy, I would propose this algorithm, which (I think) still is O(n²), but overall much faster: 如果允许使用numpy,我建议使用此算法,该算法(我认为)仍为O(n²),但总体上要快得多:

arr = np.random.randint(1,10,n)
np.sum(np.tile(arr, n)//np.repeat(arr,n))

Maybe I can come up with something more intelligent tomorrow 也许我明天可以想出一些更聪明的东西

EDIT 2 Sorting actually is a good idea and speeds up the algorithm a bit. 编辑2排序实际上是一个好主意,可以稍微加快算法的速度。 I tested the three solutions so far with this litte script (I know, that time.time is not good for exact times, but it just shows the general speed of each algorithm): 到目前为止,我已经使用此脚本测试了这三种解决方案(我知道, time.time不适用于精确时间,但是它仅显示了每种算法的总体速度):

for n in range(100, 500):
    arr = np.random.randint(1,10,n)
    #sorted
    t = time.time()
    arr2 = sorted(arr)
    sum([el1//el2 for i, el2 in enumerate(arr2) for el1 in arr2[i:]])
    t2 = time.time()-t
    times_sorted.append(t2)

    #standard
    t = time.time()
    arr = sorted(arr)
    sum([el1//el2 for el2 in arr for el1 in arr])
    t2 = time.time()-t
    times_standard.append(t2)

    #numpy
    t = time.time()
    arr = sorted(arr)
    np.sum(np.tile(arr, n)//np.repeat(arr, n))
    t2 = time.time()-t
    times_numpy.append(t2)
    if not n%50:
        print(n)

Plotting with 用绘图

plt.figure()
plt.plot(times_numpy)
plt.plot(times_sorted)
plt.plot(times_standard)
plt.legend(["numpy", "sorted", "standard"])
plt.xlabel("n")
plt.ylabel("time in s")

Gives: 得到:

对照

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM