diff --git a/solution/3600-3699/3633.Earliest Finish Time for Land and Water Rides I/README.md b/solution/3600-3699/3633.Earliest Finish Time for Land and Water Rides I/README.md index 12d29540153b6..4f6e568d439c2 100644 --- a/solution/3600-3699/3633.Earliest Finish Time for Land and Water Rides I/README.md +++ b/solution/3600-3699/3633.Earliest Finish Time for Land and Water Rides I/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3633.Earliest%20Finish%20Time%20for%20Land%20and%20Water%20Rides%20I/README.md +rating: 1342 +source: 第 162 场双周赛 Q1 --- diff --git a/solution/3600-3699/3633.Earliest Finish Time for Land and Water Rides I/README_EN.md b/solution/3600-3699/3633.Earliest Finish Time for Land and Water Rides I/README_EN.md index 3aa6c3401305c..38cceb7d73a45 100644 --- a/solution/3600-3699/3633.Earliest Finish Time for Land and Water Rides I/README_EN.md +++ b/solution/3600-3699/3633.Earliest Finish Time for Land and Water Rides I/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3633.Earliest%20Finish%20Time%20for%20Land%20and%20Water%20Rides%20I/README_EN.md +rating: 1342 +source: Biweekly Contest 162 Q1 --- diff --git a/solution/3600-3699/3634.Minimum Removals to Balance Array/README.md b/solution/3600-3699/3634.Minimum Removals to Balance Array/README.md index 9477d7c3074f8..8e5089754690f 100644 --- a/solution/3600-3699/3634.Minimum Removals to Balance Array/README.md +++ b/solution/3600-3699/3634.Minimum Removals to Balance Array/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3634.Minimum%20Removals%20to%20Balance%20Array/README.md +rating: 1453 +source: 第 162 场双周赛 Q2 --- diff --git a/solution/3600-3699/3634.Minimum Removals to Balance Array/README_EN.md b/solution/3600-3699/3634.Minimum Removals to Balance Array/README_EN.md index dfa0844b84f67..16af384406b2b 100644 --- a/solution/3600-3699/3634.Minimum Removals to Balance Array/README_EN.md +++ b/solution/3600-3699/3634.Minimum Removals to Balance Array/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3634.Minimum%20Removals%20to%20Balance%20Array/README_EN.md +rating: 1453 +source: Biweekly Contest 162 Q2 --- diff --git a/solution/3600-3699/3635.Earliest Finish Time for Land and Water Rides II/README.md b/solution/3600-3699/3635.Earliest Finish Time for Land and Water Rides II/README.md index 6ed3932715dea..aff643c82965c 100644 --- a/solution/3600-3699/3635.Earliest Finish Time for Land and Water Rides II/README.md +++ b/solution/3600-3699/3635.Earliest Finish Time for Land and Water Rides II/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3635.Earliest%20Finish%20Time%20for%20Land%20and%20Water%20Rides%20II/README.md +rating: 1869 +source: 第 162 场双周赛 Q3 --- diff --git a/solution/3600-3699/3635.Earliest Finish Time for Land and Water Rides II/README_EN.md b/solution/3600-3699/3635.Earliest Finish Time for Land and Water Rides II/README_EN.md index c7bc7cb5fae0d..8e8cd77c0e966 100644 --- a/solution/3600-3699/3635.Earliest Finish Time for Land and Water Rides II/README_EN.md +++ b/solution/3600-3699/3635.Earliest Finish Time for Land and Water Rides II/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3635.Earliest%20Finish%20Time%20for%20Land%20and%20Water%20Rides%20II/README_EN.md +rating: 1869 +source: Biweekly Contest 162 Q3 --- diff --git a/solution/3600-3699/3636.Threshold Majority Queries/README.md b/solution/3600-3699/3636.Threshold Majority Queries/README.md index a91adf357b44f..997f601d14c22 100644 --- a/solution/3600-3699/3636.Threshold Majority Queries/README.md +++ b/solution/3600-3699/3636.Threshold Majority Queries/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3636.Threshold%20Majority%20Queries/README.md +rating: 2451 +source: 第 162 场双周赛 Q4 --- diff --git a/solution/3600-3699/3636.Threshold Majority Queries/README_EN.md b/solution/3600-3699/3636.Threshold Majority Queries/README_EN.md index 11d83afd6d682..6cf14892a2661 100644 --- a/solution/3600-3699/3636.Threshold Majority Queries/README_EN.md +++ b/solution/3600-3699/3636.Threshold Majority Queries/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3636.Threshold%20Majority%20Queries/README_EN.md +rating: 2451 +source: Biweekly Contest 162 Q4 --- diff --git a/solution/3600-3699/3637.Trionic Array I/README.md b/solution/3600-3699/3637.Trionic Array I/README.md index 835b26d047c99..ffed0be6a6dfd 100644 --- a/solution/3600-3699/3637.Trionic Array I/README.md +++ b/solution/3600-3699/3637.Trionic Array I/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3637.Trionic%20Array%20I/README.md +rating: 1263 +source: 第 461 场周赛 Q1 --- diff --git a/solution/3600-3699/3637.Trionic Array I/README_EN.md b/solution/3600-3699/3637.Trionic Array I/README_EN.md index 57f3219882e2c..36e016c8c1399 100644 --- a/solution/3600-3699/3637.Trionic Array I/README_EN.md +++ b/solution/3600-3699/3637.Trionic Array I/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3637.Trionic%20Array%20I/README_EN.md +rating: 1263 +source: Weekly Contest 461 Q1 --- diff --git a/solution/3600-3699/3638.Maximum Balanced Shipments/README.md b/solution/3600-3699/3638.Maximum Balanced Shipments/README.md index 3c06b5821b1c5..6cbfca4007fcf 100644 --- a/solution/3600-3699/3638.Maximum Balanced Shipments/README.md +++ b/solution/3600-3699/3638.Maximum Balanced Shipments/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3638.Maximum%20Balanced%20Shipments/README.md +rating: 1463 +source: 第 461 场周赛 Q2 --- diff --git a/solution/3600-3699/3638.Maximum Balanced Shipments/README_EN.md b/solution/3600-3699/3638.Maximum Balanced Shipments/README_EN.md index a549ac9a93046..6337a15767728 100644 --- a/solution/3600-3699/3638.Maximum Balanced Shipments/README_EN.md +++ b/solution/3600-3699/3638.Maximum Balanced Shipments/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3638.Maximum%20Balanced%20Shipments/README_EN.md +rating: 1463 +source: Weekly Contest 461 Q2 --- diff --git a/solution/3600-3699/3639.Minimum Time to Activate String/README.md b/solution/3600-3699/3639.Minimum Time to Activate String/README.md index 4a7d30b676b67..a4a8f5125019d 100644 --- a/solution/3600-3699/3639.Minimum Time to Activate String/README.md +++ b/solution/3600-3699/3639.Minimum Time to Activate String/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3639.Minimum%20Time%20to%20Activate%20String/README.md +rating: 1853 +source: 第 461 场周赛 Q3 --- diff --git a/solution/3600-3699/3639.Minimum Time to Activate String/README_EN.md b/solution/3600-3699/3639.Minimum Time to Activate String/README_EN.md index 9078f0b11529c..312bff28043a7 100644 --- a/solution/3600-3699/3639.Minimum Time to Activate String/README_EN.md +++ b/solution/3600-3699/3639.Minimum Time to Activate String/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3639.Minimum%20Time%20to%20Activate%20String/README_EN.md +rating: 1853 +source: Weekly Contest 461 Q3 --- diff --git a/solution/3600-3699/3640.Trionic Array II/README.md b/solution/3600-3699/3640.Trionic Array II/README.md index f2c8f64203aca..018d167451ad4 100644 --- a/solution/3600-3699/3640.Trionic Array II/README.md +++ b/solution/3600-3699/3640.Trionic Array II/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3640.Trionic%20Array%20II/README.md +rating: 2277 +source: 第 461 场周赛 Q4 --- diff --git a/solution/3600-3699/3640.Trionic Array II/README_EN.md b/solution/3600-3699/3640.Trionic Array II/README_EN.md index aba68cd5724d3..d781c7ddd4bac 100644 --- a/solution/3600-3699/3640.Trionic Array II/README_EN.md +++ b/solution/3600-3699/3640.Trionic Array II/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3640.Trionic%20Array%20II/README_EN.md +rating: 2277 +source: Weekly Contest 461 Q4 --- diff --git a/solution/3600-3699/3641.Longest Semi-Repeating Subarray/README.md b/solution/3600-3699/3641.Longest Semi-Repeating Subarray/README.md new file mode 100644 index 0000000000000..a2af3ddce3528 --- /dev/null +++ b/solution/3600-3699/3641.Longest Semi-Repeating Subarray/README.md @@ -0,0 +1,241 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3641.Longest%20Semi-Repeating%20Subarray/README.md +--- + + + +# [3641. 最长半重复子数组 🔒](https://leetcode.cn/problems/longest-semi-repeating-subarray) + +[English Version](/solution/3600-3699/3641.Longest%20Semi-Repeating%20Subarray/README_EN.md) + +## 题目描述 + + + +
给定一个长度为 n
的整数数组 nums
和一个整数 k
。
半重复 子数组是指最多有 k
个元素重复(即出现超过一次)的连续子数组。
返回 nums
中最长 半重复 子数组的长度。
+ +
示例 1:
+ +输入:nums = [1,2,3,1,2,3,4], k = 2
+ +输出:6
+ +解释:
+ +最长的半重复子数组是 [2, 3, 1, 2, 3, 4]
,其中有 2 个重复元素(2 和 3)。
示例 2:
+ +输入:nums = [1,1,1,1,1], k = 4
+ +输出:5
+ +解释:
+ +最长的半重复子数组是 [1, 1, 1, 1, 1]
,其中只有 1 个重复元素(1)。
示例 3:
+ +输入:nums = [1,1,1,1,1], k = 0
+ +输出:1
+ +解释:
+ +最长的半重复子数组是 [1]
,其中没有重复元素。
+ +
提示:
+ +1 <= nums.length <= 105
1 <= nums[i] <= 105
0 <= k <= nums.length
You are given an integer array nums
of length n
and an integer k
.
A semi‑repeating subarray is a contiguous subarray in which at most k
elements repeat (i.e., appear more than once).
Return the length of the longest semi‑repeating subarray in nums
.
+
Example 1:
+ +Input: nums = [1,2,3,1,2,3,4], k = 2
+ +Output: 6
+ +Explanation:
+ +The longest semi-repeating subarray is [2, 3, 1, 2, 3, 4]
, which has two repeating elements (2 and 3).
Example 2:
+ +Input: nums = [1,1,1,1,1], k = 4
+ +Output: 5
+ +Explanation:
+ +The longest semi-repeating subarray is [1, 1, 1, 1, 1]
, which has only one repeating element (1).
Example 3:
+ +Input: nums = [1,1,1,1,1], k = 0
+ +Output: 1
+ +Explanation:
+ +The longest semi-repeating subarray is [1]
, which has no repeating elements.
+
Constraints:
+ +1 <= nums.length <= 105
1 <= nums[i] <= 105
0 <= k <= nums.length
+ + +
FOR TESTING ONLY. WILL BE DELETED LATER.
+// Model solution has runtime of O(n log n), O(n*n) and above should TLE. + ++# Bromelia + +import sys +import random, json, string +import math +import datetime +from collections import defaultdict +ri = random.randint + +MAX_N = 100_000 +MAX_VAL = 100_000 + +def randomString(n, allowed): + return ''.join(random.choices(allowed, k=n)) + +def randomUnique(x, y, n): + return random.sample(range(x, y + 1), n) + +def randomArray(x, y, n): + return [ri(x, y) for _ in range(n)] + +def shuffle(arr): + random.shuffle(arr) + return arr + +def pr(a): + file.write(str(a).replace(" ", "").replace("\'", "\"").replace("\"null\"", "null") + '\n') + +def prstr(a): + pr("\"" + a + "\"") + + +def prtc(tc): + nums, k = tc + pr(nums) + pr(k) + +def examples(): + yield ([1, 2, 3, 1, 2, 3, 4], 2) + yield ([1, 1, 1, 1, 1], 4) + yield ([1, 1, 1, 1, 1], 0) + +def smallCases(): + yield ([MAX_VAL], 0) + yield ([MAX_VAL], 1) + + for len in range(1, 3 + 1): + nums = [0] * len + + def recursiveGenerate(idx: int): + if idx == len: + for k in range(0, len + 1): + yield (nums, k) + else: + for nextElement in range(1, len + 1): + nums[idx] = nextElement + yield from recursiveGenerate(idx + 1) + + yield from recursiveGenerate(0) + +def randomCases(): + params = [ + ( 4, 20, 10, 400), + ( 21, 2000, 1000, 100), + (MAX_N, MAX_N, 10, 2), + (MAX_N, MAX_N, 500, 2), + (MAX_N, MAX_N, MAX_VAL, 2), + ] + for minLen, maxLen, maxVal, testCount in params: + for _ in range(testCount): + len = ri(minLen, maxLen) + k = ri(1, len) + + nums = [0] * len + for i in range(len): + nums[i] = ri(1, maxVal) + + yield (nums, k) + +def cornerCases(): + yield ([MAX_VAL] * MAX_N, 0) + yield ([MAX_VAL] * MAX_N, MAX_N) + yield ([i for i in range(1, MAX_N + 1)], 0) + yield ([i for i in range(1, MAX_N + 1)], MAX_N) + yield ([i // 2 + 1 for i in range(MAX_N)], MAX_N // 2 - 1) + yield ([i % (MAX_N // 2) + 1 for i in range(MAX_N)], MAX_N // 2 - 1) + + +with open('test.txt', 'w') as file: + random.seed(0) + for tc in examples(): prtc(tc) + for tc in smallCases(): prtc(tc) + for tc in sorted(list(randomCases()), key = lambda x: len(x[0])): prtc(tc) + for tc in cornerCases(): prtc(tc) ++