Skip to content

Commit 655e0e6

Browse files
solves #2210: Count Hills and Valleys in an Array in java
1 parent a5916d1 commit 655e0e6

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@
720720
| 2194 | [Cells in a Range on an Excel Sheet](https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet) | [![Java](assets/java.png)](src/CellsInARangeOnAnExcelSheet.java) | |
721721
| 2200 | [Find All K-Distant Indices in an Array](https://leetcode.com/problems/find-all-k-distant-indices-in-an-array) | [![Java](assets/java.png)](src/FindAllKDistantIndicesInAnArray.java) | |
722722
| 2206 | [Divide Array Into Equal Pairs](https://leetcode.com/problems/divide-array-into-equal-pairs) | [![Java](assets/java.png)](src/DivideArrayIntoEqualPairs.java) | |
723-
| 2210 | [Count Hills and Valleys in an Array](https://leetcode.com/problems/count-hills-and-valleys-in-an-array) | | |
723+
| 2210 | [Count Hills and Valleys in an Array](https://leetcode.com/problems/count-hills-and-valleys-in-an-array) | [![Java](assets/java.png)](src/CountHillsAndValleysInAnArray.java) | |
724724
| 2215 | [Find the Difference of Two Arrays](https://leetcode.com/problems/find-the-difference-of-two-arrays) | | |
725725
| 2220 | [Minimum Bit Flips to Convert Number](https://leetcode.com/problems/minimum-bit-flips-to-convert-number) | | |
726726
| 2224 | [Minimum Number of Operations to Convert Time](https://leetcode.com/problems/minimum-number-of-operations-to-convert-time) | | |

Diff for: src/CountHillsAndValleysInAnArray.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// https://leetcode.com/problems/count-hills-and-valleys-in-an-array
2+
3+
public class CountHillsAndValleysInAnArray {
4+
public int countHillValley(int[] array) {
5+
int result = 0, left = array[0];
6+
for (int index = 1 ; index < array.length - 1; index++) {
7+
if ((left < array[index] && array[index] > array[index + 1]) || (left > array[index] && array[index] < array[index + 1])) {
8+
result++;
9+
left = array[index];
10+
}
11+
}
12+
return result;
13+
}
14+
}

0 commit comments

Comments
 (0)