Skip to content

Commit 34bfb36

Browse files
committed
feat: add solutions to lc problem: No.2190
No.2190.Most Frequent Number Following Key In an Array
1 parent 1dcfbd5 commit 34bfb36

File tree

7 files changed

+127
-95
lines changed

7 files changed

+127
-95
lines changed

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

+49-32
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ target = 2 是紧跟着 key 之后出现次数最多的数字,所以我们返
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
**方法一:遍历计数**
55+
56+
我们用一个哈希表或数组 $cnt$ 记录每个 $target$ 出现的次数,用一个变量 $mx$ 维护 $target$ 出现的最大次数,初始时 $mx = 0$。
57+
58+
遍历数组 $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]$。
59+
60+
遍历结束后,返回答案 $ans$。
61+
62+
时间复杂度 $O(n)$,空间复杂度 $O(M)$。其中 $n$ 和 $M$ 分别为数组 $nums$ 的长度和数组 $nums$ 中元素的最大值。
63+
5464
<!-- tabs:start -->
5565

5666
### **Python3**
@@ -61,14 +71,13 @@ target = 2 是紧跟着 key 之后出现次数最多的数字,所以我们返
6171
class Solution:
6272
def mostFrequent(self, nums: List[int], key: int) -> int:
6373
cnt = Counter()
64-
mx = ans = 0
65-
for i, v in enumerate(nums[:-1]):
66-
if v == key:
67-
target = nums[i + 1]
68-
cnt[target] += 1
69-
if mx < cnt[target]:
70-
mx = cnt[target]
71-
ans = nums[i + 1]
74+
ans = mx = 0
75+
for a, b in pairwise(nums):
76+
if a == key:
77+
cnt[b] += 1
78+
if mx < cnt[b]:
79+
mx = cnt[b]
80+
ans = b
7281
return ans
7382
```
7483

@@ -79,14 +88,12 @@ class Solution:
7988
```java
8089
class Solution {
8190
public int mostFrequent(int[] nums, int key) {
82-
int[] cnt = new int[1010];
83-
int mx = 0, ans = 0;
91+
int[] cnt = new int[1001];
92+
int ans = 0, mx = 0;
8493
for (int i = 0; i < nums.length - 1; ++i) {
8594
if (nums[i] == key) {
86-
int target = nums[i + 1];
87-
++cnt[target];
88-
if (mx < cnt[target]) {
89-
mx = cnt[target];
95+
if (mx < ++cnt[nums[i + 1]]) {
96+
mx = cnt[nums[i + 1]];
9097
ans = nums[i + 1];
9198
}
9299
}
@@ -102,14 +109,12 @@ class Solution {
102109
class Solution {
103110
public:
104111
int mostFrequent(vector<int>& nums, int key) {
105-
vector<int> cnt(1010);
106-
int mx = 0, ans = 0;
112+
int cnt[1001]{};
113+
int ans = 0, mx = 0;
107114
for (int i = 0; i < nums.size() - 1; ++i) {
108115
if (nums[i] == key) {
109-
int target = nums[i + 1];
110-
++cnt[target];
111-
if (mx < cnt[target]) {
112-
mx = cnt[target];
116+
if (mx < ++cnt[nums[i + 1]]) {
117+
mx = cnt[nums[i + 1]];
113118
ans = nums[i + 1];
114119
}
115120
}
@@ -122,27 +127,39 @@ public:
122127
### **Go**
123128
124129
```go
125-
func mostFrequent(nums []int, key int) int {
126-
cnt := make([]int, 1010)
127-
mx, ans := 0, 0
128-
for i, v := range nums[:len(nums)-1] {
129-
if v == key {
130-
target := nums[i+1]
131-
cnt[target]++
132-
if mx < cnt[target] {
133-
mx = cnt[target]
134-
ans = nums[i+1]
130+
func mostFrequent(nums []int, key int) (ans int) {
131+
cnt := [1001]int{}
132+
mx := 0
133+
for i, x := range nums[1:] {
134+
if nums[i] == key {
135+
cnt[x]++
136+
if mx < cnt[x] {
137+
mx = cnt[x]
138+
ans = x
135139
}
136140
}
137141
}
138-
return ans
142+
return
139143
}
140144
```
141145

142146
### **TypeScript**
143147

144148
```ts
145-
149+
function mostFrequent(nums: number[], key: number): number {
150+
const cnt: number[] = new Array(1001).fill(0);
151+
let ans = 0;
152+
let mx = 0;
153+
for (let i = 0; i < nums.length - 1; ++i) {
154+
if (nums[i] === key) {
155+
if (mx < ++cnt[nums[i + 1]]) {
156+
mx = cnt[nums[i + 1]];
157+
ans = nums[i + 1];
158+
}
159+
}
160+
}
161+
return ans;
162+
}
146163
```
147164

148165
### **PHP**

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

+39-32
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,13 @@ target = 2 has the maximum number of occurrences following an occurrence of key,
5555
class Solution:
5656
def mostFrequent(self, nums: List[int], key: int) -> int:
5757
cnt = Counter()
58-
mx = ans = 0
59-
for i, v in enumerate(nums[:-1]):
60-
if v == key:
61-
target = nums[i + 1]
62-
cnt[target] += 1
63-
if mx < cnt[target]:
64-
mx = cnt[target]
65-
ans = nums[i + 1]
58+
ans = mx = 0
59+
for a, b in pairwise(nums):
60+
if a == key:
61+
cnt[b] += 1
62+
if mx < cnt[b]:
63+
mx = cnt[b]
64+
ans = b
6665
return ans
6766
```
6867

@@ -71,14 +70,12 @@ class Solution:
7170
```java
7271
class Solution {
7372
public int mostFrequent(int[] nums, int key) {
74-
int[] cnt = new int[1010];
75-
int mx = 0, ans = 0;
73+
int[] cnt = new int[1001];
74+
int ans = 0, mx = 0;
7675
for (int i = 0; i < nums.length - 1; ++i) {
7776
if (nums[i] == key) {
78-
int target = nums[i + 1];
79-
++cnt[target];
80-
if (mx < cnt[target]) {
81-
mx = cnt[target];
77+
if (mx < ++cnt[nums[i + 1]]) {
78+
mx = cnt[nums[i + 1]];
8279
ans = nums[i + 1];
8380
}
8481
}
@@ -94,14 +91,12 @@ class Solution {
9491
class Solution {
9592
public:
9693
int mostFrequent(vector<int>& nums, int key) {
97-
vector<int> cnt(1010);
98-
int mx = 0, ans = 0;
94+
int cnt[1001]{};
95+
int ans = 0, mx = 0;
9996
for (int i = 0; i < nums.size() - 1; ++i) {
10097
if (nums[i] == key) {
101-
int target = nums[i + 1];
102-
++cnt[target];
103-
if (mx < cnt[target]) {
104-
mx = cnt[target];
98+
if (mx < ++cnt[nums[i + 1]]) {
99+
mx = cnt[nums[i + 1]];
105100
ans = nums[i + 1];
106101
}
107102
}
@@ -114,27 +109,39 @@ public:
114109
### **Go**
115110
116111
```go
117-
func mostFrequent(nums []int, key int) int {
118-
cnt := make([]int, 1010)
119-
mx, ans := 0, 0
120-
for i, v := range nums[:len(nums)-1] {
121-
if v == key {
122-
target := nums[i+1]
123-
cnt[target]++
124-
if mx < cnt[target] {
125-
mx = cnt[target]
126-
ans = nums[i+1]
112+
func mostFrequent(nums []int, key int) (ans int) {
113+
cnt := [1001]int{}
114+
mx := 0
115+
for i, x := range nums[1:] {
116+
if nums[i] == key {
117+
cnt[x]++
118+
if mx < cnt[x] {
119+
mx = cnt[x]
120+
ans = x
127121
}
128122
}
129123
}
130-
return ans
124+
return
131125
}
132126
```
133127

134128
### **TypeScript**
135129

136130
```ts
137-
131+
function mostFrequent(nums: number[], key: number): number {
132+
const cnt: number[] = new Array(1001).fill(0);
133+
let ans = 0;
134+
let mx = 0;
135+
for (let i = 0; i < nums.length - 1; ++i) {
136+
if (nums[i] === key) {
137+
if (mx < ++cnt[nums[i + 1]]) {
138+
mx = cnt[nums[i + 1]];
139+
ans = nums[i + 1];
140+
}
141+
}
142+
}
143+
return ans;
144+
}
138145
```
139146

140147
### **PHP**

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
class Solution {
22
public:
33
int mostFrequent(vector<int>& nums, int key) {
4-
vector<int> cnt(1010);
5-
int mx = 0, ans = 0;
4+
int cnt[1001]{};
5+
int ans = 0, mx = 0;
66
for (int i = 0; i < nums.size() - 1; ++i) {
77
if (nums[i] == key) {
8-
int target = nums[i + 1];
9-
++cnt[target];
10-
if (mx < cnt[target]) {
11-
mx = cnt[target];
8+
if (mx < ++cnt[nums[i + 1]]) {
9+
mx = cnt[nums[i + 1]];
1210
ans = nums[i + 1];
1311
}
1412
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
func mostFrequent(nums []int, key int) int {
2-
cnt := make([]int, 1010)
3-
mx, ans := 0, 0
4-
for i, v := range nums[:len(nums)-1] {
5-
if v == key {
6-
target := nums[i+1]
7-
cnt[target]++
8-
if mx < cnt[target] {
9-
mx = cnt[target]
10-
ans = nums[i+1]
1+
func mostFrequent(nums []int, key int) (ans int) {
2+
cnt := [1001]int{}
3+
mx := 0
4+
for i, x := range nums[1:] {
5+
if nums[i] == key {
6+
cnt[x]++
7+
if mx < cnt[x] {
8+
mx = cnt[x]
9+
ans = x
1110
}
1211
}
1312
}
14-
return ans
13+
return
1514
}

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
class Solution {
22
public int mostFrequent(int[] nums, int key) {
3-
int[] cnt = new int[1010];
4-
int mx = 0, ans = 0;
3+
int[] cnt = new int[1001];
4+
int ans = 0, mx = 0;
55
for (int i = 0; i < nums.length - 1; ++i) {
66
if (nums[i] == key) {
7-
int target = nums[i + 1];
8-
++cnt[target];
9-
if (mx < cnt[target]) {
10-
mx = cnt[target];
7+
if (mx < ++cnt[nums[i + 1]]) {
8+
mx = cnt[nums[i + 1]];
119
ans = nums[i + 1];
1210
}
1311
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
class Solution:
22
def mostFrequent(self, nums: List[int], key: int) -> int:
33
cnt = Counter()
4-
mx = ans = 0
5-
for i, v in enumerate(nums[:-1]):
6-
if v == key:
7-
target = nums[i + 1]
8-
cnt[target] += 1
9-
if mx < cnt[target]:
10-
mx = cnt[target]
11-
ans = nums[i + 1]
4+
ans = mx = 0
5+
for a, b in pairwise(nums):
6+
if a == key:
7+
cnt[b] += 1
8+
if mx < cnt[b]:
9+
mx = cnt[b]
10+
ans = b
1211
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function mostFrequent(nums: number[], key: number): number {
2+
const cnt: number[] = new Array(1001).fill(0);
3+
let ans = 0;
4+
let mx = 0;
5+
for (let i = 0; i < nums.length - 1; ++i) {
6+
if (nums[i] === key) {
7+
if (mx < ++cnt[nums[i + 1]]) {
8+
mx = cnt[nums[i + 1]];
9+
ans = nums[i + 1];
10+
}
11+
}
12+
}
13+
return ans;
14+
}

0 commit comments

Comments
 (0)