Created
November 22, 2014 13:08
-
-
Save binhngoc17/0569bc07cd2a991800c4 to your computer and use it in GitHub Desktop.
Stock Price Calculation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import string | |
import fileinput | |
import pprint | |
inputs = fileinput.input() | |
input_vals = [] | |
for n in inputs: | |
input_vals.append(n.replace('\n', '')) | |
numTestCase = int(input_vals[0]) | |
data = [] | |
for i in range(numTestCase): | |
data.append( | |
[int(i) for i in input_vals[(i +1) * 2].split(' ')] | |
) | |
for prices in data: | |
maxFromLastEl = [0 for i in range(len(prices))] | |
i = len(prices) - 1 | |
currMax = 0 | |
while i >= 0: | |
if prices[i] > currMax: | |
currMax = prices[i] | |
maxFromLastEl[i] = currMax | |
i = i - 1 | |
# print maxFromLastEl | |
profits = [i[0] - i[1] for i in zip(maxFromLastEl, prices)] | |
print sum(profits) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment