This problem is fairly simple but the solution that I used here (taken and studied from lee on Leetcode) made me scratch my head a lot.
A basic idea is that we greedily form groups and if we are not able to form groups, i.e. freq[i+j] == 0
, where i = hand_number
, j = position_of_group_member_to_be_formed
.
So, the statement freq[i+j] -= freq[i]
is equivalent to simulating forming a group.
Code:
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since we are sorting the frequency keys
-
Memory The memory usage is since we use the
collections.Counter
object to store frequencies
— A