返回介绍

solution / 2200-2299 / 2210.Count Hills and Valleys in an Array / README_EN

发布于 2024-06-17 01:03:08 字数 6368 浏览 0 评论 0 收藏 0

2210. Count Hills and Valleys in an Array

中文文档

Description

You are given a 0-indexed integer array nums. An index i is part of a hill in nums if the closest non-equal neighbors of i are smaller than nums[i]. Similarly, an index i is part of a valley in nums if the closest non-equal neighbors 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 neighbor on both the left and right of the index.

Return the number of hills and valleys in nums.

 

Example 1:

Input: nums = [2,4,1,1,6,5]
Output: 3
Explanation:
At index 0: There is no non-equal neighbor of 2 on the left, so index 0 is neither a hill nor a valley.
At index 1: The closest non-equal neighbors of 4 are 2 and 1. Since 4 > 2 and 4 > 1, index 1 is a hill. 
At index 2: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 2 is a valley.
At index 3: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 3 is a valley, but note that it is part of the same valley as index 2.
At index 4: The closest non-equal neighbors of 6 are 1 and 5. Since 6 > 1 and 6 > 5, index 4 is a hill.
At index 5: There is no non-equal neighbor of 5 on the right, so index 5 is neither a hill nor a valley. 
There are 3 hills and valleys so we return 3.

Example 2:

Input: nums = [6,6,5,5,4,1]
Output: 0
Explanation:
At index 0: There is no non-equal neighbor of 6 on the left, so index 0 is neither a hill nor a valley.
At index 1: There is no non-equal neighbor of 6 on the left, so index 1 is neither a hill nor a valley.
At index 2: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 2 is neither a hill nor a valley.
At index 3: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 3 is neither a hill nor a valley.
At index 4: The closest non-equal neighbors of 4 are 5 and 1. Since 4 < 5 and 4 > 1, index 4 is neither a hill nor a valley.
At index 5: There is no non-equal neighbor of 1 on the right, so index 5 is neither a hill nor a valley.
There are 0 hills and valleys so we return 0.

 

Constraints:

  • 3 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Solutions

Solution 1

class Solution:
  def countHillValley(self, nums: List[int]) -> int:
    arr = [nums[0]]
    for v in nums[1:]:
      if v != arr[-1]:
        arr.append(v)
    return sum(
      (arr[i] < arr[i - 1]) == (arr[i] < arr[i + 1])
      for i in range(1, len(arr) - 1)
    )
class Solution {
  public int countHillValley(int[] nums) {
    int ans = 0;
    for (int i = 1, j = 0; i < nums.length - 1; ++i) {
      if (nums[i] == nums[i + 1]) {
        continue;
      }
      if (nums[i] > nums[j] && nums[i] > nums[i + 1]) {
        ++ans;
      }
      if (nums[i] < nums[j] && nums[i] < nums[i + 1]) {
        ++ans;
      }
      j = i;
    }
    return ans;
  }
}
class Solution {
public:
  int countHillValley(vector<int>& nums) {
    int ans = 0;
    for (int i = 1, j = 0; i < nums.size() - 1; ++i) {
      if (nums[i] == nums[i + 1]) continue;
      if (nums[i] > nums[j] && nums[i] > nums[i + 1]) ++ans;
      if (nums[i] < nums[j] && nums[i] < nums[i + 1]) ++ans;
      j = i;
    }
    return ans;
  }
};
func countHillValley(nums []int) int {
  ans := 0
  for i, j := 1, 0; i < len(nums)-1; i++ {
    if nums[i] == nums[i+1] {
      continue
    }
    if nums[i] > nums[j] && nums[i] > nums[i+1] {
      ans++
    }
    if nums[i] < nums[j] && nums[i] < nums[i+1] {
      ans++
    }
    j = i
  }
  return ans
}
function countHillValley(nums: number[]): number {
  const n = nums.length;
  let res = 0;
  let prev = nums[0];
  for (let i = 1; i < n - 1; i++) {
    const num = nums[i];
    const next = nums[i + 1];
    if (num == next) {
      continue;
    }
    if ((num > prev && num > next) || (num < prev && num < next)) {
      res += 1;
    }
    prev = num;
  }
  return res;
}
impl Solution {
  pub fn count_hill_valley(nums: Vec<i32>) -> i32 {
    let n = nums.len();
    let mut res = 0;
    let mut prev = nums[0];
    for i in 1..n - 1 {
      let num = nums[i];
      let next = nums[i + 1];
      if num == next {
        continue;
      }
      if (num > prev && num > next) || (num < prev && num < next) {
        res += 1;
      }
      prev = num;
    }
    res
  }
}

Solution 2

class Solution:
  def countHillValley(self, nums: List[int]) -> int:
    ans = j = 0
    for i in range(1, len(nums) - 1):
      if nums[i] == nums[i + 1]:
        continue
      if nums[i] > nums[j] and nums[i] > nums[i + 1]:
        ans += 1
      if nums[i] < nums[j] and nums[i] < nums[i + 1]:
        ans += 1
      j = i
    return ans

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文