Created
June 8, 2024 10:06
-
-
Save codecakes/9ba38db9f0ec426277619dc108db936d to your computer and use it in GitHub Desktop.
N choose K combinations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_combinations(n, k): | |
""" | |
Args: | |
n(int32) | |
k(int32) | |
Returns: | |
list_list_int32 | |
""" | |
array = list(range(1,n+1)) | |
result = set() | |
for idx, num in enumerate(array): | |
do_find_combinations(result, array[idx+1:], [num], k, k-1) | |
return [list(x) for x in result] | |
def do_find_combinations(result, array, acc, count, k): | |
if k == 0: | |
result.add(tuple(acc)) | |
for idx, num in enumerate(array): | |
do_find_combinations(result, array[idx+1:], acc + [num], count, k-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment