Skip to content

Instantly share code, notes, and snippets.

@jhonatanmsc
Created February 5, 2020 20:32
Show Gist options
  • Select an option

  • Save jhonatanmsc/25ff36c663086a77f9a01c67facd873f to your computer and use it in GitHub Desktop.

Select an option

Save jhonatanmsc/25ff36c663086a77f9a01c67facd873f to your computer and use it in GitHub Desktop.
def main():
'''
O senhor e-Deployer gostaria de comprar uma ação e vendê-la em um curto espaço de tempo,
mas apenas se esta operação dê lucro. Para isso, é passado um array K com os valores das
ações nos determinados dias, onde ele poderá escolher onde comprar e vender.
Determine o maior lucro dado esse array K de preços.
Exemplo 1:
- nput: [7,1,5,3,6,4]
- Output: 5
- Explicação: Este valor acontece quando compramos a ação no 2º dia e a vendemos no 5º dia (6 - 1)
'''
array = eval(input())
best_day = 0
length = len(array)
best_moment = length
best_results = []
for i in range(length):
if i == (length-1):
break
for j in range(i+1, length):
if array[j] - array[i] > 0 and array[j] - array[i] >= best_day:
best_day = array[j] - array[i]
best_moment = j-i
best_results.append([best_day, j-i])
for val in best_results:
if val[0] == best_day and val[1] < best_moment:
best_moment = val[1]
print(best_day)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment