Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 312ee51

Browse files
feat: add solutions to lc problem: No.3400 (#3890)
No.3400.Maximum Number of Matching Indices After Right Shifts
1 parent 2d18cf7 commit 312ee51

File tree

12 files changed

+417
-5
lines changed

12 files changed

+417
-5
lines changed

‎solution/1500-1599/1565.Unique Orders and Customers Per Month/README_EN.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ This table contains information about the orders made by customer_id.
4343
<p><strong class="example">Example 1:</strong></p>
4444

4545
<pre>
46-
<strong>Input:</strong>
46+
<strong>Input:</strong>
4747
Orders table:
4848
+----------+------------+-------------+------------+
4949
| order_id | order_date | customer_id | invoice |
@@ -59,7 +59,7 @@ Orders table:
5959
| 9 | 2021年01月07日 | 3 | 31 |
6060
| 10 | 2021年01月15日 | 2 | 20 |
6161
+----------+------------+-------------+------------+
62-
<strong>Output:</strong>
62+
<strong>Output:</strong>
6363
+---------+-------------+----------------+
6464
| month | order_count | customer_count |
6565
+---------+-------------+----------------+
@@ -68,7 +68,7 @@ Orders table:
6868
| 2020-12 | 2 | 1 |
6969
| 2021-01 | 1 | 1 |
7070
+---------+-------------+----------------+
71-
<strong>Explanation:</strong>
71+
<strong>Explanation:</strong>
7272
In September 2020 we have two orders from 2 different customers with invoices &gt; 20ドル.
7373
In October 2020 we have two orders from 1 customer, and only one of the two orders has invoice &gt; 20ドル.
7474
In November 2020 we have two orders from 2 different customers but invoices &lt; 20,ドル so we don&#39;t include that month.

‎solution/1500-1599/1577.Number of Ways Where Square of Number Is Equal to Product of Two Numbers/README_EN.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ tags:
3434
<pre>
3535
<strong>Input:</strong> nums1 = [7,4], nums2 = [5,2,8,9]
3636
<strong>Output:</strong> 1
37-
<strong>Explanation:</strong> Type 1: (1, 1, 2), nums1[1]<sup>2</sup> = nums2[1] * nums2[2]. (4<sup>2</sup> = 2 * 8).
37+
<strong>Explanation:</strong> Type 1: (1, 1, 2), nums1[1]<sup>2</sup> = nums2[1] * nums2[2]. (4<sup>2</sup> = 2 * 8).
3838
</pre>
3939

4040
<p><strong class="example">Example 2:</strong></p>

‎solution/1500-1599/1589.Maximum Sum Obtained of Any Permutation/README_EN.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ tags:
3333
<pre>
3434
<strong>Input:</strong> nums = [1,2,3,4,5], requests = [[1,3],[0,1]]
3535
<strong>Output:</strong> 19
36-
<strong>Explanation:</strong> One permutation of nums is [2,1,3,4,5] with the following result:
36+
<strong>Explanation:</strong> One permutation of nums is [2,1,3,4,5] with the following result:
3737
requests[0] -&gt; nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8
3838
requests[1] -&gt; nums[0] + nums[1] = 2 + 1 = 3
3939
Total sum: 8 + 3 = 11.
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3400.Maximum%20Number%20of%20Matching%20Indices%20After%20Right%20Shifts/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3400. 右移后的最大匹配索引数 🔒](https://leetcode.cn/problems/maximum-number-of-matching-indices-after-right-shifts)
10+
11+
[English Version](/solution/3400-3499/3400.Maximum%20Number%20of%20Matching%20Indices%20After%20Right%20Shifts/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给定两个长度相同的整数数组&nbsp;<code>nums1</code> 和&nbsp;<code>nums2</code>。</p>
18+
19+
<p>如果&nbsp;<code>nums1[i] == nums2[i]</code>&nbsp;则认为下标&nbsp;<code>i</code> 是 <strong>匹配</strong> 的。</p>
20+
21+
<p>返回在&nbsp;<code>nums1</code>&nbsp;上进行任意次数 <strong>右移</strong>&nbsp;后 <strong>最大</strong>&nbsp;的 <strong>匹配&nbsp;</strong>下标数量。</p>
22+
23+
<p><strong>右移&nbsp;</strong>是对于所有下标,将位于下标&nbsp;<code>i</code>&nbsp;的元素移动到&nbsp;<code>(i + 1) % n</code>。</p>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong class="example">示例 1:</strong></p>
28+
29+
<div class="example-block">
30+
<p><strong>输入:</strong><span class="example-io">nums1 = [3,1,2,3,1,2], nums2 = [1,2,3,1,2,3]</span></p>
31+
32+
<p><span class="example-io"><b>输出:</b>6</span></p>
33+
34+
<p><strong>解释:</strong></p>
35+
36+
<p>如果我们右移&nbsp;<code>nums1</code> 2 次,它变为&nbsp;<code>[1, 2, 3, 1, 2, 3]</code>。每个下标都匹配,所以输出为 6。</p>
37+
</div>
38+
39+
<p><strong class="example">示例 2:</strong></p>
40+
41+
<div class="example-block">
42+
<p><span class="example-io"><b>输入:</b>nums1 = [1,4,2,5,3,1], nums2 = [2,3,1,2,4,6]</span></p>
43+
44+
<p><span class="example-io"><b>输出:</b>3</span></p>
45+
46+
<p><strong>解释:</strong></p>
47+
48+
<p>如果我们右移&nbsp;<code>nums1</code> 3 次,它变为&nbsp;<code>[5, 3, 1, 1, 4, 2]</code>。下标 1,2,4 匹配,所以输出为 3。</p>
49+
</div>
50+
51+
<p>&nbsp;</p>
52+
53+
<p><strong>提示:</strong></p>
54+
55+
<ul>
56+
<li><code>nums1.length == nums2.length</code></li>
57+
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 3000</code></li>
58+
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>9</sup></code></li>
59+
</ul>
60+
61+
<!-- description:end -->
62+
63+
## 解法
64+
65+
<!-- solution:start -->
66+
67+
### 方法一:枚举
68+
69+
我们可以枚举右移的次数 $k,ドル其中 0ドル \leq k \lt n$。对于每一个 $k,ドル我们可以计算出右移 $k$ 次后的数组 $\textit{nums1}$ 和 $\textit{nums2}$ 的匹配下标数量,取最大值作为答案即可。
70+
71+
时间复杂度 $O(n^2),ドル其中 $n$ 为数组 $\textit{nums1}$ 的长度。空间复杂度 $O(1)$。
72+
73+
<!-- tabs:start -->
74+
75+
#### Python3
76+
77+
```python
78+
class Solution:
79+
def maximumMatchingIndices(self, nums1: List[int], nums2: List[int]) -> int:
80+
n = len(nums1)
81+
ans = 0
82+
for k in range(n):
83+
t = sum(nums1[(i + k) % n] == x for i, x in enumerate(nums2))
84+
ans = max(ans, t)
85+
return ans
86+
```
87+
88+
#### Java
89+
90+
```java
91+
class Solution {
92+
public int maximumMatchingIndices(int[] nums1, int[] nums2) {
93+
int n = nums1.length;
94+
int ans = 0;
95+
for (int k = 0; k < n; ++k) {
96+
int t = 0;
97+
for (int i = 0; i < n; ++i) {
98+
if (nums1[(i + k) % n] == nums2[i]) {
99+
++t;
100+
}
101+
}
102+
ans = Math.max(ans, t);
103+
}
104+
return ans;
105+
}
106+
}
107+
```
108+
109+
#### C++
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
int maximumMatchingIndices(vector<int>& nums1, vector<int>& nums2) {
115+
int n = nums1.size();
116+
int ans = 0;
117+
for (int k = 0; k < n; ++k) {
118+
int t = 0;
119+
for (int i = 0; i < n; ++i) {
120+
if (nums1[(i + k) % n] == nums2[i]) {
121+
++t;
122+
}
123+
}
124+
ans = max(ans, t);
125+
}
126+
return ans;
127+
}
128+
};
129+
```
130+
131+
#### Go
132+
133+
```go
134+
func maximumMatchingIndices(nums1 []int, nums2 []int) (ans int) {
135+
n := len(nums1)
136+
for k := range nums1 {
137+
t := 0
138+
for i, x := range nums2 {
139+
if nums1[(i+k)%n] == x {
140+
t++
141+
}
142+
}
143+
ans = max(ans, t)
144+
}
145+
return
146+
}
147+
```
148+
149+
#### TypeScript
150+
151+
```ts
152+
function maximumMatchingIndices(nums1: number[], nums2: number[]): number {
153+
const n = nums1.length;
154+
let ans: number = 0;
155+
for (let k = 0; k < n; ++k) {
156+
let t: number = 0;
157+
for (let i = 0; i < n; ++i) {
158+
if (nums1[(i + k) % n] === nums2[i]) {
159+
++t;
160+
}
161+
}
162+
ans = Math.max(ans, t);
163+
}
164+
return ans;
165+
}
166+
```
167+
168+
<!-- tabs:end -->
169+
170+
<!-- solution:end -->
171+
172+
<!-- problem:end -->
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3400.Maximum%20Number%20of%20Matching%20Indices%20After%20Right%20Shifts/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3400. Maximum Number of Matching Indices After Right Shifts 🔒](https://leetcode.com/problems/maximum-number-of-matching-indices-after-right-shifts)
10+
11+
[中文文档](/solution/3400-3499/3400.Maximum%20Number%20of%20Matching%20Indices%20After%20Right%20Shifts/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given two integer arrays, <code>nums1</code> and <code>nums2</code>, of the same length.</p>
18+
19+
<p>An index <code>i</code> is considered <strong>matching</strong> if <code>nums1[i] == nums2[i]</code>.</p>
20+
21+
<p>Return the <strong>maximum</strong> number of <strong>matching</strong> indices after performing any number of <strong>right shifts</strong> on <code>nums1</code>.</p>
22+
23+
<p>A <strong>right shift</strong> is defined as shifting the element at index <code>i</code> to index <code>(i + 1) % n</code>, for all indices.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
28+
<div class="example-block">
29+
<p><strong>Input:</strong> <span class="example-io">nums1 = [3,1,2,3,1,2], nums2 = [1,2,3,1,2,3]</span></p>
30+
31+
<p><strong>Output:</strong> <span class="example-io">6</span></p>
32+
33+
<p><strong>Explanation:</strong></p>
34+
35+
<p>If we right shift <code>nums1</code> 2 times, it becomes <code>[1, 2, 3, 1, 2, 3]</code>. Every index matches, so the output is 6.</p>
36+
</div>
37+
38+
<p><strong class="example">Example 2:</strong></p>
39+
40+
<div class="example-block">
41+
<p><strong>Input:</strong> <span class="example-io">nums1 = [1,4,2,5,3,1], nums2 = [2,3,1,2,4,6]</span></p>
42+
43+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
44+
45+
<p><strong>Explanation:</strong></p>
46+
47+
<p>If we right shift <code>nums1</code> 3 times, it becomes <code>[5, 3, 1, 1, 4, 2]</code>. Indices 1, 2, and 4 match, so the output is 3.</p>
48+
</div>
49+
50+
<p>&nbsp;</p>
51+
<p><strong>Constraints:</strong></p>
52+
53+
<ul>
54+
<li><code>nums1.length == nums2.length</code></li>
55+
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 3000</code></li>
56+
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>9</sup></code></li>
57+
</ul>
58+
59+
<!-- description:end -->
60+
61+
## Solutions
62+
63+
<!-- solution:start -->
64+
65+
### Solution 1: Enumeration
66+
67+
We can enumerate the number of right shifts $k,ドル where 0ドル \leq k < n$. For each $k,ドル we can calculate the number of matching indices between the array $\textit{nums1}$ after right shifting $k$ times and $\textit{nums2}$. The maximum value is taken as the answer.
68+
69+
The time complexity is $O(n^2),ドル where $n$ is the length of the array $\textit{nums1}$. The space complexity is $O(1)$.
70+
71+
<!-- tabs:start -->
72+
73+
#### Python3
74+
75+
```python
76+
class Solution:
77+
def maximumMatchingIndices(self, nums1: List[int], nums2: List[int]) -> int:
78+
n = len(nums1)
79+
ans = 0
80+
for k in range(n):
81+
t = sum(nums1[(i + k) % n] == x for i, x in enumerate(nums2))
82+
ans = max(ans, t)
83+
return ans
84+
```
85+
86+
#### Java
87+
88+
```java
89+
class Solution {
90+
public int maximumMatchingIndices(int[] nums1, int[] nums2) {
91+
int n = nums1.length;
92+
int ans = 0;
93+
for (int k = 0; k < n; ++k) {
94+
int t = 0;
95+
for (int i = 0; i < n; ++i) {
96+
if (nums1[(i + k) % n] == nums2[i]) {
97+
++t;
98+
}
99+
}
100+
ans = Math.max(ans, t);
101+
}
102+
return ans;
103+
}
104+
}
105+
```
106+
107+
#### C++
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int maximumMatchingIndices(vector<int>& nums1, vector<int>& nums2) {
113+
int n = nums1.size();
114+
int ans = 0;
115+
for (int k = 0; k < n; ++k) {
116+
int t = 0;
117+
for (int i = 0; i < n; ++i) {
118+
if (nums1[(i + k) % n] == nums2[i]) {
119+
++t;
120+
}
121+
}
122+
ans = max(ans, t);
123+
}
124+
return ans;
125+
}
126+
};
127+
```
128+
129+
#### Go
130+
131+
```go
132+
func maximumMatchingIndices(nums1 []int, nums2 []int) (ans int) {
133+
n := len(nums1)
134+
for k := range nums1 {
135+
t := 0
136+
for i, x := range nums2 {
137+
if nums1[(i+k)%n] == x {
138+
t++
139+
}
140+
}
141+
ans = max(ans, t)
142+
}
143+
return
144+
}
145+
```
146+
147+
#### TypeScript
148+
149+
```ts
150+
function maximumMatchingIndices(nums1: number[], nums2: number[]): number {
151+
const n = nums1.length;
152+
let ans: number = 0;
153+
for (let k = 0; k < n; ++k) {
154+
let t: number = 0;
155+
for (let i = 0; i < n; ++i) {
156+
if (nums1[(i + k) % n] === nums2[i]) {
157+
++t;
158+
}
159+
}
160+
ans = Math.max(ans, t);
161+
}
162+
return ans;
163+
}
164+
```
165+
166+
<!-- tabs:end -->
167+
168+
<!-- solution:end -->
169+
170+
<!-- problem:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /