diff --git a/solution/2700-2799/2748.Number of Beautiful Pairs/README.md b/solution/2700-2799/2748.Number of Beautiful Pairs/README.md index bc93f9b4588e9..382f7a8434f2b 100644 --- a/solution/2700-2799/2748.Number of Beautiful Pairs/README.md +++ b/solution/2700-2799/2748.Number of Beautiful Pairs/README.md @@ -70,7 +70,15 @@ i = 0 和 j = 2 :nums[0] 的第一个数字是 1 ,nums[2] 的最后一个数 -### 方法一 +### 方法一:计数 + +我们可以用一个长度为 $10$ 的数组 $\text{cnt}$ 来记录每个数字的第一个数字出现的次数。 + +遍历数组 $\text{nums}$,对于每个数字 $x$,我们枚举 $0$ 到 $9$ 的每个数字 $y$,如果 $\text{cnt}[y]$ 不为 $0$ 且 $\text{gcd}(x b\mod 10, y) = 1$,则答案加上 $\text{cnt}[y]$。然后,我们将 $x$ 的第一个数字出现的次数加 $1$。 + +遍历结束后,返回答案即可。 + +时间复杂度 $O(n \times (k + \log M))$,空间复杂度 $O(k + \log M)$。其中 $n$ 为数组 $\text{nums}$ 的长度,而 $k$ 和 $M$ 分别表示数组 $\text{nums}$ 中的数字的种类以及最大值。 diff --git a/solution/2700-2799/2748.Number of Beautiful Pairs/README_EN.md b/solution/2700-2799/2748.Number of Beautiful Pairs/README_EN.md index 0ef54ca10efaa..5b9a2ea903ea6 100644 --- a/solution/2700-2799/2748.Number of Beautiful Pairs/README_EN.md +++ b/solution/2700-2799/2748.Number of Beautiful Pairs/README_EN.md @@ -69,7 +69,15 @@ Thus, we return 2. -### Solution 1 +### Solution 1: Counting + +We can use an array $\text{cnt}$ of length $10$ to record the count of the first digit of each number. + +Iterate through the array $\text{nums}$. For each number $x$, we enumerate each digit $y$ from $0$ to $9$. If $\text{cnt}[y]$ is not $0$ and $\text{gcd}(x \mod 10, y) = 1$, then the answer is incremented by $\text{cnt}[y]$. Then, we increment the count of the first digit of $x$ by $1$. + +After the iteration, return the answer. + +The time complexity is $O(n \times (k + \log M))$, and the space complexity is $O(k + \log M)$. Here, $n$ is the length of the array $\text{nums}$, while $k$ and $M$ respectively represent the number of distinct numbers and the maximum value in the array $\text{nums}$. diff --git a/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README.md b/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README.md index 2d026fac1fae9..54e3498390cfa 100644 --- a/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README.md +++ b/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3158.Find%20the%20XOR%20of%20Numbers%20Which%20Appear%20Twice/README.md +rating: 1172 +source: 第 131 场双周赛 Q1 tags: - 位运算 - 数组 diff --git a/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README_EN.md b/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README_EN.md index 8f070239505d9..497c8b45a4ad2 100644 --- a/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README_EN.md +++ b/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3158.Find%20the%20XOR%20of%20Numbers%20Which%20Appear%20Twice/README_EN.md +rating: 1172 +source: Biweekly Contest 131 Q1 tags: - Bit Manipulation - Array diff --git a/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README.md b/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README.md index a5e38d623a534..e40f0c105fb26 100644 --- a/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README.md +++ b/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3159.Find%20Occurrences%20of%20an%20Element%20in%20an%20Array/README.md +rating: 1262 +source: 第 131 场双周赛 Q2 tags: - 数组 - 哈希表 diff --git a/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README_EN.md b/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README_EN.md index 2d2d8c150eb1b..f77b0bedebc03 100644 --- a/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README_EN.md +++ b/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3159.Find%20Occurrences%20of%20an%20Element%20in%20an%20Array/README_EN.md +rating: 1262 +source: Biweekly Contest 131 Q2 tags: - Array - Hash Table diff --git a/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README.md b/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README.md index 9f94281c34d43..f67d5d4a3f5c4 100644 --- a/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README.md +++ b/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3160.Find%20the%20Number%20of%20Distinct%20Colors%20Among%20the%20Balls/README.md +rating: 1517 +source: 第 131 场双周赛 Q3 tags: - 数组 - 哈希表 diff --git a/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README_EN.md b/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README_EN.md index 9342d11aa1b80..ed3c1ff308fca 100644 --- a/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README_EN.md +++ b/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3160.Find%20the%20Number%20of%20Distinct%20Colors%20Among%20the%20Balls/README_EN.md +rating: 1517 +source: Biweekly Contest 131 Q3 tags: - Array - Hash Table diff --git a/solution/3100-3199/3161.Block Placement Queries/README.md b/solution/3100-3199/3161.Block Placement Queries/README.md index 069d4a3e0de1f..877e3ddbbae62 100644 --- a/solution/3100-3199/3161.Block Placement Queries/README.md +++ b/solution/3100-3199/3161.Block Placement Queries/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3161.Block%20Placement%20Queries/README.md +rating: 2513 +source: 第 131 场双周赛 Q4 tags: - 树状数组 - 线段树 diff --git a/solution/3100-3199/3161.Block Placement Queries/README_EN.md b/solution/3100-3199/3161.Block Placement Queries/README_EN.md index 73949f1ffb4b6..fb3400190bd0d 100644 --- a/solution/3100-3199/3161.Block Placement Queries/README_EN.md +++ b/solution/3100-3199/3161.Block Placement Queries/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3161.Block%20Placement%20Queries/README_EN.md +rating: 2513 +source: Biweekly Contest 131 Q4 tags: - Binary Indexed Tree - Segment Tree diff --git a/solution/3100-3199/3162.Find the Number of Good Pairs I/README.md b/solution/3100-3199/3162.Find the Number of Good Pairs I/README.md index fa660e9d8fa0e..a99efdfd8a83e 100644 --- a/solution/3100-3199/3162.Find the Number of Good Pairs I/README.md +++ b/solution/3100-3199/3162.Find the Number of Good Pairs I/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3162.Find%20the%20Number%20of%20Good%20Pairs%20I/README.md +rating: 1168 +source: 第 399 场周赛 Q1 tags: - 数组 - 哈希表 diff --git a/solution/3100-3199/3162.Find the Number of Good Pairs I/README_EN.md b/solution/3100-3199/3162.Find the Number of Good Pairs I/README_EN.md index 87ba55a87dc40..194216adbaa3c 100644 --- a/solution/3100-3199/3162.Find the Number of Good Pairs I/README_EN.md +++ b/solution/3100-3199/3162.Find the Number of Good Pairs I/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3162.Find%20the%20Number%20of%20Good%20Pairs%20I/README_EN.md +rating: 1168 +source: Weekly Contest 399 Q1 tags: - Array - Hash Table diff --git a/solution/3100-3199/3163.String Compression III/README.md b/solution/3100-3199/3163.String Compression III/README.md index 26c18eb78149f..0e3135ffb0678 100644 --- a/solution/3100-3199/3163.String Compression III/README.md +++ b/solution/3100-3199/3163.String Compression III/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3163.String%20Compression%20III/README.md +rating: 1311 +source: 第 399 场周赛 Q2 tags: - 字符串 --- diff --git a/solution/3100-3199/3163.String Compression III/README_EN.md b/solution/3100-3199/3163.String Compression III/README_EN.md index d5692ef8fc6d3..7c4452240287a 100644 --- a/solution/3100-3199/3163.String Compression III/README_EN.md +++ b/solution/3100-3199/3163.String Compression III/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3163.String%20Compression%20III/README_EN.md +rating: 1311 +source: Weekly Contest 399 Q2 tags: - String --- diff --git a/solution/3100-3199/3164.Find the Number of Good Pairs II/README.md b/solution/3100-3199/3164.Find the Number of Good Pairs II/README.md index 8e955b77cdb32..8fd39fac99c32 100644 --- a/solution/3100-3199/3164.Find the Number of Good Pairs II/README.md +++ b/solution/3100-3199/3164.Find the Number of Good Pairs II/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3164.Find%20the%20Number%20of%20Good%20Pairs%20II/README.md +rating: 1777 +source: 第 399 场周赛 Q3 tags: - 数组 - 哈希表 diff --git a/solution/3100-3199/3164.Find the Number of Good Pairs II/README_EN.md b/solution/3100-3199/3164.Find the Number of Good Pairs II/README_EN.md index a296ba0a03f4c..b15407f7cdf8e 100644 --- a/solution/3100-3199/3164.Find the Number of Good Pairs II/README_EN.md +++ b/solution/3100-3199/3164.Find the Number of Good Pairs II/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3164.Find%20the%20Number%20of%20Good%20Pairs%20II/README_EN.md +rating: 1777 +source: Weekly Contest 399 Q3 tags: - Array - Hash Table diff --git a/solution/3100-3199/3165.Maximum Sum of Subsequence With Non-adjacent Elements/README.md b/solution/3100-3199/3165.Maximum Sum of Subsequence With Non-adjacent Elements/README.md index 6c6bb31f7d249..dfcefb94b3902 100644 --- a/solution/3100-3199/3165.Maximum Sum of Subsequence With Non-adjacent Elements/README.md +++ b/solution/3100-3199/3165.Maximum Sum of Subsequence With Non-adjacent Elements/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3165.Maximum%20Sum%20of%20Subsequence%20With%20Non-adjacent%20Elements/README.md +rating: 2697 +source: 第 399 场周赛 Q4 tags: - 线段树 - 数组 diff --git a/solution/3100-3199/3165.Maximum Sum of Subsequence With Non-adjacent Elements/README_EN.md b/solution/3100-3199/3165.Maximum Sum of Subsequence With Non-adjacent Elements/README_EN.md index d8987c905d716..8cbed702cb356 100644 --- a/solution/3100-3199/3165.Maximum Sum of Subsequence With Non-adjacent Elements/README_EN.md +++ b/solution/3100-3199/3165.Maximum Sum of Subsequence With Non-adjacent Elements/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3165.Maximum%20Sum%20of%20Subsequence%20With%20Non-adjacent%20Elements/README_EN.md +rating: 2697 +source: Weekly Contest 399 Q4 tags: - Segment Tree - Array diff --git a/solution/3100-3199/3168.Minimum Number of Chairs in a Waiting Room/README.md b/solution/3100-3199/3168.Minimum Number of Chairs in a Waiting Room/README.md index 5ca59a00b105d..fef1a59a66d2c 100644 --- a/solution/3100-3199/3168.Minimum Number of Chairs in a Waiting Room/README.md +++ b/solution/3100-3199/3168.Minimum Number of Chairs in a Waiting Room/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3168.Minimum%20Number%20of%20Chairs%20in%20a%20Waiting%20Room/README.md +rating: 1211 +source: 第 400 场周赛 Q1 tags: - 字符串 - 模拟 diff --git a/solution/3100-3199/3168.Minimum Number of Chairs in a Waiting Room/README_EN.md b/solution/3100-3199/3168.Minimum Number of Chairs in a Waiting Room/README_EN.md index 233f63a98682d..592a0a693dd72 100644 --- a/solution/3100-3199/3168.Minimum Number of Chairs in a Waiting Room/README_EN.md +++ b/solution/3100-3199/3168.Minimum Number of Chairs in a Waiting Room/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3168.Minimum%20Number%20of%20Chairs%20in%20a%20Waiting%20Room/README_EN.md +rating: 1211 +source: Weekly Contest 400 Q1 tags: - String - Simulation diff --git a/solution/3100-3199/3169.Count Days Without Meetings/README.md b/solution/3100-3199/3169.Count Days Without Meetings/README.md index cbf8115634c30..bb0e8d21cc9b2 100644 --- a/solution/3100-3199/3169.Count Days Without Meetings/README.md +++ b/solution/3100-3199/3169.Count Days Without Meetings/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3169.Count%20Days%20Without%20Meetings/README.md +rating: 1483 +source: 第 400 场周赛 Q2 tags: - 数组 - 排序 diff --git a/solution/3100-3199/3169.Count Days Without Meetings/README_EN.md b/solution/3100-3199/3169.Count Days Without Meetings/README_EN.md index 4cfe0f6627a39..5373d1f894693 100644 --- a/solution/3100-3199/3169.Count Days Without Meetings/README_EN.md +++ b/solution/3100-3199/3169.Count Days Without Meetings/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3169.Count%20Days%20Without%20Meetings/README_EN.md +rating: 1483 +source: Weekly Contest 400 Q2 tags: - Array - Sorting diff --git a/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README.md b/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README.md index b4e93f16665fe..8d1aa6931366a 100644 --- a/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README.md +++ b/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3170.Lexicographically%20Minimum%20String%20After%20Removing%20Stars/README.md +rating: 1772 +source: 第 400 场周赛 Q3 tags: - 栈 - 贪心 diff --git a/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README_EN.md b/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README_EN.md index 3f3d0b8e24dfe..1703505a8e3d5 100644 --- a/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README_EN.md +++ b/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3170.Lexicographically%20Minimum%20String%20After%20Removing%20Stars/README_EN.md +rating: 1772 +source: Weekly Contest 400 Q3 tags: - Stack - Greedy diff --git a/solution/3100-3199/3171.Find Subarray With Bitwise OR Closest to K/README.md b/solution/3100-3199/3171.Find Subarray With Bitwise OR Closest to K/README.md index c5279d8b9b7e5..8bc8c9f2c1a35 100644 --- a/solution/3100-3199/3171.Find Subarray With Bitwise OR Closest to K/README.md +++ b/solution/3100-3199/3171.Find Subarray With Bitwise OR Closest to K/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3171.Find%20Subarray%20With%20Bitwise%20OR%20Closest%20to%20K/README.md +rating: 2162 +source: 第 400 场周赛 Q4 tags: - 位运算 - 线段树 diff --git a/solution/3100-3199/3171.Find Subarray With Bitwise OR Closest to K/README_EN.md b/solution/3100-3199/3171.Find Subarray With Bitwise OR Closest to K/README_EN.md index f792d45c24001..b3bbb6021b442 100644 --- a/solution/3100-3199/3171.Find Subarray With Bitwise OR Closest to K/README_EN.md +++ b/solution/3100-3199/3171.Find Subarray With Bitwise OR Closest to K/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3171.Find%20Subarray%20With%20Bitwise%20OR%20Closest%20to%20K/README_EN.md +rating: 2162 +source: Weekly Contest 400 Q4 tags: - Bit Manipulation - Segment Tree diff --git a/solution/3100-3199/3174.Clear Digits/README.md b/solution/3100-3199/3174.Clear Digits/README.md index db0b2774e3b86..16bbb3258f58a 100644 --- a/solution/3100-3199/3174.Clear Digits/README.md +++ b/solution/3100-3199/3174.Clear Digits/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3174.Clear%20Digits/README.md +rating: 1255 +source: 第 132 场双周赛 Q1 tags: - 哈希表 - 字符串 diff --git a/solution/3100-3199/3174.Clear Digits/README_EN.md b/solution/3100-3199/3174.Clear Digits/README_EN.md index e112da2df26e6..77870d76bc439 100644 --- a/solution/3100-3199/3174.Clear Digits/README_EN.md +++ b/solution/3100-3199/3174.Clear Digits/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3174.Clear%20Digits/README_EN.md +rating: 1255 +source: Biweekly Contest 132 Q1 tags: - Hash Table - String diff --git a/solution/3100-3199/3175.Find The First Player to win K Games in a Row/README.md b/solution/3100-3199/3175.Find The First Player to win K Games in a Row/README.md index 84e52d4fb8f03..8de19fe9c6493 100644 --- a/solution/3100-3199/3175.Find The First Player to win K Games in a Row/README.md +++ b/solution/3100-3199/3175.Find The First Player to win K Games in a Row/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3175.Find%20The%20First%20Player%20to%20win%20K%20Games%20in%20a%20Row/README.md +rating: 1488 +source: 第 132 场双周赛 Q2 tags: - 数组 - 模拟 diff --git a/solution/3100-3199/3175.Find The First Player to win K Games in a Row/README_EN.md b/solution/3100-3199/3175.Find The First Player to win K Games in a Row/README_EN.md index 8f143cfbb3e1c..884e8b2c47db7 100644 --- a/solution/3100-3199/3175.Find The First Player to win K Games in a Row/README_EN.md +++ b/solution/3100-3199/3175.Find The First Player to win K Games in a Row/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3175.Find%20The%20First%20Player%20to%20win%20K%20Games%20in%20a%20Row/README_EN.md +rating: 1488 +source: Biweekly Contest 132 Q2 tags: - Array - Simulation diff --git a/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README.md b/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README.md index 80f7df129e076..40158e36eba5b 100644 --- a/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README.md +++ b/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3176.Find%20the%20Maximum%20Length%20of%20a%20Good%20Subsequence%20I/README.md +rating: 1849 +source: 第 132 场双周赛 Q3 tags: - 数组 - 哈希表 diff --git a/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README_EN.md b/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README_EN.md index f50ee48b4fad6..3f9a6db909578 100644 --- a/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README_EN.md +++ b/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3176.Find%20the%20Maximum%20Length%20of%20a%20Good%20Subsequence%20I/README_EN.md +rating: 1849 +source: Biweekly Contest 132 Q3 tags: - Array - Hash Table diff --git a/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README.md b/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README.md index 302b46314a970..161d08460dd5e 100644 --- a/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README.md +++ b/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3177.Find%20the%20Maximum%20Length%20of%20a%20Good%20Subsequence%20II/README.md +rating: 2364 +source: 第 132 场双周赛 Q4 tags: - 数组 - 哈希表 diff --git a/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README_EN.md b/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README_EN.md index b20d7b2317250..62f0620bbecec 100644 --- a/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README_EN.md +++ b/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3177.Find%20the%20Maximum%20Length%20of%20a%20Good%20Subsequence%20II/README_EN.md +rating: 2364 +source: Biweekly Contest 132 Q4 tags: - Array - Hash Table diff --git a/solution/3100-3199/3178.Find the Child Who Has the Ball After K Seconds/README.md b/solution/3100-3199/3178.Find the Child Who Has the Ball After K Seconds/README.md index 498e62e3c0618..99adfd76fec09 100644 --- a/solution/3100-3199/3178.Find the Child Who Has the Ball After K Seconds/README.md +++ b/solution/3100-3199/3178.Find the Child Who Has the Ball After K Seconds/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3178.Find%20the%20Child%20Who%20Has%20the%20Ball%20After%20K%20Seconds/README.md +rating: 1255 +source: 第 401 场周赛 Q1 tags: - 数学 - 模拟 diff --git a/solution/3100-3199/3178.Find the Child Who Has the Ball After K Seconds/README_EN.md b/solution/3100-3199/3178.Find the Child Who Has the Ball After K Seconds/README_EN.md index 3461a888b8b1d..0d987d1edfb04 100644 --- a/solution/3100-3199/3178.Find the Child Who Has the Ball After K Seconds/README_EN.md +++ b/solution/3100-3199/3178.Find the Child Who Has the Ball After K Seconds/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3178.Find%20the%20Child%20Who%20Has%20the%20Ball%20After%20K%20Seconds/README_EN.md +rating: 1255 +source: Weekly Contest 401 Q1 tags: - Math - Simulation diff --git a/solution/3100-3199/3179.Find the N-th Value After K Seconds/README.md b/solution/3100-3199/3179.Find the N-th Value After K Seconds/README.md index 0e04cbf26d6ff..7653f1d95471d 100644 --- a/solution/3100-3199/3179.Find the N-th Value After K Seconds/README.md +++ b/solution/3100-3199/3179.Find the N-th Value After K Seconds/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3179.Find%20the%20N-th%20Value%20After%20K%20Seconds/README.md +rating: 1369 +source: 第 401 场周赛 Q2 tags: - 数组 - 数学 diff --git a/solution/3100-3199/3179.Find the N-th Value After K Seconds/README_EN.md b/solution/3100-3199/3179.Find the N-th Value After K Seconds/README_EN.md index b3e857325f773..814a4ed70b251 100644 --- a/solution/3100-3199/3179.Find the N-th Value After K Seconds/README_EN.md +++ b/solution/3100-3199/3179.Find the N-th Value After K Seconds/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3179.Find%20the%20N-th%20Value%20After%20K%20Seconds/README_EN.md +rating: 1369 +source: Weekly Contest 401 Q2 tags: - Array - Math diff --git a/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README.md b/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README.md index 9918939680483..5f8735a3c3082 100644 --- a/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README.md +++ b/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3180.Maximum%20Total%20Reward%20Using%20Operations%20I/README.md +rating: 1848 +source: 第 401 场周赛 Q3 tags: - 数组 - 动态规划 diff --git a/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README_EN.md b/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README_EN.md index 7a623d3c43cac..10cd748978f98 100644 --- a/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README_EN.md +++ b/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3180.Maximum%20Total%20Reward%20Using%20Operations%20I/README_EN.md +rating: 1848 +source: Weekly Contest 401 Q3 tags: - Array - Dynamic Programming diff --git a/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README.md b/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README.md index 7d6730b971ae7..be8aeacc8e602 100644 --- a/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README.md +++ b/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3181.Maximum%20Total%20Reward%20Using%20Operations%20II/README.md +rating: 2688 +source: 第 401 场周赛 Q4 tags: - 位运算 - 数组 diff --git a/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README_EN.md b/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README_EN.md index 547d7fc35e082..f745d0c947de1 100644 --- a/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README_EN.md +++ b/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3181.Maximum%20Total%20Reward%20Using%20Operations%20II/README_EN.md +rating: 2688 +source: Weekly Contest 401 Q4 tags: - Bit Manipulation - Array diff --git a/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/README.md b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/README.md new file mode 100644 index 0000000000000..0d03f42164c32 --- /dev/null +++ b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/README.md @@ -0,0 +1,156 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/README.md +--- + + + +# [3189. Minimum Moves to Get a Peaceful Board 🔒](https://leetcode.cn/problems/minimum-moves-to-get-a-peaceful-board) + +[English Version](/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/README_EN.md) + +## 题目描述 + + + +

