邓英超 发表于 2013-7-30 15:44:04

\"[google面试题]If
 you
 were
 only
 permitted
 to
 buy
 one
 share
 of
 the 
stock
 and
 sell
 one 
share 
of
 the
 stock,
design 
an 
algorithm
 to
 find
 the 
best
 times
 to
 buy
 and 
sell.\"

发表于 2013-7-30 16:20:41

Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to buy one share of the stock and sell one share of the stock,design an algorithm to find the best times to buy and sell.Good answer:Go through the array in order,keeping track of the lowest stock priceand the best deal you've seen so far.Whenever the current stock price minus thecurrent lowest stock price is better than the current best deal,update the best deal to this new value.
页: [1]
查看完整版本: \"[google面试题]If
 you
 were
 only
 permitted
 to
 buy
 one
 share
 of
 the 
stock
 and
 sell
 one 
share 
of
 the
 stock,
design 
an 
algorithm
 to
 find
 the 
best
 times
 to
 buy
 and 
sell.\"