Skip to content

Commit 9f01558

Browse files
authored
feat: add solutions to lc problem: No.2190 (doocs#3980)
No.2190.Most Frequent Number Following Key In an Array
1 parent 80367ae commit 9f01558

File tree

5 files changed

+113
-56
lines changed

5 files changed

+113
-56
lines changed

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

+39-21
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ target = 2 是紧跟着 key 之后出现次数最多的数字,所以我们返
6969

7070
### 方法一:遍历计数
7171

72-
我们用一个哈希表或数组 $cnt$ 记录每个 $target$ 出现的次数,用一个变量 $mx$ 维护 $target$ 出现的最大次数,初始时 $mx = 0$。
72+
我们用一个哈希表或数组 $\textit{cnt}$ 记录每个 $\textit{target}$ 出现的次数,用一个变量 $\textit{mx}$ 维护 $\textit{target}$ 出现的最大次数,初始时 $\textit{mx} = 0$。
7373

74-
遍历数组 $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]$。
74+
遍历数组 $\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]$。
7575

76-
遍历结束后,返回答案 $ans$。
76+
遍历结束后,返回答案 $\textit{ans}$。
7777

78-
时间复杂度 $O(n)$,空间复杂度 $O(M)$。其中 $n$ 和 $M$ 分别为数组 $nums$ 的长度和数组 $nums$ 中元素的最大值。
78+
时间复杂度 $O(n)$,空间复杂度 $O(M)$。其中 $n$ 和 $M$ 分别为数组 $\textit{nums}$ 的长度和数组 $\textit{nums}$ 中元素的最大值。
7979

8080
<!-- tabs:start -->
8181

@@ -159,9 +159,8 @@ func mostFrequent(nums []int, key int) (ans int) {
159159

160160
```ts
161161
function mostFrequent(nums: number[], key: number): number {
162-
const cnt: number[] = new Array(1001).fill(0);
163-
let ans = 0;
164-
let mx = 0;
162+
const cnt: number[] = Array(Math.max(...nums) + 1).fill(0);
163+
let [ans, mx] = [0, 0];
165164
for (let i = 0; i < nums.length - 1; ++i) {
166165
if (nums[i] === key) {
167166
if (mx < ++cnt[nums[i + 1]]) {
@@ -174,28 +173,47 @@ function mostFrequent(nums: number[], key: number): number {
174173
}
175174
```
176175

176+
#### JavaScript
177+
178+
```js
179+
/**
180+
* @param {number[]} nums
181+
* @param {number} key
182+
* @return {number}
183+
*/
184+
var mostFrequent = function (nums, key) {
185+
const cnt = Array(Math.max(...nums) + 1).fill(0);
186+
let [ans, mx] = [0, 0];
187+
for (let i = 0; i < nums.length - 1; ++i) {
188+
if (nums[i] === key) {
189+
if (mx < ++cnt[nums[i + 1]]) {
190+
mx = cnt[nums[i + 1]];
191+
ans = nums[i + 1];
192+
}
193+
}
194+
}
195+
return ans;
196+
};
197+
```
198+
177199
#### PHP
178200

179201
```php
180202
class Solution {
181-
/**
182-
* @param Integer[] $nums
183-
* @param Integer $key
184-
* @return Integer
185-
*/
186203
function mostFrequent($nums, $key) {
187-
$max = $maxNum = 0;
188-
for ($i = 0; $i < count($nums) - 1; $i++) {
189-
if ($nums[$i] == $key) {
190-
$hashtable[$nums[$i + 1]] += 1;
191-
$tmp = $hashtable[$nums[$i + 1]];
192-
if ($tmp > $max) {
193-
$max = $tmp;
194-
$maxNum = $nums[$i + 1];
204+
$cnt = array_fill(0, max($nums) + 1, 0);
205+
$ans = 0;
206+
$mx = 0;
207+
for ($i = 0; $i < count($nums) - 1; ++$i) {
208+
if ($nums[$i] === $key) {
209+
$cnt[$nums[$i + 1]]++;
210+
if ($mx < $cnt[$nums[$i + 1]]) {
211+
$mx = $cnt[$nums[$i + 1]];
212+
$ans = $nums[$i + 1];
195213
}
196214
}
197215
}
198-
return $maxNum;
216+
return $ans;
199217
}
200218
}
201219
```

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

+44-18
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ target = 2 has the maximum number of occurrences following an occurrence of key,
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Traversal and Counting
71+
72+
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$.
73+
74+
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]$.
75+
76+
After the traversal, return the answer $\textit{ans}$.
77+
78+
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.
7179

7280
<!-- tabs:start -->
7381

@@ -151,9 +159,8 @@ func mostFrequent(nums []int, key int) (ans int) {
151159

152160
```ts
153161
function mostFrequent(nums: number[], key: number): number {
154-
const cnt: number[] = new Array(1001).fill(0);
155-
let ans = 0;
156-
let mx = 0;
162+
const cnt: number[] = Array(Math.max(...nums) + 1).fill(0);
163+
let [ans, mx] = [0, 0];
157164
for (let i = 0; i < nums.length - 1; ++i) {
158165
if (nums[i] === key) {
159166
if (mx < ++cnt[nums[i + 1]]) {
@@ -166,28 +173,47 @@ function mostFrequent(nums: number[], key: number): number {
166173
}
167174
```
168175

176+
#### JavaScript
177+
178+
```js
179+
/**
180+
* @param {number[]} nums
181+
* @param {number} key
182+
* @return {number}
183+
*/
184+
var mostFrequent = function (nums, key) {
185+
const cnt = Array(Math.max(...nums) + 1).fill(0);
186+
let [ans, mx] = [0, 0];
187+
for (let i = 0; i < nums.length - 1; ++i) {
188+
if (nums[i] === key) {
189+
if (mx < ++cnt[nums[i + 1]]) {
190+
mx = cnt[nums[i + 1]];
191+
ans = nums[i + 1];
192+
}
193+
}
194+
}
195+
return ans;
196+
};
197+
```
198+
169199
#### PHP
170200

171201
```php
172202
class Solution {
173-
/**
174-
* @param Integer[] $nums
175-
* @param Integer $key
176-
* @return Integer
177-
*/
178203
function mostFrequent($nums, $key) {
179-
$max = $maxNum = 0;
180-
for ($i = 0; $i < count($nums) - 1; $i++) {
181-
if ($nums[$i] == $key) {
182-
$hashtable[$nums[$i + 1]] += 1;
183-
$tmp = $hashtable[$nums[$i + 1]];
184-
if ($tmp > $max) {
185-
$max = $tmp;
186-
$maxNum = $nums[$i + 1];
204+
$cnt = array_fill(0, max($nums) + 1, 0);
205+
$ans = 0;
206+
$mx = 0;
207+
for ($i = 0; $i < count($nums) - 1; ++$i) {
208+
if ($nums[$i] === $key) {
209+
$cnt[$nums[$i + 1]]++;
210+
if ($mx < $cnt[$nums[$i + 1]]) {
211+
$mx = $cnt[$nums[$i + 1]];
212+
$ans = $nums[$i + 1];
187213
}
188214
}
189215
}
190-
return $maxNum;
216+
return $ans;
191217
}
192218
}
193219
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} key
4+
* @return {number}
5+
*/
6+
var mostFrequent = function (nums, key) {
7+
const cnt = Array(Math.max(...nums) + 1).fill(0);
8+
let [ans, mx] = [0, 0];
9+
for (let i = 0; i < nums.length - 1; ++i) {
10+
if (nums[i] === key) {
11+
if (mx < ++cnt[nums[i + 1]]) {
12+
mx = cnt[nums[i + 1]];
13+
ans = nums[i + 1];
14+
}
15+
}
16+
}
17+
return ans;
18+
};
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
class Solution {
2-
/**
3-
* @param Integer[] $nums
4-
* @param Integer $key
5-
* @return Integer
6-
*/
72
function mostFrequent($nums, $key) {
8-
$max = $maxNum = 0;
9-
for ($i = 0; $i < count($nums) - 1; $i++) {
10-
if ($nums[$i] == $key) {
11-
$hashtable[$nums[$i + 1]] += 1;
12-
$tmp = $hashtable[$nums[$i + 1]];
13-
if ($tmp > $max) {
14-
$max = $tmp;
15-
$maxNum = $nums[$i + 1];
3+
$cnt = array_fill(0, max($nums) + 1, 0);
4+
$ans = 0;
5+
$mx = 0;
6+
for ($i = 0; $i < count($nums) - 1; ++$i) {
7+
if ($nums[$i] === $key) {
8+
$cnt[$nums[$i + 1]]++;
9+
if ($mx < $cnt[$nums[$i + 1]]) {
10+
$mx = $cnt[$nums[$i + 1]];
11+
$ans = $nums[$i + 1];
1612
}
1713
}
1814
}
19-
return $maxNum;
15+
return $ans;
2016
}
2117
}

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
function mostFrequent(nums: number[], key: number): number {
2-
const cnt: number[] = new Array(1001).fill(0);
3-
let ans = 0;
4-
let mx = 0;
2+
const cnt: number[] = Array(Math.max(...nums) + 1).fill(0);
3+
let [ans, mx] = [0, 0];
54
for (let i = 0; i < nums.length - 1; ++i) {
65
if (nums[i] === key) {
76
if (mx < ++cnt[nums[i + 1]]) {

0 commit comments

Comments
 (0)