Given a 2D array rooks of length n, where rooks[i] = [xi, yi] indicates the position of a rook on an n x n chess board. Your task is to move the rooks 1 cell at a time vertically or horizontally (to an adjacent cell) such that the board becomes peaceful.

+ +

A board is peaceful if there is exactly one rook in each row and each column.

+ +

Return the minimum number of moves required to get a peaceful board.

+ +

Note that at no point can there be two rooks in the same cell.

+ +

 

+

Example 1:

+ +
+

Input: rooks = [[0,0],[1,0],[1,1]]

+ +

Output: 3

+ +

Explanation:

+
+ +

Example 2:

+ +
+

Input: rooks = [[0,0],[0,1],[0,2],[0,3]]

+ +

Output: 6

+ +

Explanation:

+
+ +

 

+

Constraints:

+ + + + + +## 解法 + + + +### 方法一:贪心 + +我们可以将所有的车按照横坐标排序,然后将车按顺序分配给每一行,计算每个车到目标位置的距离之和。然后将所有的车按照纵坐标排序,按照同样的方法计算每个车到目标位置的距离之和。最后将两个距离之和相加即为答案。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为车的数量。 + + + +#### Python3 + +```python +class Solution: + def minMoves(self, rooks: List[List[int]]) -> int: + rooks.sort() + ans = sum(abs(x - i) for i, (x, _) in enumerate(rooks)) + rooks.sort(key=lambda x: x[1]) + ans += sum(abs(y - j) for j, (_, y) in enumerate(rooks)) + return ans +``` + +#### Java + +```java +class Solution { + public int minMoves(int[][] rooks) { + Arrays.sort(rooks, (a, b) -> a[0] - b[0]); + int ans = 0; + int n = rooks.length; + for (int i = 0; i < n; ++i) { + ans += Math.abs(rooks[i][0] - i); + } + Arrays.sort(rooks, (a, b) -> a[1] - b[1]); + for (int j = 0; j < n; ++j) { + ans += Math.abs(rooks[j][1] - j); + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int minMoves(vector>& rooks) { + sort(rooks.begin(), rooks.end()); + int ans = 0; + int n = rooks.size(); + for (int i = 0; i < n; ++i) { + ans += abs(rooks[i][0] - i); + } + sort(rooks.begin(), rooks.end(), [](const vector& a, const vector& b) { + return a[1] < b[1]; + }); + for (int j = 0; j < n; ++j) { + ans += abs(rooks[j][1] - j); + } + return ans; + } +}; +``` + +#### Go + +```go +func minMoves(rooks [][]int) (ans int) { + sort.Slice(rooks, func(i, j int) bool { return rooks[i][0] < rooks[j][0] }) + for i, row := range rooks { + ans += int(math.Abs(float64(row[0] - i))) + } + sort.Slice(rooks, func(i, j int) bool { return rooks[i][1] < rooks[j][1] }) + for j, col := range rooks { + ans += int(math.Abs(float64(col[1] - j))) + } + return +} +``` + +#### TypeScript + +```ts +function minMoves(rooks: number[][]): number { + rooks.sort((a, b) => a[0] - b[0]); + let ans = rooks.reduce((sum, rook, i) => sum + Math.abs(rook[0] - i), 0); + rooks.sort((a, b) => a[1] - b[1]); + ans += rooks.reduce((sum, rook, j) => sum + Math.abs(rook[1] - j), 0); + return ans; +} +``` + + + + + + diff --git a/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/README_EN.md b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/README_EN.md new file mode 100644 index 0000000000000..9a7a31cd79739 --- /dev/null +++ b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/README_EN.md @@ -0,0 +1,156 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/README_EN.md +--- + + + +# [3189. Minimum Moves to Get a Peaceful Board 🔒](https://leetcode.com/problems/minimum-moves-to-get-a-peaceful-board) + +[中文文档](/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/README.md) + +## Description + + + +

