diff options
author | Jack Sangdahl <[email protected]> | 2025-07-26 23:16:47 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-07-26 23:17:00 -0600 |
commit | f031d94a96ae6ea157da22dac6b7f533b18f5f9c (patch) | |
tree | 977ffc31a23d6d035c5a26253108ee94e4735273 | |
parent | eaced75979196840bbb0138526d533515fa56720 (diff) | |
download | leetcode-f031d94a96ae6ea157da22dac6b7f533b18f5f9c.tar.gz leetcode-f031d94a96ae6ea157da22dac6b7f533b18f5f9c.zip |
2210. count hills and valleys in an array
-rw-r--r-- | count_hills_valleys_arr.c | 31 | ||||
-rw-r--r-- | count_hills_valleys_arr.py | 29 |
2 files changed, 60 insertions, 0 deletions
diff --git a/count_hills_valleys_arr.c b/count_hills_valleys_arr.c new file mode 100644 index 0000000..86e5f0a --- /dev/null +++ b/count_hills_valleys_arr.c @@ -0,0 +1,31 @@ +// 2210. Count Hills and Valleys in an Array +#include "leetcode.h" + +/* + * you are given a 0-indexed integer array 'nums'. an index 'i' is part of a + * hill in 'nums' if the closest non-equal neighbours of 'i' are smaller than + * 'nums[i]'. similarly, an index 'i' is part of a valley in 'nums' if the + * closest non-equal neighbours of 'i' are larger than 'nums[i]'. adjacent + * indices 'i' and 'j' are part of the same hill or valley if 'nums[i] == + * nums[j]'. note that for an index to be part of a hill or valley, it must have + * a non-equal neighbour on both the left and right of the index. return the + * number of hills and valleys in 'nums'. + */ + +int countHillValley(int *nums, int numsSize) { + int prev = 0, ans = 0; + for (int i = 1; i < numsSize - 1; ++i) { + if (nums[prev] == nums[i] || nums[i + 1] == nums[i]) + continue; + if ((nums[prev] - nums[i] & INT_MIN) == (nums[i + 1] - nums[i] & INT_MIN)) + ans++; + prev = i; + } + return ans; +} + +int main() { + int n1[] = {2, 4, 1, 1, 6, 5}, n2[] = {6, 6, 5, 5, 4, 1}; + printf("%d\n", countHillValley(n1, ARRAY_SIZE(n1))); // expect: 3 + printf("%d\n", countHillValley(n2, ARRAY_SIZE(n2))); // expect: 0 +} diff --git a/count_hills_valleys_arr.py b/count_hills_valleys_arr.py new file mode 100644 index 0000000..4c4c9e9 --- /dev/null +++ b/count_hills_valleys_arr.py @@ -0,0 +1,29 @@ +# 2210. Count Hills and Valleys in an Array +from itertools import groupby + +""" +you are given a 0-indexed integer array 'nums'. an index 'i' is part of a +hill in 'nums' if the closest non-equal neighbours of 'i' are smaller than +'nums[i]'. similarly, an index 'i' is part of a valley in 'nums' if the +closest non-equal neighbours of 'i' are larger than 'nums[i]'. adjacent +indices 'i' and 'j' are part of the same hill or valley if 'nums[i] == +nums[j]'. note that for an index to be part of a hill or valley, it must have +a non-equal neighbour on both the left and right of the index. return the +number of hills and valleys in 'nums'. +""" + + +class Solution(object): + def countHillValley(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + a = [i for i, _ in groupby(nums)] + return sum(l > m < r or l < m > r for l, m, r in zip(a, a[1:], a[2:])) + + +if __name__ == "__main__": + obj = Solution() + print(obj.countHillValley(nums=[2, 4, 1, 1, 6, 5])) + print(obj.countHillValley(nums=[6, 6, 5, 5, 4, 1])) |