###Fibo pourri
def fibo(n):
    if n <= 1:
        return n
    return fibo(n-1) + fibo(n-2)

#Complexité temporelle : exponentielle


###Fibo bas en haut avec tableau
def fibo(n):
    T = [0, 1]+[0]*(n-1)
    for i in range(2,n+1):
        T[i] = T[i-2] + T[i-1]
    return T[n]

#Complexité temporelle : linéaire
#Complexité spatiale : linéaire

###Fibo bas en haut optimisé
def fibo(n):
    if n == 0:
        return 0
    a, b = 0, 1
    for _ in range(2, n+1):
        a, b = b, a+b
    return b

#Complexité temporelle : linéaire
#Complexité spatiale : constante

###Rendu de monnaie de bas en haut
def rendu(E, s):
    n = [0]*(s+1)
    for i in range(1, s+1):
        m = n[i-1]
        for p in E:
            if p <= i and n[i-p] < m:
                m = n[i-p]
        n[i] = 1 + m
    return n[s]

#Variante plus concise :
def rendu(E, s):
    n = [0]*(s+1)
    for i in range(1, s+1):
        m = min([n[i-p] for p in E if p <= i])
        n[i] = 1 + m
    return n[s]

#Complexité temporelle : O(s.|E|)
#Complexité spatiale : O(s)

###Fibo mémoïsé
memo_fibo = {0:0, 1:1}

def fibo(n):
    if n in memo_fibo:
        return memo_fibo[n]
    else:
        r = fibo(n-1) + fibo(n-2)
        memo_fibo[n] = r
        return r

#Complexité temporelle : linéaire
#Complexité spatiale : linéaire

###Rendu de monnaie mémoïsé
memo_rendu = {}

def rendu(E, s):
    if s==0:
        return 0
    if (E, s) in memo_rendu:
        return memo_rendu[E, s]
    m = rendu(E, s-1)
    for p in E:
        if p <= s and rendu(E, s-p) < m:
            m = rendu(E, s-p)
    memo_rendu[E, s] = 1 + m
    return 1 + m

#Complexité temporelle : O(s.|E|)
#Complexité spatiale : O(s)

#variante qui ne marche pas (clé insuffisante)
memo_rendu = {}

def rendu(E, s):
    if s==0:
        return 0
    if s in memo_rendu:
        return memo_rendu[s]
    m = rendu(E, s-1)
    for p in E:
        if p <= s and rendu(E, s-p) < m:
            m = rendu(E, s-p)
    memo_rendu[s] = 1 + m
    return 1 + m

rendu((1, 2), 4)
rendu((1, 2, 4), 4)

###Reconstruction du rendu de monnaie de bas en haut
def rendu(E, s):
    n = [0]*(s+1)
    for i in range(1, s+1):
        m = n[i-1]
        for p in E:
            if p <= i and n[i-p] < m:
                m = n[i-p]
        n[i] = 1 + m
    #Reconstruction
    R = []
    while s > 0:
        for p in E:
            if p <= s and n[s-p] + 1 == n[s]:
                R.append(p)
                s = s - p
                break
    return R


###Reconstruction optimisée de rendu bas en haut
def rendu(E, s):
    T = [(0,0)]*(s+1)
    #T[i][0] est la quantité optimale de pièces à rendre sur la somme i
    #T[i][1] est la première pièce qu'on doit rendre sur la somme i
    for i in range(1, s+1):
        m = T[i-1][0]
        pmin = 1
        for p in E:
            if p <= i and T[i-p][0] < m:
                m = T[i-p][0]
                pmin = p
        T[i] = (1 + m, pmin)
    R = []
    while s > 0:
        p = T[s][1]
        s = s - p
        R.append(p)
    return R

###Reconstruction de rendu mémoïsé
memo_rendu = {}

def rendu(E, s):
    if s==0:
        return 0
    if (E, s) in memo_rendu:
        return memo_rendu[E, s]
    m = rendu(E, s-1)
    for p in E:
        if p <= s and rendu(E, s-p) < m:
            m = rendu(E, s-p)
    memo_rendu[E, s] = 1 + m
    return 1 + m

def pieces_a_rendre(E, s):
    R = []
    while s > 0:
        for p in E:
            if p <= s and rendu(E, s-p) + 1 == rendu(E, s):
                R.append(p)
                s = s - p
                break
    return R




