Skip to content

feat: add solutions to lc problem: No.3400 #3890

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
Dec 27, 2024
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 @@ -43,7 +43,7 @@ This table contains information about the orders made by customer_id.
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong>
<strong>Input:</strong>
Orders table:
+----------+------------+-------------+------------+
| order_id | order_date | customer_id | invoice |
Expand All @@ -59,7 +59,7 @@ Orders table:
| 9 | 2021-01-07 | 3 | 31 |
| 10 | 2021-01-15 | 2 | 20 |
+----------+------------+-------------+------------+
<strong>Output:</strong>
<strong>Output:</strong>
+---------+-------------+----------------+
| month | order_count | customer_count |
+---------+-------------+----------------+
Expand All @@ -68,7 +68,7 @@ Orders table:
| 2020-12 | 2 | 1 |
| 2021-01 | 1 | 1 |
+---------+-------------+----------------+
<strong>Explanation:</strong>
<strong>Explanation:</strong>
In September 2020 we have two orders from 2 different customers with invoices &gt; $20.
In October 2020 we have two orders from 1 customer, and only one of the two orders has invoice &gt; $20.
In November 2020 we have two orders from 2 different customers but invoices &lt; $20, so we don&#39;t include that month.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ tags:
<pre>
<strong>Input:</strong> nums1 = [7,4], nums2 = [5,2,8,9]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Type 1: (1, 1, 2), nums1[1]<sup>2</sup> = nums2[1] * nums2[2]. (4<sup>2</sup> = 2 * 8).
<strong>Explanation:</strong> Type 1: (1, 1, 2), nums1[1]<sup>2</sup> = nums2[1] * nums2[2]. (4<sup>2</sup> = 2 * 8).
</pre>

<p><strong class="example">Example 2:</strong></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], requests = [[1,3],[0,1]]
<strong>Output:</strong> 19
<strong>Explanation:</strong> One permutation of nums is [2,1,3,4,5] with the following result:
<strong>Explanation:</strong> One permutation of nums is [2,1,3,4,5] with the following result:
requests[0] -&gt; nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8
requests[1] -&gt; nums[0] + nums[1] = 2 + 1 = 3
Total sum: 8 + 3 = 11.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
---
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3400.Maximum%20Number%20of%20Matching%20Indices%20After%20Right%20Shifts/README.md
---

<!-- problem:start -->

# [3400. 右移后的最大匹配索引数 🔒](https://leetcode.cn/problems/maximum-number-of-matching-indices-after-right-shifts)

