Skip to content

feat: add solutions to lc problem: No.2190 #3980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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}$ 中元素的最大值。

<!-- tabs:start -->

Expand Down Expand Up @@ -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]]) {
Expand All @@ -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;
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ target = 2 has the maximum number of occurrences following an occurrence of key,

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand Down Expand Up @@ -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]]) {
Expand All @@ -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;
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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]]) {
Expand Down
Loading