150强访谈简单

顺手牌

“直手牌”问题的详细指南和 Python 实现。

问题陈述

简单

Alice 有一定数量的卡片,她想将卡片重新排列成组,以便每个组的大小为 groupSize,并由 groupSize 连续的卡片组成。给定一个整数数组 hand,其中 hand[i] 是第 i 张卡片上写的值和一个整数 groupSize,如果她可以重新排列卡片,则返回 True,否则返回 False。

编写一个函数 isNStraightHand(hand: List[int], groupSize: int) -> bool

约束条件
  • 1 <= len(hand) <= 10^4
  • 0 <= hand[i] <= 10^9
  • 1 <= groupSize <= len(hand)

示例

Example 1
Input
hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
Output
True
Explanation

[1,2,3], [2,3,4], [6,7,8] are consecutive groups of 3.

Example 2
Input
hand = [1,2,3,4,5], groupSize = 4
Output
False
Explanation

Cannot rearrange cards into consecutive groups of 4.

Need a Hint?
考虑使用特定于贪婪的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在编辑器中打开
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推荐的 Python 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。