forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSum of Beauty in the Array.java
38 lines (33 loc) · 1.02 KB
/
Sum of Beauty in the Array.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Runtime: 9 ms (Top 73.89%) | Memory: 94 MB (Top 67.52%)
class Solution {
public int sumOfBeauties(int[] nums) {
boolean[] left = new boolean[nums.length];
boolean[] right = new boolean[nums.length];
left[0] = true;
int leftMax = nums[0];
for(int i = 1; i < nums.length; i++) {
if(nums[i] > leftMax) {
left[i] = true;
leftMax = nums[i];
}
}
right[nums.length-1] = true;
int rightMin = nums[nums.length-1];
for(int i = nums.length-2; i >= 0; i--) {
if(nums[i] < rightMin) {
right[i] = true;
rightMin = nums[i];
}
}
int beautyCount = 0;
for(int i = 1; i < nums.length-1; i++) {
if(left[i] && right[i]) {
beautyCount += 2;
}
else if(nums[i-1] < nums[i] && nums[i] < nums[i+1]) {
beautyCount += 1;
}
}
return beautyCount;
}
}