LeetCode 题解工作台

让所有学生保持开心的分组方法数

给你一个下标从 0 开始、长度为 n 的整数数组 nums ,其中 n 是班级中学生的总数。班主任希望能够在让所有学生保持开心的情况下选出一组学生: 如果能够满足下述两个条件之一,则认为第 i 位学生将会保持开心: 这位学生被选中,并且被选中的学生人数 严格大于 nums[i] 。 这位学生没有被选…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·排序

bolt

答案摘要

假设选出了 个学生,那么以下情况成立: - 如果 $nums[i] = k$,那么不存在分组方法;

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·排序 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始、长度为 n 的整数数组 nums ,其中 n 是班级中学生的总数。班主任希望能够在让所有学生保持开心的情况下选出一组学生:

如果能够满足下述两个条件之一,则认为第 i 位学生将会保持开心:

  • 这位学生被选中,并且被选中的学生人数 严格大于 nums[i]
  • 这位学生没有被选中,并且被选中的学生人数 严格小于 nums[i]

返回能够满足让所有学生保持开心的分组方法的数目。

 

示例 1:

输入:nums = [1,1]
输出:2
解释:
有两种可行的方法:
班主任没有选中学生。
班主任选中所有学生形成一组。 
如果班主任仅选中一个学生来完成分组,那么两个学生都无法保持开心。因此,仅存在两种可行的方法。

示例 2:

输入:nums = [6,0,3,3,6,7,2,7]
输出:3
解释:
存在三种可行的方法:
班主任选中下标为 1 的学生形成一组。
班主任选中下标为 1、2、3、6 的学生形成一组。
班主任选中所有学生形成一组。 

 

提示:

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

解题思路

方法一:排序 + 枚举

假设选出了 kk 个学生,那么以下情况成立:

  • 如果 nums[i]=knums[i] = k,那么不存在分组方法;
  • 如果 nums[i]>knums[i] \gt k,那么学生 ii 不被选中;
  • 如果 nums[i]<knums[i] \lt k,那么学生 ii 被选中。

因此,被选中的学生一定是排序后的 numsnums 数组中的前 kk 个元素。

我们在 [0,..n][0,..n] 范围内枚举 kk,对于当前选出的学生人数 ii,我们可以得到组内最大的学生编号 i1i-1,数字为 nums[i1]nums[i-1]。如果 i>0i \gt 0 并且 nums[i1]inums[i-1] \ge i,那么不存在分组方法;如果 i<ni \lt n 并且 nums[i]inums[i] \le i,那么不存在分组方法。否则,存在分组方法,答案加一。

枚举结束后,返回答案即可。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def countWays(self, nums: List[int]) -> int:
        nums.sort()
        n = len(nums)
        ans = 0
        for i in range(n + 1):
            if i and nums[i - 1] >= i:
                continue
            if i < n and nums[i] <= i:
                continue
            ans += 1
        return ans
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Look for an understanding of how sorting and grouping can simplify the problem.

  • question_mark

    Check if the candidate is able to identify the key observation about grouping students with the same happiness value.

  • question_mark

    Assess whether the candidate can correctly evaluate the number of ways to form valid student groups.

warning

常见陷阱

外企场景
  • error

    Failing to correctly group students by their happiness value and considering invalid groupings.

  • error

    Overcomplicating the problem by missing the key observation that students with the same value must either be included together or excluded.

  • error

    Misunderstanding the time complexity and assuming the problem can be solved in linear time without sorting.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Change the problem to allow partial selections of students, introducing more complexity in the valid groupings.

  • arrow_right_alt

    Alter the input such that students have a wider range of happiness values, which increases the number of possible groupings.

  • arrow_right_alt

    Introduce constraints that limit the size of groups, requiring a more efficient solution to handle large inputs.

help

常见问题

外企场景

让所有学生保持开心的分组方法数题解:数组·排序 | LeetCode #2860 中等