mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
feat: add Minimum Average Difference (#1171)
* add leetcode Minimum Average Difference * updating DIRECTORY.md Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com>
This commit is contained in:
parent
bed955ed40
commit
b8a8807585
@ -217,6 +217,7 @@
|
|||||||
* [217](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/217.c)
|
* [217](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/217.c)
|
||||||
* [2222](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2222.c)
|
* [2222](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2222.c)
|
||||||
* [223](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/223.c)
|
* [223](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/223.c)
|
||||||
|
* [2256](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2256.c)
|
||||||
* [226](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/226.c)
|
* [226](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/226.c)
|
||||||
* [2270](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2270.c)
|
* [2270](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/2270.c)
|
||||||
* [230](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/230.c)
|
* [230](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/230.c)
|
||||||
@ -271,6 +272,7 @@
|
|||||||
* [771](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/771.c)
|
* [771](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/771.c)
|
||||||
* [79](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/79.c)
|
* [79](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/79.c)
|
||||||
* [8](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/8.c)
|
* [8](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/8.c)
|
||||||
|
* [807](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/807.c)
|
||||||
* [82](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/82.c)
|
* [82](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/82.c)
|
||||||
* [83](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/83.c)
|
* [83](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/83.c)
|
||||||
* [852](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/852.c)
|
* [852](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/852.c)
|
||||||
|
@ -117,5 +117,6 @@
|
|||||||
| 2024 | [Maximize the Confusion of an Exam](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/) | [C](./src/2024.c) | Medium |
|
| 2024 | [Maximize the Confusion of an Exam](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/) | [C](./src/2024.c) | Medium |
|
||||||
| 2130 | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [C](./src/2130.c) | Medium |
|
| 2130 | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [C](./src/2130.c) | Medium |
|
||||||
| 2222 | [Number of Ways to Select Buildings](https://leetcode.com/problems/number-of-ways-to-select-buildings/) | [C](./src/2222.c) | Medium |
|
| 2222 | [Number of Ways to Select Buildings](https://leetcode.com/problems/number-of-ways-to-select-buildings/) | [C](./src/2222.c) | Medium |
|
||||||
|
| 2256 | [Minimum Average Difference](https://leetcode.com/problems/minimum-average-difference/) | [C](./src/2256.c) | Medium |
|
||||||
| 2270 | [Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/) | [C](./src/2270.c) | Medium |
|
| 2270 | [Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/) | [C](./src/2270.c) | Medium |
|
||||||
| 2304 | [Minimum Path Cost in a Grid](https://leetcode.com/problems/minimum-path-cost-in-a-grid/) | [C](./src/2304.c) | Medium |
|
| 2304 | [Minimum Path Cost in a Grid](https://leetcode.com/problems/minimum-path-cost-in-a-grid/) | [C](./src/2304.c) | Medium |
|
||||||
|
37
leetcode/src/2256.c
Normal file
37
leetcode/src/2256.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// Prefix sum.
|
||||||
|
// - Calculate whole nums sum.
|
||||||
|
// - Calculate currIndex sum.
|
||||||
|
// - Compare averages
|
||||||
|
// Runtime: O(n)
|
||||||
|
// Space: O(1)
|
||||||
|
|
||||||
|
int minimumAverageDifference(int* nums, int numsSize){
|
||||||
|
long numsSum = 0;
|
||||||
|
for (int i = 0; i < numsSize; i++){
|
||||||
|
numsSum += nums[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
long currSum = 0;
|
||||||
|
long minAverage = 9223372036854775807; // Long max
|
||||||
|
int minIndex = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < numsSize; i++){
|
||||||
|
currSum += nums[i];
|
||||||
|
|
||||||
|
int leftItemsNumber = (numsSize - i - 1);
|
||||||
|
long leftItemsNumberAverage = 0;
|
||||||
|
if (leftItemsNumber != 0){
|
||||||
|
leftItemsNumberAverage = (numsSum - currSum) / leftItemsNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
long currItemsNumberAverage = currSum / (i + 1);
|
||||||
|
long averageDiff = abs(currItemsNumberAverage - leftItemsNumberAverage);
|
||||||
|
|
||||||
|
if (averageDiff < minAverage){
|
||||||
|
minAverage = averageDiff;
|
||||||
|
minIndex = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return minIndex;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user