정수들이 담긴 벡터를 받고,
벡터에서 특정 두 구간에서의 평균 차이가 가장 작을 때 인덱스를 구하는 문제
두 구간의 조건은 아래와 같다
The average difference of the index i is the absolute difference between the average of the first i + 1 elements of nums and the average of the last n - i - 1 elements. Both averages should be rounded down to the nearest integer.
class Solution {
public:
int minimumAverageDifference(vector<int>& nums) {
int numsSize = nums.size();
vector<long long int> sums(numsSize);
sums[0] = nums[0];
for (int i = 1; i < numsSize; i++)
{
sums[i] = sums[i - 1] + nums[i];
}
long long int min{numeric_limits<int>::max()};
int minIndex{0};
for (int i = 0; i < numsSize; i++)
{
int head{i + 1};
int tail{numsSize - head};
long long int headMean = sums[i];
long long int tailMean = sums[numsSize - 1] - sums[i];
int temp{};
if (tail == 0)
{
temp = abs(headMean / head);
}
else
{
temp = abs(floor(headMean / head) - floor(tailMean / tail));
}
if (temp < min)
{
min = temp;
minIndex = i;
}
}
return minIndex;
}
};