-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm2210.java
More file actions
26 lines (24 loc) · 754 Bytes
/
Copy pathprblm2210.java
File metadata and controls
26 lines (24 loc) · 754 Bytes
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
public class prblm2210 {
public static void main(String[] args) {
int[] nums = {2,4,1,1,6,5};
System.out.println(new prblm2210().countHillValley(nums));
int[] nums2 = {6,6,5,5,4,1};
System.out.println(new prblm2210().countHillValley(nums2));
}
public int countHillValley(int[] nums) {
int ans = 0, j = 0;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1]) {
continue;
}
else if(nums[i] > nums[j] && nums[i] > nums[i + 1]){
ans++;
}
else if(nums[i] < nums[j] && nums[i] < nums[i + 1]){
ans++;
}
j = i;
}
return ans;
}
}