diff --git a/solution/1500-1599/1565.Unique Orders and Customers Per Month/README_EN.md b/solution/1500-1599/1565.Unique Orders and Customers Per Month/README_EN.md index 17f2788eee346..308a4c17a7bd1 100644 --- a/solution/1500-1599/1565.Unique Orders and Customers Per Month/README_EN.md +++ b/solution/1500-1599/1565.Unique Orders and Customers Per Month/README_EN.md @@ -43,7 +43,7 @@ This table contains information about the orders made by customer_id.

Example 1:

-Input:
+Input: 
 Orders table:
 +----------+------------+-------------+------------+
 | order_id | order_date | customer_id | invoice    |
@@ -59,7 +59,7 @@ Orders table:
 | 9        | 2021-01-07 | 3           | 31         |
 | 10       | 2021-01-15 | 2           | 20         |
 +----------+------------+-------------+------------+
-Output:
+Output: 
 +---------+-------------+----------------+
 | month   | order_count | customer_count |
 +---------+-------------+----------------+
@@ -68,7 +68,7 @@ Orders table:
 | 2020-12 | 2           | 1              |
 | 2021-01 | 1           | 1              |
 +---------+-------------+----------------+
-Explanation:
+Explanation: 
 In September 2020 we have two orders from 2 different customers with invoices > $20.
 In October 2020 we have two orders from 1 customer, and only one of the two orders has invoice > $20.
 In November 2020 we have two orders from 2 different customers but invoices < $20, so we don't include that month.
diff --git a/solution/1500-1599/1577.Number of Ways Where Square of Number Is Equal to Product of Two Numbers/README_EN.md b/solution/1500-1599/1577.Number of Ways Where Square of Number Is Equal to Product of Two Numbers/README_EN.md
index 830fab875afd5..af21803f6266d 100644
--- a/solution/1500-1599/1577.Number of Ways Where Square of Number Is Equal to Product of Two Numbers/README_EN.md	
+++ b/solution/1500-1599/1577.Number of Ways Where Square of Number Is Equal to Product of Two Numbers/README_EN.md	
@@ -34,7 +34,7 @@ tags:
 
 Input: nums1 = [7,4], nums2 = [5,2,8,9]
 Output: 1
-Explanation: Type 1: (1, 1, 2), nums1[1]2 = nums2[1] * nums2[2]. (42 = 2 * 8).
+Explanation: Type 1: (1, 1, 2), nums1[1]2 = nums2[1] * nums2[2]. (42 = 2 * 8). 
 

Example 2:

diff --git a/solution/1500-1599/1589.Maximum Sum Obtained of Any Permutation/README_EN.md b/solution/1500-1599/1589.Maximum Sum Obtained of Any Permutation/README_EN.md index 386c07bc8ee49..f2b200a3a8618 100644 --- a/solution/1500-1599/1589.Maximum Sum Obtained of Any Permutation/README_EN.md +++ b/solution/1500-1599/1589.Maximum Sum Obtained of Any Permutation/README_EN.md @@ -33,7 +33,7 @@ tags:
 Input: nums = [1,2,3,4,5], requests = [[1,3],[0,1]]
 Output: 19
-Explanation: One permutation of nums is [2,1,3,4,5] with the following result:
+Explanation: One permutation of nums is [2,1,3,4,5] with the following result: 
 requests[0] -> nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8
 requests[1] -> nums[0] + nums[1] = 2 + 1 = 3
 Total sum: 8 + 3 = 11.
diff --git a/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/README.md b/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/README.md
new file mode 100644
index 0000000000000..7e0d855f9d99f
--- /dev/null
+++ b/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/README.md	
@@ -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
+---
+
+
+
+# [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)
+
+## 题目描述
+
+
+
+

给定两个长度相同的整数数组 nums1 和 nums2

+ +

如果 nums1[i] == nums2[i] 则认为下标 i匹配 的。

+ +

返回在 nums1 上进行任意次数 右移 后 最大 的 匹配 下标数量。

+ +

右移 是对于所有下标,将位于下标 i 的元素移动到 (i + 1) % n

+ +

 

+ +

示例 1:

+ +
+

输入:nums1 = [3,1,2,3,1,2], nums2 = [1,2,3,1,2,3]

+ +

输出:6

+ +

解释:

+ +

如果我们右移 nums1 2 次,它变为 [1, 2, 3, 1, 2, 3]。每个下标都匹配,所以输出为 6。

+
+ +

示例 2:

+ +
+

输入:nums1 = [1,4,2,5,3,1], nums2 = [2,3,1,2,4,6]

+ +

输出:3

+ +

解释:

+ +

如果我们右移 nums1 3 次,它变为 [5, 3, 1, 1, 4, 2]。下标 1,2,4 匹配,所以输出为 3。

+
+ +

 

+ +

提示:

