From e96b4a30e86b67a34b030bc78603ea4f28265357 Mon Sep 17 00:00:00 2001 From: obelisk0114 Date: Wed, 26 Jun 2019 14:15:17 -0700 Subject: [PATCH 1/4] Update Bucket Sort time complexity analysis --- sorts/bucket_sort.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sorts/bucket_sort.py b/sorts/bucket_sort.py index c4d61874fc47..bd212ede4e10 100644 --- a/sorts/bucket_sort.py +++ b/sorts/bucket_sort.py @@ -11,7 +11,12 @@ # involve the number of buckets. # Time Complexity of Solution: -# Best Case O(n); Average Case O(n); Worst Case O(n) +# Worst case scenario occurs when all the elements are placed in a single bucket. The overall performance +# would then be dominated by the algorithm used to sort each bucket. In this case, O(n log n), because of TimSort +# +# Average Case O(n + (n^2)/k + k), where k is the number of buckets +# +# If k = O(n), time complexity is O(n) DEFAULT_BUCKET_SIZE=5 From 27ba11a1e58f76c06286d15a69e8ba5edf582f21 Mon Sep 17 00:00:00 2001 From: obelisk0114 Date: Sat, 13 Jul 2019 21:29:24 -0700 Subject: [PATCH 2/4] Add combinations --- backtracking/all_combinations.py | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 backtracking/all_combinations.py diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py new file mode 100644 index 000000000000..b0c3f9020e68 --- /dev/null +++ b/backtracking/all_combinations.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- + +""" + In this problem, we want to determine all possible combinations of k + numbers out of 1 ... n. We use backtracking to solve this problem. + Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!))) +""" + + +def generate_all_combinations(n: int, k: int) -> [[int]]: + result = [] + create_all_state(1, n, k, [], result) + return result + + +def create_all_state(increment, total_number, level, current_list, total_list): + if level == 0: + total_list.append(current_list[:]) + return + + for i in range(increment, total_number - level + 2): + current_list.append(i) + create_all_state(i + 1, total_number, level - 1, current_list, total_list) + current_list.pop() + + +def print_all_state(total_list): + for i in total_list: + print(*i) + + +n = 4 +k = 2 +total_list = generate_all_combinations(n, k) +print_all_state(total_list) From a175ca342d4a7dcb559ac28db2679f4ec897e303 Mon Sep 17 00:00:00 2001 From: obelisk0114 Date: Sat, 13 Jul 2019 22:19:36 -0700 Subject: [PATCH 3/4] Adding doctest --- backtracking/all_combinations.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py index b0c3f9020e68..56c84fcf9be4 100644 --- a/backtracking/all_combinations.py +++ b/backtracking/all_combinations.py @@ -8,6 +8,16 @@ def generate_all_combinations(n: int, k: int) -> [[int]]: + """ + >>> generate_all_combinations(n=4, k=2) + 1 2 + 1 3 + 1 4 + 2 3 + 2 4 + 3 4 + """ + result = [] create_all_state(1, n, k, [], result) return result @@ -29,7 +39,8 @@ def print_all_state(total_list): print(*i) -n = 4 -k = 2 -total_list = generate_all_combinations(n, k) -print_all_state(total_list) +if __name__ == '__main__': + n = 4 + k = 2 + total_list = generate_all_combinations(n, k) + print_all_state(total_list) From 6c699111daccc4c839b8121d0335e4157be9ad3d Mon Sep 17 00:00:00 2001 From: obelisk0114 Date: Sat, 13 Jul 2019 22:29:01 -0700 Subject: [PATCH 4/4] Fix doctest problem --- backtracking/all_combinations.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py index 56c84fcf9be4..63425aeabbd1 100644 --- a/backtracking/all_combinations.py +++ b/backtracking/all_combinations.py @@ -10,12 +10,7 @@ def generate_all_combinations(n: int, k: int) -> [[int]]: """ >>> generate_all_combinations(n=4, k=2) - 1 2 - 1 3 - 1 4 - 2 3 - 2 4 - 3 4 + [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]] """ result = []