Skip to content

Commit 89d08ee

Browse files
add 2012
1 parent 3fbd919 commit 89d08ee

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|2012|[Sum of Beauty in the Array](https://leetcode.com/problems/sum-of-beauty-in-the-array/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2012.java) ||Medium||
1112
|2011|[Final Value of Variable After Performing Operations](https://leetcode.com/problems/final-value-of-variable-after-performing-operations/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2011.java) ||Easy||
1213
|2007|[Find Original Array From Doubled Array](https://leetcode.com/problems/find-original-array-from-doubled-array/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2007.java) ||Medium||
1314
|2006|[Count Number of Pairs With Absolute Difference K](https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2006.java) ||Easy||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
public class _2012 {
6+
public static class Solution1 {
7+
public int sumOfBeauties(int[] nums) {
8+
int[] preMax = new int[nums.length];
9+
preMax[0] = nums[0];
10+
int max = nums[0];
11+
for (int i = 1; i < nums.length; i++) {
12+
max = Math.max(max, nums[i]);
13+
preMax[i] = max;
14+
}
15+
int[] postMin = new int[nums.length];
16+
Arrays.fill(postMin, Integer.MAX_VALUE);
17+
int min = nums[nums.length - 1];
18+
postMin[nums.length - 1] = nums[nums.length - 1];
19+
for (int i = nums.length - 2; i >= 0; i--) {
20+
min = Math.min(min, nums[i]);
21+
postMin[i] = min;
22+
}
23+
int sum = 0;
24+
for (int i = 1; i <= nums.length - 2; i++) {
25+
if (nums[i] > preMax[i - 1] && nums[i] < postMin[i + 1]) {
26+
sum += 2;
27+
} else if (nums[i] > nums[i - 1] && nums[i] < nums[i + 1]) {
28+
sum++;
29+
}
30+
}
31+
return sum;
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._2012;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _2012Test {
10+
private static _2012.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _2012.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.sumOfBeauties(new int[]{2, 4, 6, 4}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(14, solution1.sumOfBeauties(new int[]{1, 2, 3, 4, 5, 7, 8, 9, 10}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(0, solution1.sumOfBeauties(new int[]{9, 9, 3, 8, 7, 9, 6, 10}));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(0, solution1.sumOfBeauties(new int[]{8, 4, 6, 3, 10, 5, 8, 5, 5, 9}));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)