+ + + + + +## 解法 + + + +### 方法一:枚举 + +我们可以枚举右移的次数 $k$,其中 $0 \leq k \lt n$。对于每一个 $k$,我们可以计算出右移 $k$ 次后的数组 $\textit{nums1}$ 和 $\textit{nums2}$ 的匹配下标数量,取最大值作为答案即可。 + +时间复杂度 $O(n^2)$,其中 $n$ 为数组 $\textit{nums1}$ 的长度。空间复杂度 $O(1)$。 + + + +#### 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& nums1, vector& 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; +} +``` + + + + + + diff --git a/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/README_EN.md b/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/README_EN.md new file mode 100644 index 0000000000000..0b88e75ddc597 --- /dev/null +++ b/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/README_EN.md @@ -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 +--- + + + +# [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 + + + +

You are given two integer arrays, nums1 and nums2, of the same length.

+ +

An index i is considered matching if nums1[i] == nums2[i].

+ +

Return the maximum number of matching indices after performing any number of right shifts on nums1.

+ +

A right shift is defined as shifting the element at index i to index (i + 1) % n, for all indices.

+ +

 

+

Example 1:

+ +
+

Input: nums1 = [3,1,2,3,1,2], nums2 = [1,2,3,1,2,3]

+ +

Output: 6

+ +

Explanation:

+ +

If we right shift nums1 2 times, it becomes [1, 2, 3, 1, 2, 3]. Every index matches, so the output is 6.

+
+ +

Example 2:

+ +
+

Input: nums1 = [1,4,2,5,3,1], nums2 = [2,3,1,2,4,6]

+ +

Output: 3

+ +

Explanation:

+ +

If we right shift nums1 3 times, it becomes [5, 3, 1, 1, 4, 2]. Indices 1, 2, and 4 match, so the output is 3.

+
+ +

 

+

Constraints:

+ +
    +
  • nums1.length == nums2.length
  • +
  • 1 <= nums1.length, nums2.length <= 3000
  • +
  • 1 <= nums1[i], nums2[i] <= 109
  • +
+ + + +## Solutions + + + +### 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)$. + + + +#### 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& nums1, vector& 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; +} +``` + + + + + + diff --git a/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.cpp b/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.cpp new file mode 100644 index 0000000000000..2832ec0b23e3c --- /dev/null +++ b/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int maximumMatchingIndices(vector& nums1, vector& 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; + } +}; diff --git a/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.go b/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.go new file mode 100644 index 0000000000000..c2d173c2ce12c --- /dev/null +++ b/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.go @@ -0,0 +1,13 @@ +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 +} diff --git a/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.java b/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.java new file mode 100644 index 0000000000000..301cba253c1d4 --- /dev/null +++ b/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.java @@ -0,0 +1,16 @@ +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; + } +} diff --git a/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.py b/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.py new file mode 100644 index 0000000000000..ced0cc1c3ef6e --- /dev/null +++ b/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.py @@ -0,0 +1,8 @@ +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 diff --git a/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.ts b/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.ts new file mode 100644 index 0000000000000..b0a09fe147751 --- /dev/null +++ b/solution/3400-3499/3400.Maximum Number of Matching Indices After Right Shifts/Solution.ts @@ -0,0 +1,14 @@ +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; +} diff --git a/solution/README.md b/solution/README.md index d73f2b1355251..753f3ac5cc3d3 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3410,6 +3410,7 @@ | 3397 | [执行操作后不同元素的最大数量](/solution/3300-3399/3397.Maximum%20Number%20of%20Distinct%20Elements%20After%20Operations/README.md) | | 中等 | 第 429 场周赛 | | 3398 | [字符相同的最短子字符串 I](/solution/3300-3399/3398.Smallest%20Substring%20With%20Identical%20Characters%20I/README.md) | | 困难 | 第 429 场周赛 | | 3399 | [字符相同的最短子字符串 II](/solution/3300-3399/3399.Smallest%20Substring%20With%20Identical%20Characters%20II/README.md) | | 困难 | 第 429 场周赛 | +| 3400 | [右移后的最大匹配索引数](/solution/3400-3499/3400.Maximum%20Number%20of%20Matching%20Indices%20After%20Right%20Shifts/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index d2a51356a556b..5ce0855910a6a 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3408,6 +3408,7 @@ Press Control + F(or Command + F on | 3397 | [Maximum Number of Distinct Elements After Operations](/solution/3300-3399/3397.Maximum%20Number%20of%20Distinct%20Elements%20After%20Operations/README_EN.md) | | Medium | Weekly Contest 429 | | 3398 | [Smallest Substring With Identical Characters I](/solution/3300-3399/3398.Smallest%20Substring%20With%20Identical%20Characters%20I/README_EN.md) | | Hard | Weekly Contest 429 | | 3399 | [Smallest Substring With Identical Characters II](/solution/3300-3399/3399.Smallest%20Substring%20With%20Identical%20Characters%20II/README_EN.md) | | Hard | Weekly Contest 429 | +| 3400 | [Maximum Number of Matching Indices After Right Shifts](/solution/3400-3499/3400.Maximum%20Number%20of%20Matching%20Indices%20After%20Right%20Shifts/README_EN.md) | | Medium | 🔒 | ## Copyright