diff --git a/solution/2500-2599/2576.Find the Maximum Number of Marked Indices/README.md b/solution/2500-2599/2576.Find the Maximum Number of Marked Indices/README.md index a4b5345d11e7d..e38954d0c6bec 100644 --- a/solution/2500-2599/2576.Find the Maximum Number of Marked Indices/README.md +++ b/solution/2500-2599/2576.Find the Maximum Number of Marked Indices/README.md @@ -78,9 +78,11 @@ tags: ### 方法一:贪心 + 双指针 -为了将下标尽可能多地标记,我们可以将数组 `nums` 排序,然后从左到右遍历数组,对于每个下标 $i$,我们在数组的右半部分找到第一个满足 $2 \times nums[i] \leq nums[j]$ 的下标 $j$,然后标记下标 $i$ 和 $j$。继续遍历下一个下标 $i$。当我们遍历完数组的右半部分时,说明标记已经完成,此时标记的下标数目即为答案。 +根据题目描述,题目最多产生 $n / 2$ 组标记,其中 $n$ 为数组 $\textit{nums}$ 的长度。 -时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `nums` 的长度。 +为了将下标尽可能多地标记,我们可以将数组 $\textit{nums}$ 排序,接下来,我们遍历右半部分的每个元素 $\textit{nums}[j]$,用一个指针 $\textit{i}$ 指向左半部分的最小元素,如果 $\textit{nums}[i] \times 2 \leq \textit{nums}[j]$,则可以标记下标 $\textit{i}$ 和 $\textit{j}$,我们将 $\textit{i}$ 向右移动一个位置。继续遍历右半部分的元素,直到到达数组的末尾。此时,我们可以标记的下标数目为 $\textit{i} \times 2$。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 @@ -90,16 +92,11 @@ tags: class Solution: def maxNumOfMarkedIndices(self, nums: List[int]) -> int: nums.sort() - n = len(nums) - i, j = 0, (n + 1) // 2 - ans = 0 - while j < n: - while j < n and nums[i] * 2 > nums[j]: - j += 1 - if j < n: - ans += 2 - i, j = i + 1, j + 1 - return ans + i, n = 0, len(nums) + for x in nums[(n + 1) // 2 :]: + if nums[i] * 2 <= x: + i += 1 + return i * 2 ``` #### Java @@ -108,17 +105,13 @@ class Solution: class Solution { public int maxNumOfMarkedIndices(int[] nums) { Arrays.sort(nums); - int n = nums.length; - int ans = 0; - for (int i = 0, j = (n + 1) / 2; j < n; ++i, ++j) { - while (j < n && nums[i] * 2 > nums[j]) { - ++j; - } - if (j < n) { - ans += 2; + int i = 0, n = nums.length; + for (int j = (n + 1) / 2; j < n; ++j) { + if (nums[i] * 2 <= nums[j]) { + ++i; } } - return ans; + return i * 2; } } ``` @@ -129,18 +122,14 @@ class Solution { class Solution { public: int maxNumOfMarkedIndices(vector& nums) { - sort(nums.begin(), nums.end()); - int n = nums.size(); - int ans = 0; - for (int i = 0, j = (n + 1) / 2; j < n; ++i, ++j) { - while (j < n && nums[i] * 2 > nums[j]) { - ++j; - } - if (j < n) { - ans += 2; + ranges::sort(nums); + int i = 0, n = nums.size(); + for (int j = (n + 1) / 2; j < n; ++j) { + if (nums[i] * 2 <= nums[j]) { + ++i; } } - return ans; + return i * 2; } }; ``` @@ -150,16 +139,13 @@ public: ```go func maxNumOfMarkedIndices(nums []int) (ans int) { sort.Ints(nums) - n := len(nums) - for i, j := 0, (n+1)/2; j < n; i, j = i+1, j+1 { - for j < n && nums[i]*2 > nums[j] { - j++ - } - if j < n { - ans += 2 + i, n := 0, len(nums) + for _, x := range nums[(n+1)/2:] { + if nums[i]*2 <= x { + i++ } } - return + return i * 2 } ``` @@ -169,16 +155,31 @@ func maxNumOfMarkedIndices(nums []int) (ans int) { function maxNumOfMarkedIndices(nums: number[]): number { nums.sort((a, b) => a - b); const n = nums.length; - let ans = 0; - for (let i = 0, j = Math.floor((n + 1) / 2); j < n; ++i, ++j) { - while (j < n && nums[i] * 2 > nums[j]) { - ++j; + let i = 0; + for (let j = (n + 1) >> 1; j < n; ++j) { + if (nums[i] * 2 <= nums[j]) { + ++i; } - if (j < n) { - ans += 2; + } + return i * 2; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn max_num_of_marked_indices(mut nums: Vec) -> i32 { + nums.sort(); + let mut i = 0; + let n = nums.len(); + for j in (n + 1) / 2..n { + if nums[i] * 2 <= nums[j] { + i += 1; + } } + (i * 2) as i32 } - return ans; } ``` diff --git a/solution/2500-2599/2576.Find the Maximum Number of Marked Indices/README_EN.md b/solution/2500-2599/2576.Find the Maximum Number of Marked Indices/README_EN.md index 34bf3bd91bfe1..ff328aff8910e 100644 --- a/solution/2500-2599/2576.Find the Maximum Number of Marked Indices/README_EN.md +++ b/solution/2500-2599/2576.Find the Maximum Number of Marked Indices/README_EN.md @@ -70,7 +70,7 @@ Since there is no other operation, the answer is 4.

 

-