LeetCode 题解工作台

求出最多标记下标

给你一个下标从 0 开始的整数数组 nums 。 一开始,所有下标都没有被标记。你可以执行以下操作任意次: 选择两个 互不相同且未标记 的下标 i 和 j ,满足 2 * nums[i] ,标记下标 i 和 j 。 请你执行上述操作任意次,返回 nums 中最多可以标记的下标数目。 示例 1: 输入…

category

5

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 二分·搜索·答案·空间

bolt

答案摘要

根据题目描述,题目最多产生 $n / 2$ 组标记,其中 为数组 的长度。 为了将下标尽可能多地标记,我们可以将数组 排序,接下来,我们遍历右半部分的每个元素 ,用一个指针 指向左半部分的最小元素,如果 $\textit{nums}[i] \times 2 \leq \textit{nums}[j]$,则可以标记下标 和 ,我们将 向右移动一个位置。继续遍历右半部分的元素,直到到达数组…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 二分·搜索·答案·空间 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始的整数数组 nums 。

一开始,所有下标都没有被标记。你可以执行以下操作任意次:

  • 选择两个 互不相同且未标记 的下标 i 和 j ,满足 2 * nums[i] <= nums[j] ,标记下标 i 和 j 。

请你执行上述操作任意次,返回 nums 中最多可以标记的下标数目。

 

示例 1:

输入:nums = [3,5,2,4]
输出:2
解释:第一次操作中,选择 i = 2 和 j = 1 ,操作可以执行的原因是 2 * nums[2] <= nums[1] ,标记下标 2 和 1 。
没有其他更多可执行的操作,所以答案为 2 。

示例 2:

输入:nums = [9,2,5,4]
输出:4
解释:第一次操作中,选择 i = 3 和 j = 0 ,操作可以执行的原因是 2 * nums[3] <= nums[0] ,标记下标 3 和 0 。
第二次操作中,选择 i = 1 和 j = 2 ,操作可以执行的原因是 2 * nums[1] <= nums[2] ,标记下标 1 和 2 。
没有其他更多可执行的操作,所以答案为 4 。

示例 3:

输入:nums = [7,6,8]
输出:0
解释:没有任何可以执行的操作,所以答案为 0 。

 

提示:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109
lightbulb

解题思路

方法一:贪心 + 双指针

根据题目描述,题目最多产生 n/2n / 2 组标记,其中 nn 为数组 nums\textit{nums} 的长度。

为了将下标尽可能多地标记,我们可以将数组 nums\textit{nums} 排序,接下来,我们遍历右半部分的每个元素 nums[j]\textit{nums}[j],用一个指针 i\textit{i} 指向左半部分的最小元素,如果 nums[i]×2nums[j]\textit{nums}[i] \times 2 \leq \textit{nums}[j],则可以标记下标 i\textit{i}j\textit{j},我们将 i\textit{i} 向右移动一个位置。继续遍历右半部分的元素,直到到达数组的末尾。此时,我们可以标记的下标数目为 i×2\textit{i} \times 2

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(logn)O(\log n)。其中 nn 为数组 nums\textit{nums} 的长度。

1
2
3
4
5
6
7
8
9
class Solution:
    def maxNumOfMarkedIndices(self, nums: List[int]) -> int:
        nums.sort()
        i, n = 0, len(nums)
        for x in nums[(n + 1) // 2 :]:
            if nums[i] * 2 <= x:
                i += 1
        return i * 2
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Focus on pairing strategy and feasibility check for k operations.

  • question_mark

    Check how candidates apply binary search to maximize marked indices.

  • question_mark

    Listen for explanation of why greedy two-pointer pairing guarantees optimal answer.

warning

常见陷阱

外企场景
  • error

    Failing to sort nums first, causing incorrect pairings.

  • error

    Incorrectly pairing indices, violating i < j constraint or the 2*nums[i] <= nums[j] rule.

  • error

    Not using binary search and trying brute-force, which is too slow for large arrays.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Change condition to 3 * nums[i] <= nums[j] and maximize marked indices.

  • arrow_right_alt

    Restrict indices to be consecutive, altering the two-pointer strategy.

  • arrow_right_alt

    Compute maximum marked indices but return the specific pairs chosen.

help

常见问题

外企场景

求出最多标记下标题解:二分·搜索·答案·空间 | LeetCode #2576 中等