比较直观好理解的解法:
class Solution {
public:
int minOperations(int n) {
int res = 0;
for(int i = 1;i <= n;i++)
{
int num = 2*(i-1) + 1;
if(num > n)
res += num -n;
}
return res;
}
};
运行效率:
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Minimum Operations to Make Array Equal.
Memory Usage: 5.9 MB, less than 22.53% of C++ online submissions for Minimum Operations to Make Array Equal.
数列的平均值是n,如果当前数大于n,需要减去n即是需要操作的次数。