diff --git a/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/README.md b/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/README.md index 6f0ac6f8c4f56..f41f6665bc770 100644 --- a/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/README.md +++ b/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/README.md @@ -69,13 +69,13 @@ target = 2 是紧跟着 key 之后出现次数最多的数字,所以我们返 ### 方法一:遍历计数 -我们用一个哈希表或数组 $cnt$ 记录每个 $target$ 出现的次数,用一个变量 $mx$ 维护 $target$ 出现的最大次数,初始时 $mx = 0$。 +我们用一个哈希表或数组 $\textit{cnt}$ 记录每个 $\textit{target}$ 出现的次数,用一个变量 $\textit{mx}$ 维护 $\textit{target}$ 出现的最大次数,初始时 $\textit{mx} = 0$。 -遍历数组 $nums$,如果 $nums[i] = key$,则 $nums[i + 1]$ 出现的次数 $cnt[nums[i + 1]]$ 加一,如果此时 $mx \lt cnt[nums[i + 1]]$,则更新 $mx = cnt[nums[i + 1]]$,并更新答案 $ans = nums[i + 1]$。 +遍历数组 $\textit{nums}$,如果 $\textit{nums}[i] = \textit{key}$,则 $\textit{nums}[i + 1]$ 出现的次数 $\textit{cnt}[\textit{nums}[i + 1]]$ 加一,如果此时 $\textit{mx} \lt \textit{cnt}[\textit{nums}[i + 1]]$,则更新 $\textit{mx} = \textit{cnt}[\textit{nums}[i + 1]]$,并更新答案 $\textit{ans} = \textit{nums}[i + 1]$。 -遍历结束后,返回答案 $ans$。 +遍历结束后,返回答案 $\textit{ans}$。 -时间复杂度 $O(n)$,空间复杂度 $O(M)$。其中 $n$ 和 $M$ 分别为数组 $nums$ 的长度和数组 $nums$ 中元素的最大值。 +时间复杂度 $O(n)$,空间复杂度 $O(M)$。其中 $n$ 和 $M$ 分别为数组 $\textit{nums}$ 的长度和数组 $\textit{nums}$ 中元素的最大值。 @@ -159,9 +159,8 @@ func mostFrequent(nums []int, key int) (ans int) { ```ts function mostFrequent(nums: number[], key: number): number { - const cnt: number[] = new Array(1001).fill(0); - let ans = 0; - let mx = 0; + const cnt: number[] = Array(Math.max(...nums) + 1).fill(0); + let [ans, mx] = [0, 0]; for (let i = 0; i < nums.length - 1; ++i) { if (nums[i] === key) { if (mx < ++cnt[nums[i + 1]]) { @@ -174,28 +173,47 @@ function mostFrequent(nums: number[], key: number): number { } ``` +#### JavaScript + +```js +/** + * @param {number[]} nums + * @param {number} key + * @return {number} + */ +var mostFrequent = function (nums, key) { + const cnt = Array(Math.max(...nums) + 1).fill(0); + let [ans, mx] = [0, 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; +}; +``` + #### PHP ```php 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]; + $cnt = array_fill(0, max($nums) + 1, 0); + $ans = 0; + $mx = 0; + for ($i = 0; $i < count($nums) - 1; ++$i) { + if ($nums[$i] === $key) { + $cnt[$nums[$i + 1]]++; + if ($mx < $cnt[$nums[$i + 1]]) { + $mx = $cnt[$nums[$i + 1]]; + $ans = $nums[$i + 1]; } } } - return $maxNum; + return $ans; } } ``` diff --git a/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/README_EN.md b/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/README_EN.md index 0e0df1f0b9a64..fefa829cc7dfe 100644 --- a/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/README_EN.md +++ b/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/README_EN.md @@ -67,7 +67,15 @@ target = 2 has the maximum number of occurrences following an occurrence of key, -### Solution 1 +### Solution 1: Traversal and Counting + +We use a hash table or an array $\textit{cnt}$ to record the number of occurrences of each $\textit{target}$, and use a variable $\textit{mx}$ to maintain the maximum number of occurrences of $\textit{target}$. Initially, $\textit{mx} = 0$. + +Traverse the array $\textit{nums}$. If $\textit{nums}[i] = \textit{key}$, increment the count of $\textit{nums}[i + 1]$ in $\textit{cnt}[\textit{nums}[i + 1]]$. If $\textit{mx} \lt \textit{cnt}[\textit{nums}[i + 1]]$, update $\textit{mx} = \textit{cnt}[\textit{nums}[i + 1]]$ and update the answer $\textit{ans} = \textit{nums}[i + 1]$. + +After the traversal, return the answer $\textit{ans}$. + +The time complexity is $O(n)$, and the space complexity is $O(M)$. Here, $n$ and $M$ are the length of the array $\textit{nums}$ and the maximum value of the elements in the array $\textit{nums}$, respectively. @@ -151,9 +159,8 @@ func mostFrequent(nums []int, key int) (ans int) { ```ts function mostFrequent(nums: number[], key: number): number { - const cnt: number[] = new Array(1001).fill(0); - let ans = 0; - let mx = 0; + const cnt: number[] = Array(Math.max(...nums) + 1).fill(0); + let [ans, mx] = [0, 0]; for (let i = 0; i < nums.length - 1; ++i) { if (nums[i] === key) { if (mx < ++cnt[nums[i + 1]]) { @@ -166,28 +173,47 @@ function mostFrequent(nums: number[], key: number): number { } ``` +#### JavaScript + +```js +/** + * @param {number[]} nums + * @param {number} key + * @return {number} + */ +var mostFrequent = function (nums, key) { + const cnt = Array(Math.max(...nums) + 1).fill(0); + let [ans, mx] = [0, 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; +}; +``` + #### PHP ```php 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]; + $cnt = array_fill(0, max($nums) + 1, 0); + $ans = 0; + $mx = 0; + for ($i = 0; $i < count($nums) - 1; ++$i) { + if ($nums[$i] === $key) { + $cnt[$nums[$i + 1]]++; + if ($mx < $cnt[$nums[$i + 1]]) { + $mx = $cnt[$nums[$i + 1]]; + $ans = $nums[$i + 1]; } } } - return $maxNum; + return $ans; } } ``` diff --git a/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.js b/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.js new file mode 100644 index 0000000000000..8b89136423407 --- /dev/null +++ b/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.js @@ -0,0 +1,18 @@ +/** + * @param {number[]} nums + * @param {number} key + * @return {number} + */ +var mostFrequent = function (nums, key) { + const cnt = Array(Math.max(...nums) + 1).fill(0); + let [ans, mx] = [0, 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; +}; diff --git a/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.php b/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.php index 7cf8538ad0d3d..bbd14e9674fef 100644 --- a/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.php +++ b/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.php @@ -1,21 +1,17 @@ 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]; + $cnt = array_fill(0, max($nums) + 1, 0); + $ans = 0; + $mx = 0; + for ($i = 0; $i < count($nums) - 1; ++$i) { + if ($nums[$i] === $key) { + $cnt[$nums[$i + 1]]++; + if ($mx < $cnt[$nums[$i + 1]]) { + $mx = $cnt[$nums[$i + 1]]; + $ans = $nums[$i + 1]; } } } - return $maxNum; + return $ans; } } diff --git a/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.ts b/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.ts index 7e72352bd81d5..540748018e01a 100644 --- a/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.ts +++ b/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.ts @@ -1,7 +1,6 @@ function mostFrequent(nums: number[], key: number): number { - const cnt: number[] = new Array(1001).fill(0); - let ans = 0; - let mx = 0; + const cnt: number[] = Array(Math.max(...nums) + 1).fill(0); + let [ans, mx] = [0, 0]; for (let i = 0; i < nums.length - 1; ++i) { if (nums[i] === key) { if (mx < ++cnt[nums[i + 1]]) {