An array A consisting of N integers is given. It contains daily prices of a stock share for a period of N consecutive days. If a single share was bought on day P and sold on day Q, where 0 ≤ P ≤ Q < N, then the profit of such transaction is equal to A[Q] − A[P], provided that A[Q] ≥ A[P]. Otherwise, the transaction brings loss of A[P] − A[Q].
For example, consider the following array A consisting of six elements such that:
A[0] = 23171 A[1] = 21011 A[2] = 21123 A[3] = 21366 A[4] = 21013 A[5] = 21367If a share was bought on day 0 and sold on day 2, a loss of 2048 would occur because A[2] − A[0] = 21123 − 23171 = −2048. If a share was bought on day 4 and sold on day 5, a profit of 354 would occur because A[5] − A[4] = 21367 − 21013 = 354. Maximum possible profit was 356. It would occur if a share was bought on day 1 and sold on day 5.
Write a function,
def solution(A)
that, given an array A consisting of N integers containing daily prices of a stock share for a period of N consecutive days, returns the maximum possible profit from one transaction during this period. The function should return 0 if it was impossible to gain any profit.
For example, given array A consisting of six elements such that:
A[0] = 23171 A[1] = 21011 A[2] = 21123 A[3] = 21366 A[4] = 21013 A[5] = 21367the function should return 356, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..400,000];
- each element of array A is an integer within the range [0..200,000].
23171 21011 21123 21366 21013 21367
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
buy_price = A[0]
sell_price = [A1]
for a in A[2:]:
print(a)
day_profit = max(0, a - day_profit)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
buy_price = A[0]
sell_price = [A1]
for a in A[2:]:
print(a)
day_profit = max(0, a - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
buy_price = A[0]
sell_price = [A1]
for a in A[2:]:
print(a)
day_profit = max(0, sell_price - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
buy_price = A[0]
sell_price = [A1]
for a in A[2:]:
print(a)
day_profit = max(0, sell_price - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
buy_price = A[0]
sell_price = [A1]
for a in A[2:]:
print(a)
day_profit = max(0, sell_price - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
daymax_profit = 0
buy_price = A[0]
sell_price = [A1]
for a in A[2:]:
print(a)
day_profit = max(0, sell_price - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
buy_price = sell_price =
for a in A:
print(a)
day_profit = max(0, day_profit - (buy_price))
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
buy_price = sell_price = None
for a in A:
print(a)
day_profit = max(0, day_profit - (buy_price))
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
buy_price = sell_price = None
for a in A:
if not
day_profit = max(0, day_profit - (buy_price))
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
buy_price = sell_price = None
for a in A:
if not buy_price and not sell_price:
day_profit = max(0, day_profit - (buy_price))
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
buy_price = sell_price = None
for a in A:
if not buy_price and not sell_price:
buy_price = sell_price = a
day_profit = max(0, day_profit - (buy_price))
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
buy_price = sell_price = None
for a in A:
if not buy_price and not sell_price:
buy_price = sell_price = a
day_profit = max(0, buy_price - (buy_price))
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
buy_price = sell_price = None
for a in A:
if not buy_price and not sell_price:
buy_price = sell_price = a
day_profit = max(0, a - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
min
for a in A:
if not buy_price and not sell_price:
buy_price = sell_price = a
day_profit = max(0, a - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
minPrice =
for a in A:
if not buy_price and not sell_price:
buy_price = sell_price = a
day_profit = max(0, a - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
minPrice = A[0]
for a in A:
if not buy_price and not sell_price:
buy_price = sell_price = a
day_profit = max(0, a - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
minPrice = A[0]
for a in A[1:]:
if not buy_price and not sell_price:
buy_price = sell_price = a
day_profit = max(0, a - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
minPrice = maxPrice = A[0]
for a in A[1:]:
if not buy_price and not sell_price:
buy_price = sell_price = a
day_profit = max(0, a - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
minPrice = maxPrice = A[0]
for a in A[1:]:
minPrice = maxPrice = A[0]
day_profit = max(0, a - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
minPrice = maxPrice = A[0]
for a in A[1:]:
minPrice =
maxPrice = max(0, a - buy_price)
day_profit = max(0, a - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
minPrice = maxPrice = A[0]
for a in A[1:]:
minPrice = min(0,
maxPrice = max(0, a)
day_profit = max(0, a - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
day_profit = max_profit = 0
minPrice = maxPrice = A[0]
for a in A[1:]:
minPrice = min(minPrice, a)
maxPrice = max(maxPrice, a)
day_profit = max(0, a - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
minPrice = maxPrice = A[0]
for a in A[1:]:
minPrice = min(minPrice, a)
maxPrice = max(maxPrice, a)
day_profit = max(0, a - buy_price)
max_profit = max(max_profit, day_profit)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
minPrice = maxPrice = None
for a in A:
if not minPrice and not maxPrice:
minPrice = maxPrice = a
else:
minPrice = min(minPrice, a)
maxPrice = max(maxPrice, a)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
minPrice = maxPrice = None
for a in A:
if not minPrice and not maxPrice:
minPrice = maxPrice = a
else:
minPrice = min(minPrice, a)
maxPrice = max(maxPrice, a)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
minPrice = maxPrice = None
for a in A:
if not minPrice and not maxPrice:
minPrice = maxPrice = a
else:
minPrice = min(minPrice, a)
maxPrice = max(maxPrice, a)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
minPrice = maxPrice = None
for a in A:
if not minPrice and not maxPrice:
minPrice = maxPrice = a
else:
minPrice = min(minPrice, a)
maxPrice = max(maxPrice, a)
if minPrice and maxPrice:
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
minPrice = maxPrice = None
for a in A:
if not minPrice and not maxPrice:
minPrice = maxPrice = a
else:
minPrice = min(minPrice, a)
maxPrice = max(maxPrice, a)
if minPrice < maxPrice:
max_profit = maxPrice -
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
minPrice = maxPrice = None
for a in A:
if not minPrice and not maxPrice:
minPrice = maxPrice = a
else:
minPrice = min(minPrice, a)
maxPrice = max(maxPrice, a)
if minPrice < maxPrice:
max_profit = maxPrice - minPrice
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
minPrice = None
for a in A:
if not minPrice and not maxPrice:
minPrice = maxPrice = a
else:
minPrice = min(minPrice, a)
maxPrice = max(maxPrice, a)
if minPrice < maxPrice:
max_profit = maxPrice - minPrice
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
minPrice = None
for a in A:
if not minPrice:
minPrice = a
else:
minPrice = min(minPrice, a)
maxPrice = max(maxPrice, a)
if minPrice < maxPrice:
max_profit = maxPrice - minPrice
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
minPrice = None
for a in A:
if not minPrice:
minPrice = a
else:
max_ending = max(a,max_ending + a)
max_slice = max(max_slice, max_ending)
if minPrice < maxPrice:
max_profit = maxPrice - minPrice
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
minPrice = None
for a in A:
if not minPrice:
minPrice = a
else:
max_ending = max(0, a - max_ending)
max_slice = max(max_slice, max_ending)
if minPrice < maxPrice:
max_profit = maxPrice - minPrice
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
minPrice = None
for a in A:
if not minPrice:
minPrice = a
max_ending = max(0, a - max_ending)
max_slice = max(max_slice, max_ending)
if minPrice < maxPrice:
max_profit = maxPrice - minPrice
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
minPrice = None
for a in A:
if not minPrice:
minPrice = a
max_ending = max(0, a - max_ending)
minPrice = min(minPrice,a)
max_slice = max(max_slice, max_ending)
if minPrice < maxPrice:
max_profit = maxPrice - minPrice
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_profit = 0
minPrice = None
for a in A:
if not minPrice:
minPrice = a
max_ending = max(0, a - max_ending)
minPrice = min(minPrice,a)
max_slice = max(max_slice, max_ending)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_slice = = 0
minPrice = None
for a in A:
if not minPrice:
minPrice = a
max_ending = max(0, a - max_ending)
minPrice = min(minPrice,a)
max_slice = max(max_slice, max_ending)
return max_profit
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_slice = max_ending = 0
minPrice = None
for a in A:
if not minPrice:
minPrice = a
max_ending = max(0, a - max_ending)
minPrice = min(minPrice,a)
max_slice = max(max_slice, max_ending)
return max_profit
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( A ) File "/tmp/solution.py", line 15, in solution return max_profit NameError: name 'max_profit' is not defined
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_slice = max_ending = 0
minPrice = None
for a in A:
if not minPrice:
minPrice = a
max_ending = max(0, a - max_ending)
minPrice = min(minPrice,a)
max_slice = max(max_slice, max_ending)
return max_slice
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_slice = max_ending = 0
minPrice = None
for a in A[1]:
if not minPrice:
minPrice = a
max_ending = max(0, a - max_ending)
minPrice = min(minPrice,a)
max_slice = max(max_slice, max_ending)
return max_slice
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
if len(A) < 2: return 0
max_slice = max_ending = 0
minPrice = A[0]
for a in A[1:]
max_ending = max(0, a - max_ending)
minPrice = min(minPrice,a)
max_slice = max(max_slice, max_ending)
return max_slice
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
if len(A) < 2: return 0
max_slice = max_ending = 0
minPrice = A[0]
for a in A[1:]
max_ending = max(0, a - max_ending)
minPrice = min(minPrice,a)
max_slice = max(max_slice, max_ending)
return max_slice
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 70, in main sol = __import__('solution') File "/tmp/solution.py", line 9 for a in A[1:] ^ SyntaxError: invalid syntax
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
if len(A) < 2: return 0
max_slice = max_ending = 0
minPrice = A[0]
for a in A[1:]:
max_ending = max(0, a - max_ending)
minPrice = min(minPrice,a)
max_slice = max(max_slice, max_ending)
return max_slice
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
if len(A) < 2: return 0
max_slice = max_ending = 0
minPrice = A[0]
for a in A[1:]:
max_ending = max(0, a - max_ending)
minPrice = min(minPrice,a)
max_slice = max(max_slice, max_ending)
return max_slice
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
if len(A) < 2: return 0
max_slice = max_ending = 0
minPrice = A[0]
for a in A[1:]:
max_ending = max(0, a - minPrice)
minPrice = min(minPrice,a)
max_slice = max(max_slice, max_ending)
return max_slice
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
if len(A) < 2: return 0
max_slice = max_ending = 0
minPrice = A[0]
for a in A[1:]:
max_ending = max(0, a - minPrice)
minPrice = min(minPrice,a)
max_slice = max(max_slice, max_ending)
return max_slice
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
if len(A) < 2: return 0
max_slice = max_ending = 0
minPrice = A[0]
for a in A[1:]:
max_ending = max(0, a - minPrice)
minPrice = min(minPrice,a)
max_slice = max(max_slice, max_ending)
return max_slice
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
if len(A) < 2: return 0
max_slice = max_ending = 0
minPrice = A[0]
for a in A[1:]:
max_ending = max(0, a - minPrice)
minPrice = min(minPrice,a)
max_slice = max(max_slice, max_ending)
return max_slice
The solution obtained perfect score.
max profit is after global maximum and before global minimum
large value (99) followed by short V-pattern (values from [1..5]) repeated 100 times