Leetcode - Combination Sum III (Python)

preview_player
Показать описание
September 2020 Leetcode Challenge
Leetcode - Combination Sum III
Рекомендации по теме
Комментарии
Автор

I have learned a lot from you sir for just merely a week
>"Cartesian product" to form combinations from MULTIPLE sets > CHECK
> "Importing CSV File > CHECK
> "Filtering using SUM" > CHECK
> "Filtering by matching elements, the EASIEST way i've seen" > CHECK

Thank you so

richarddevera
Автор

I think using itertools is faster because Python is written in C in the background, and itertools uses C, which is way faster than Python.

VioletVal
Автор

Thank you for your explanation. May i know what is the space and time complexity for your first approach?

yidan_wang_ittan
Автор

Was searching for this approach!
Sir, It would help a lot if you gave a dry run of the code on a notepad.

gaddesaishailesh
Автор

yes! this is one of the things that i'm looking for, filtering through sum :)

richarddevera
Автор

i'm back with another problem XD OK, so here's the case; i'm using your code for generating the all set of combinations that i want.
Now, i want to filter OUT (remove) those combinations that will match 3 and more of this set D ={a, g, l, m}
OR you could say i want that the only combinations to APPEAR are those which will match 2 or less (including 0 match) in the set D ={a, g, l, m}

A = ["a", "b", "c", "d", "e"]
B = ["f", "g", "h", "i"]
C = ["k", "l", "m", "n"]

import itertools
import csv

# combo of each group
C1 = list(itertools.combinations(A, 1))
C2 = list(itertools.combinations(B, 2))
C3 = list(itertools.combinations(C, 2))

with open('file.csv', 'w', newline='') as csvfile:
w = csv.writer(csvfile, delimiter=', ')

# combine all combo into carterisian product
for a, b, c in list(itertools.product(C1, C2, C3)):
w.writerow([a[0], b[0], b[1], c[0], c[1]])
# print(a[0], b[0], b[1], c[0], c[1])

richarddevera
welcome to shbcf.ru