Given a 2D array rooks of length n, where rooks[i] = [xi, yi] indicates the position of a rook on an n x n chess board. Your task is to move the rooks 1 cell at a time vertically or horizontally (to an adjacent cell) such that the board becomes peaceful.

+ +

A board is peaceful if there is exactly one rook in each row and each column.

+ +

Return the minimum number of moves required to get a peaceful board.

+ +

Note that at no point can there be two rooks in the same cell.

+ +

 

+

Example 1:

+ +
+

Input: rooks = [[0,0],[1,0],[1,1]]

+ +

Output: 3

+ +

Explanation:

+
+ +

Example 2:

+ +
+

Input: rooks = [[0,0],[0,1],[0,2],[0,3]]

+ +

Output: 6

+ +

Explanation:

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n == rooks.length <= 500
  • +
  • 0 <= xi, yi <= n - 1
  • +
  • The input is generated such that there are no 2 rooks in the same cell.
  • +
+ + + +## Solutions + + + +### Solution 1: Greedy Algorithm + +We can sort all the cars by their x-coordinates, and then allocate the cars to each row in order, calculating the sum of distances from each car to its target position. Then, sort all the cars by their y-coordinates and use the same method to calculate the sum of distances from each car to its target position. Finally, the sum of these two distances is the answer. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of cars. + + + +#### Python3 + +```python +class Solution: + def minMoves(self, rooks: List[List[int]]) -> int: + rooks.sort() + ans = sum(abs(x - i) for i, (x, _) in enumerate(rooks)) + rooks.sort(key=lambda x: x[1]) + ans += sum(abs(y - j) for j, (_, y) in enumerate(rooks)) + return ans +``` + +#### Java + +```java +class Solution { + public int minMoves(int[][] rooks) { + Arrays.sort(rooks, (a, b) -> a[0] - b[0]); + int ans = 0; + int n = rooks.length; + for (int i = 0; i < n; ++i) { + ans += Math.abs(rooks[i][0] - i); + } + Arrays.sort(rooks, (a, b) -> a[1] - b[1]); + for (int j = 0; j < n; ++j) { + ans += Math.abs(rooks[j][1] - j); + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int minMoves(vector>& rooks) { + sort(rooks.begin(), rooks.end()); + int ans = 0; + int n = rooks.size(); + for (int i = 0; i < n; ++i) { + ans += abs(rooks[i][0] - i); + } + sort(rooks.begin(), rooks.end(), [](const vector& a, const vector& b) { + return a[1] < b[1]; + }); + for (int j = 0; j < n; ++j) { + ans += abs(rooks[j][1] - j); + } + return ans; + } +}; +``` + +#### Go + +```go +func minMoves(rooks [][]int) (ans int) { + sort.Slice(rooks, func(i, j int) bool { return rooks[i][0] < rooks[j][0] }) + for i, row := range rooks { + ans += int(math.Abs(float64(row[0] - i))) + } + sort.Slice(rooks, func(i, j int) bool { return rooks[i][1] < rooks[j][1] }) + for j, col := range rooks { + ans += int(math.Abs(float64(col[1] - j))) + } + return +} +``` + +#### TypeScript + +```ts +function minMoves(rooks: number[][]): number { + rooks.sort((a, b) => a[0] - b[0]); + let ans = rooks.reduce((sum, rook, i) => sum + Math.abs(rook[0] - i), 0); + rooks.sort((a, b) => a[1] - b[1]); + ans += rooks.reduce((sum, rook, j) => sum + Math.abs(rook[1] - j), 0); + return ans; +} +``` + + + + + + diff --git a/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.cpp b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.cpp new file mode 100644 index 0000000000000..d47f7200e331c --- /dev/null +++ b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int minMoves(vector>& rooks) { + sort(rooks.begin(), rooks.end()); + int ans = 0; + int n = rooks.size(); + for (int i = 0; i < n; ++i) { + ans += abs(rooks[i][0] - i); + } + sort(rooks.begin(), rooks.end(), [](const vector& a, const vector& b) { + return a[1] < b[1]; + }); + for (int j = 0; j < n; ++j) { + ans += abs(rooks[j][1] - j); + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.go b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.go new file mode 100644 index 0000000000000..9eb9cbc84e1df --- /dev/null +++ b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.go @@ -0,0 +1,11 @@ +func minMoves(rooks [][]int) (ans int) { + sort.Slice(rooks, func(i, j int) bool { return rooks[i][0] < rooks[j][0] }) + for i, row := range rooks { + ans += int(math.Abs(float64(row[0] - i))) + } + sort.Slice(rooks, func(i, j int) bool { return rooks[i][1] < rooks[j][1] }) + for j, col := range rooks { + ans += int(math.Abs(float64(col[1] - j))) + } + return +} \ No newline at end of file diff --git a/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.java b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.java new file mode 100644 index 0000000000000..a44c223b8048e --- /dev/null +++ b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.java @@ -0,0 +1,15 @@ +class Solution { + public int minMoves(int[][] rooks) { + Arrays.sort(rooks, (a, b) -> a[0] - b[0]); + int ans = 0; + int n = rooks.length; + for (int i = 0; i < n; ++i) { + ans += Math.abs(rooks[i][0] - i); + } + Arrays.sort(rooks, (a, b) -> a[1] - b[1]); + for (int j = 0; j < n; ++j) { + ans += Math.abs(rooks[j][1] - j); + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.py b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.py new file mode 100644 index 0000000000000..c2f83374951f8 --- /dev/null +++ b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.py @@ -0,0 +1,7 @@ +class Solution: + def minMoves(self, rooks: List[List[int]]) -> int: + rooks.sort() + ans = sum(abs(x - i) for i, (x, _) in enumerate(rooks)) + rooks.sort(key=lambda x: x[1]) + ans += sum(abs(y - j) for j, (_, y) in enumerate(rooks)) + return ans diff --git a/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.ts b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.ts new file mode 100644 index 0000000000000..873d26f452ace --- /dev/null +++ b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/Solution.ts @@ -0,0 +1,7 @@ +function minMoves(rooks: number[][]): number { + rooks.sort((a, b) => a[0] - b[0]); + let ans = rooks.reduce((sum, rook, i) => sum + Math.abs(rook[0] - i), 0); + rooks.sort((a, b) => a[1] - b[1]); + ans += rooks.reduce((sum, rook, j) => sum + Math.abs(rook[1] - j), 0); + return ans; +} diff --git a/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/images/ex1-edited.gif b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/images/ex1-edited.gif new file mode 100644 index 0000000000000..952d5425c5204 Binary files /dev/null and b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/images/ex1-edited.gif differ diff --git a/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/images/ex2-edited.gif b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/images/ex2-edited.gif new file mode 100644 index 0000000000000..b5b73568cd072 Binary files /dev/null and b/solution/3100-3199/3189.Minimum Moves to Get a Peaceful Board/images/ex2-edited.gif differ diff --git a/solution/README.md b/solution/README.md index f6ecd0e1625dd..40c8c94ffa0e1 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3199,6 +3199,7 @@ | 3186 | [施咒的最大总伤害](/solution/3100-3199/3186.Maximum%20Total%20Damage%20With%20Spell%20Casting/README.md) | `数组`,`哈希表`,`双指针`,`二分查找`,`动态规划`,`计数`,`排序` | 中等 | 第 402 场周赛 | | 3187 | [数组中的峰值](/solution/3100-3199/3187.Peaks%20in%20Array/README.md) | `树状数组`,`线段树`,`数组` | 困难 | 第 402 场周赛 | | 3188 | [查找得分最高的学生 II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md) | | 困难 | 🔒 | +| 3189 | [Minimum Moves to Get a Peaceful Board](/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 6b8502fb2e13f..047a9813edcfe 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3197,6 +3197,7 @@ Press Control + F(or Command + F on | 3186 | [Maximum Total Damage With Spell Casting](/solution/3100-3199/3186.Maximum%20Total%20Damage%20With%20Spell%20Casting/README_EN.md) | `Array`,`Hash Table`,`Two Pointers`,`Binary Search`,`Dynamic Programming`,`Counting`,`Sorting` | Medium | Weekly Contest 402 | | 3187 | [Peaks in Array](/solution/3100-3199/3187.Peaks%20in%20Array/README_EN.md) | `Binary Indexed Tree`,`Segment Tree`,`Array` | Hard | Weekly Contest 402 | | 3188 | [Find Top Scoring Students II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README_EN.md) | | Hard | 🔒 | +| 3189 | [Minimum Moves to Get a Peaceful Board](/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/README_EN.md) | | Medium | 🔒 | ## Copyright