Skip to content

Commit 3ed6851

Browse files
add 2256
1 parent d31c2b4 commit 3ed6851

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|------|----------------|------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|-------------|-------------
11-
| 2255 |[Count Prefixes of a Given String](https://leetcode.com/problems/count-prefixes-of-a-given-string/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2255.java) || Easy ||
11+
| 2256 |[Minimum Average Difference](https://leetcode.com/problems/minimum-average-difference/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2256.java) || Medium ||
12+
| 2255 |[Count Prefixes of a Given String](https://leetcode.com/problems/count-prefixes-of-a-given-string/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2255.java) || Easy ||
1213
| 2248 |[Intersection of Multiple Arrays](https://leetcode.com/problems/intersection-of-multiple-arrays/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2248.java) || Easy ||
1314
| 2244 |[Minimum Rounds to Complete All Tasks](https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2244.java) || Medium ||
1415
| 2243 |[Calculate Digit Sum of a String](https://leetcode.com/problems/calculate-digit-sum-of-a-string/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2243.java) || Easy ||
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2256 {
4+
public static class Solution1 {
5+
public int minimumAverageDifference(int[] nums) {
6+
if (nums.length == 1) {
7+
return 0;
8+
}
9+
long secondHalfSum = 0;
10+
int minAveDiff = Integer.MAX_VALUE;
11+
for (int i = 1; i < nums.length; i++) {
12+
secondHalfSum += nums[i];
13+
}
14+
long firstHalfSum = nums[0];
15+
int count = 1;
16+
int minDiffIndex = 0;
17+
for (int i = 0; i < nums.length; ) {
18+
int firstHalfAve = (int) (firstHalfSum / count);
19+
int secondHalfAve = 0;
20+
if ((nums.length - count) != 0) {
21+
secondHalfAve = (int) (secondHalfSum / (nums.length - count));
22+
}
23+
if (minAveDiff > Math.abs(firstHalfAve - secondHalfAve)) {
24+
minAveDiff = Math.abs(firstHalfAve - secondHalfAve);
25+
minDiffIndex = i;
26+
if (minAveDiff == 0) {
27+
return minDiffIndex;
28+
}
29+
}
30+
count++;
31+
i++;
32+
if (i < nums.length) {
33+
firstHalfSum += nums[i];
34+
secondHalfSum -= nums[i];
35+
}
36+
}
37+
return minDiffIndex;
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy