Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e5d44fe

Browse files
add 2210
1 parent bb42edf commit e5d44fe

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
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+
| 2210 |[Count Hills and Valleys in an Array](https://leetcode.com/problems/count-hills-and-valleys-in-an-array/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2210.java) || Easy ||
1112
| 2201 |[Count Artifacts That Can Be Extracted](https://leetcode.com/problems/count-artifacts-that-can-be-extracted/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2201.java) || Medium ||
1213
| 2200 |[Find All K-Distant Indices in an Array](https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2200.java) || Easy ||
1314
| 2194 |[Cells in a Range on an Excel Sheet](https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2194.java) || Easy ||
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2210 {
4+
public static class Solution1 {
5+
public int countHillValley(int[] nums) {
6+
int ans = 0;
7+
for (int i = 1; i < nums.length - 1; i++) {
8+
if (nums[i] == nums[i - 1]) {
9+
continue;
10+
} else if (nums[i] > nums[i - 1]) {
11+
int tmp = i;
12+
boolean moved = false;
13+
while (tmp + 1 < nums.length && nums[tmp] == nums[tmp + 1]) {
14+
moved = true;
15+
tmp++;
16+
}
17+
if (moved) {
18+
if (tmp + 1 < nums.length && nums[i] > nums[tmp + 1]) {
19+
ans++;
20+
}
21+
} else {
22+
if (nums[i] > nums[i + 1]) {
23+
ans++;
24+
}
25+
}
26+
} else if (nums[i] < nums[i - 1]) {
27+
int tmp = i;
28+
boolean moved = false;
29+
while (tmp + 1 < nums.length && nums[tmp] == nums[tmp + 1]) {
30+
moved = true;
31+
tmp++;
32+
}
33+
if (moved) {
34+
if (tmp + 1 < nums.length && nums[i] < nums[tmp + 1]) {
35+
ans++;
36+
}
37+
} else {
38+
if (nums[i] < nums[i + 1]) {
39+
ans++;
40+
}
41+
}
42+
}
43+
}
44+
return ans;
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)