Top 150 InterviewЛегко

Hand of Straights

Detailed guide and Python implementation for the 'Hand of Straights' problem.

Постановка задачи

Легко

Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards. Given an integer array hand where hand[i] is the value written on the ith card and an integer groupSize, return True if she can rearrange the cards, or False otherwise.

Write a function 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?
Consider using Greedy-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Готовы решить?

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

Расширьте свои знания с помощью соответствующих интерактивных руководств, шпаргалок и сравнений кода.

Учебник по Python

Python Try/Except и обработка ошибок

Предотвратите сбой ваших скриптов Python. Узнайте, как блокировать try, кроме, наконец, и как правильно создавать пользовательские исключения.

Посмотреть ресурс
Практическое руководство

Как найти длину списка в Python

Узнайте, как найти длину списка в Python с помощью функции len(). Поймите временную сложность O(1) и количество проверок.

Посмотреть ресурс
Шпаргалка

Шпаргалка по строковым методам Python

Полное справочное руководство по манипулированию строками в Python. Мастер форматирования, поиска, разделения, замены и проверки свойств строк.

Посмотреть ресурс
Сравнение языков

Python против JavaScript: какой язык программирования лучше?

Всестороннее сравнение Python и JavaScript. Изучите синтаксические различия, производительность, варианты использования (серверная и клиентская части) и примеры кодирования.

Посмотреть ресурс