且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

转型的最低数量要求,以平衡阵列

更新时间:2023-11-10 15:50:16

您实际上并不需要显示的转换,却发现需要这种转换的总数量。

You do not actually need to show the transformations but find the total number of such transformations required.

递增所有而不是一个元件基本上相同降低一个元件(用于均衡所有元素的目的)。

Incrementing all but one element is essentially the same as decreasing one element(for the purpose of equalizing all elements).

策略:  降低所有非最小的元素,直到他们平等的最小元素。

strategy: decrease all non-minimal elements until they equal the minimal element.

有关,例如。如果元素是{X1,X2,X3,X4 ...... XN} 变换的数量将是

for eg. If the elements are {x1, x2, x3, x4...... xn} the number of transformations will be

let min = min{x1 .. xn}
for(int x : arr){
    // decrement x until x == m
}

变换总数

sum(k = 1 to n)x(k)−n*min{x1,…,xn}

采样运行:

For array = {1,2,3}
sum(k=1 to n) x(k) = (1 + 2 + 3) = 6
n = 3
min = 1
num_transformations = 6 - 3*1 = 3 transformations