71 views
# TDA | Ejercicio 7 practica 1 ## a) - En el [one note](https://ubauba-my.sharepoint.com/:o:/g/personal/grunt_uba_ar/EirE3mC3H3RMiK1_PMl1yJIBi-QdTe8cVBuaEXD-hqQK5g) ## b) $mgn(j, c) = \begin{cases} -\infty & \text{si } c < 0 \lor c > j \\ 0 & \text{si } c = 0 \wedge (j = 0 \lor j = n - 1) \\ \max \left\{ \begin{array}{ll} mgn(j-1, c-1) - p_j & \text{si } c > 0 \\ mgn(j-1, c+1) + p_j & \text{si } c < n - j \\ mgn(j-1, c) & \text{sin operar} \end{array} \right. & \text{en otro caso} \end{cases}$ ## d ```python dias = [3, 2, 5, 6] memo = {} def mgn(j, c): if (j, c) in memo: # Verificar si el resultado ya se calculó. return memo[(j, c)] if c < 0 or c > j: # Caso base para condiciones invalidas. return float('-inf') if j == 0: return 0 if c == 0 else float('-inf') # Caso base, día 0 con cero asteroides. # La ganancia se calcula considerando la compra, venta o no operar. comprar = mgn(j-1, c-1) - dias[j-1] if c > 0 else float('-inf') vender = mgn(j-1, c+1) + dias[j-1] if c < j else float('-inf') no_operar = mgn(j-1, c) memo[(j, c)] = max(comprar, vender, no_operar) # Se almacena el resultado en la memoria. return memo[(j, c)] print(mgn(len(dias), 0)) ```