[English Version](/solution/3400-3499/3400.Maximum%20Number%20of%20Matching%20Indices%20After%20Right%20Shifts/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给定两个长度相同的整数数组&nbsp;<code>nums1</code> 和&nbsp;<code>nums2</code>。</p>

<p>如果&nbsp;<code>nums1[i] == nums2[i]</code>&nbsp;则认为下标&nbsp;<code>i</code> 是 <strong>匹配</strong> 的。</p>

<p>返回在&nbsp;<code>nums1</code>&nbsp;上进行任意次数 <strong>右移</strong>&nbsp;后 <strong>最大</strong>&nbsp;的 <strong>匹配&nbsp;</strong>下标数量。</p>

<p><strong>右移&nbsp;</strong>是对于所有下标,将位于下标&nbsp;<code>i</code>&nbsp;的元素移动到&nbsp;<code>(i + 1) % n</code>。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">nums1 = [3,1,2,3,1,2], nums2 = [1,2,3,1,2,3]</span></p>

<p><span class="example-io"><b>输出:</b>6</span></p>

<p><strong>解释:</strong></p>

<p>如果我们右移&nbsp;<code>nums1</code> 2 次,它变为&nbsp;<code>[1, 2, 3, 1, 2, 3]</code>。每个下标都匹配,所以输出为 6。</p>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums1 = [1,4,2,5,3,1], nums2 = [2,3,1,2,4,6]</span></p>

<p><span class="example-io"><b>输出:</b>3</span></p>

<p><strong>解释:</strong></p>

<p>如果我们右移&nbsp;<code>nums1</code> 3 次,它变为&nbsp;<code>[5, 3, 1, 1, 4, 2]</code>。下标 1,2,4 匹配,所以输出为 3。</p>
</div>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>nums1.length == nums2.length</code></li>
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 3000</code></li>
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>9</sup></code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:枚举

我们可以枚举右移的次数 $k$,其中 $0 \leq k \lt n$。对于每一个 $k$,我们可以计算出右移 $k$ 次后的数组 $\textit{nums1}$ 和 $\textit{nums2}$ 的匹配下标数量,取最大值作为答案即可。

时间复杂度 $O(n^2)$,其中 $n$ 为数组 $\textit{nums1}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def maximumMatchingIndices(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
ans = 0
for k in range(n):
t = sum(nums1[(i + k) % n] == x for i, x in enumerate(nums2))
ans = max(ans, t)
return ans
```

#### Java

```java
class Solution {
public int maximumMatchingIndices(int[] nums1, int[] nums2) {
int n = nums1.length;
int ans = 0;
for (int k = 0; k < n; ++k) {
int t = 0;
for (int i = 0; i < n; ++i) {
if (nums1[(i + k) % n] == nums2[i]) {
++t;
}
}
ans = Math.max(ans, t);
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int maximumMatchingIndices(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size();
int ans = 0;
for (int k = 0; k < n; ++k) {
int t = 0;
for (int i = 0; i < n; ++i) {
if (nums1[(i + k) % n] == nums2[i]) {
++t;
}
}
ans = max(ans, t);
}
return ans;
}
};
```
#### Go
```go
func maximumMatchingIndices(nums1 []int, nums2 []int) (ans int) {
n := len(nums1)
for k := range nums1 {
t := 0
for i, x := range nums2 {
if nums1[(i+k)%n] == x {
t++
}
}
ans = max(ans, t)
}
return
}
```

#### TypeScript

```ts
function maximumMatchingIndices(nums1: number[], nums2: number[]): number {
const n = nums1.length;
let ans: number = 0;
for (let k = 0; k < n; ++k) {
let t: number = 0;
for (let i = 0; i < n; ++i) {
if (nums1[(i + k) % n] === nums2[i]) {
++t;
}
}
ans = Math.max(ans, t);
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
---
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3400.Maximum%20Number%20of%20Matching%20Indices%20After%20Right%20Shifts/README_EN.md
---

<!-- problem:start -->

# [3400. Maximum Number of Matching Indices After Right Shifts 🔒](https://leetcode.com/problems/maximum-number-of-matching-indices-after-right-shifts)

[中文文档](/solution/3400-3499/3400.Maximum%20Number%20of%20Matching%20Indices%20After%20Right%20Shifts/README.md)

## Description

<!-- description:start -->

<p>You are given two integer arrays, <code>nums1</code> and <code>nums2</code>, of the same length.</p>

<p>An index <code>i</code> is considered <strong>matching</strong> if <code>nums1[i] == nums2[i]</code>.</p>

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

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums1 = [3,1,2,3,1,2], nums2 = [1,2,3,1,2,3]</span></p>

<p><strong>Output:</strong> <span class="example-io">6</span></p>

<p><strong>Explanation:</strong></p>

<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>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums1 = [1,4,2,5,3,1], nums2 = [2,3,1,2,4,6]</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<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>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>nums1.length == nums2.length</code></li>
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 3000</code></li>
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>9</sup></code></li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1: Enumeration

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.

The time complexity is $O(n^2)$, where $n$ is the length of the array $\textit{nums1}$. The space complexity is $O(1)$.

<!-- tabs:start -->

#### Python3

```python
class Solution:
def maximumMatchingIndices(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
ans = 0
for k in range(n):
t = sum(nums1[(i + k) % n] == x for i, x in enumerate(nums2))
ans = max(ans, t)
return ans
```

#### Java

```java
class Solution {
public int maximumMatchingIndices(int[] nums1, int[] nums2) {
int n = nums1.length;
int ans = 0;
for (int k = 0; k < n; ++k) {
int t = 0;
for (int i = 0; i < n; ++i) {
if (nums1[(i + k) % n] == nums2[i]) {
++t;
}
}
ans = Math.max(ans, t);
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int maximumMatchingIndices(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size();
int ans = 0;
for (int k = 0; k < n; ++k) {
int t = 0;
for (int i = 0; i < n; ++i) {
if (nums1[(i + k) % n] == nums2[i]) {
++t;
}
}
ans = max(ans, t);
}
return ans;
}
};
```

#### Go

```go
func maximumMatchingIndices(nums1 []int, nums2 []int) (ans int) {
n := len(nums1)
for k := range nums1 {
t := 0
for i, x := range nums2 {
if nums1[(i+k)%n] == x {
t++
}
}
ans = max(ans, t)
}
return
}
```

#### TypeScript

```ts
function maximumMatchingIndices(nums1: number[], nums2: number[]): number {
const n = nums1.length;
let ans: number = 0;
for (let k = 0; k < n; ++k) {
let t: number = 0;
for (let i = 0; i < n; ++i) {
if (nums1[(i + k) % n] === nums2[i]) {
++t;
}
}
ans = Math.max(ans, t);
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading
Loading