返回介绍

solution / 2100-2199 / 2190.Most Frequent Number Following Key In an Array / README_EN

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

2190. Most Frequent Number Following Key In an Array

中文文档

Description

You are given a 0-indexed integer array nums. You are also given an integer key, which is present in nums.

For every unique integer target in nums, count the number of times target immediately follows an occurrence of key in nums. In other words, count the number of indices i such that:

  • 0 <= i <= nums.length - 2,
  • nums[i] == key and,
  • nums[i + 1] == target.

Return _the _target_ with the maximum count_. The test cases will be generated such that the target with maximum count is unique.

 

Example 1:

Input: nums = [1,100,200,1,100], key = 1
Output: 100
Explanation: For target = 100, there are 2 occurrences at indices 1 and 4 which follow an occurrence of key.
No other integers follow an occurrence of key, so we return 100.

Example 2:

Input: nums = [2,2,2,2,3], key = 2
Output: 2
Explanation: For target = 2, there are 3 occurrences at indices 1, 2, and 3 which follow an occurrence of key.
For target = 3, there is only one occurrence at index 4 which follows an occurrence of key.
target = 2 has the maximum number of occurrences following an occurrence of key, so we return 2.

 

Constraints:

  • 2 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000
  • The test cases will be generated such that the answer is unique.

Solutions

Solution 1

class Solution:
  def mostFrequent(self, nums: List[int], key: int) -> int:
    cnt = Counter()
    ans = mx = 0
    for a, b in pairwise(nums):
      if a == key:
        cnt[b] += 1
        if mx < cnt[b]:
          mx = cnt[b]
          ans = b
    return ans
class Solution {
  public int mostFrequent(int[] nums, int key) {
    int[] cnt = new int[1001];
    int ans = 0, mx = 0;
    for (int i = 0; i < nums.length - 1; ++i) {
      if (nums[i] == key) {
        if (mx < ++cnt[nums[i + 1]]) {
          mx = cnt[nums[i + 1]];
          ans = nums[i + 1];
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int mostFrequent(vector<int>& nums, int key) {
    int cnt[1001]{};
    int ans = 0, mx = 0;
    for (int i = 0; i < nums.size() - 1; ++i) {
      if (nums[i] == key) {
        if (mx < ++cnt[nums[i + 1]]) {
          mx = cnt[nums[i + 1]];
          ans = nums[i + 1];
        }
      }
    }
    return ans;
  }
};
func mostFrequent(nums []int, key int) (ans int) {
  cnt := [1001]int{}
  mx := 0
  for i, x := range nums[1:] {
    if nums[i] == key {
      cnt[x]++
      if mx < cnt[x] {
        mx = cnt[x]
        ans = x
      }
    }
  }
  return
}
function mostFrequent(nums: number[], key: number): number {
  const cnt: number[] = new Array(1001).fill(0);
  let ans = 0;
  let mx = 0;
  for (let i = 0; i < nums.length - 1; ++i) {
    if (nums[i] === key) {
      if (mx < ++cnt[nums[i + 1]]) {
        mx = cnt[nums[i + 1]];
        ans = nums[i + 1];
      }
    }
  }
  return ans;
}
class Solution {
  /**
   * @param Integer[] $nums
   * @param Integer $key
   * @return Integer
   */
  function mostFrequent($nums, $key) {
    $max = $maxNum = 0;
    for ($i = 0; $i < count($nums) - 1; $i++) {
      if ($nums[$i] == $key) {
        $hashtable[$nums[$i + 1]] += 1;
        $tmp = $hashtable[$nums[$i + 1]];
        if ($tmp > $max) {
          $max = $tmp;
          $maxNum = $nums[$i + 1];
        }
      }
    }
    return $maxNum;
  }
}

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

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

发布评论

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