Optimal dynamic pricing of inventories with stochastic demand over finite horizons