How would I create an algorithm to find out the greatest volume of sales on a given day. So the most amountSold basically. I was wondering how I would begin to write this algorithm, here's my logic:
for x the date:
get amountSold
assign to temp
if temp is greater than mostSales
mostSales = temp
I need to somehow incorporate the acknowledgement of the same date selling items
EDIT:
I think I have a solution.. Please comment improvements
char[] previousDate
for x the date:
if(x == previosDate)
temp = amountSold + temp
else
temp = amountSold
if temp is greater than mostSales
mostSales = temp
def MaxSalesOnDay(InputSet):
hash = []
maxSales = -infinity
for items in InputSet:
if items.date not in hash:
hash.add(items.date, 0)
value = hash[items.date]
hash.update(items.date, value + items.count)
maxSales = Max(value, maxSales)
retu maxSales
The hash is part of many mainstream programming languages. Or you can implement one on your own.
Assuming your dates are ordered, you don't need a map:
curDate <- invalid
curAmount <- invalid
bestAmount <- invalid
for x the date:
if not sameDate(curDate, x)
curDate = x
if bestAmount < curAmount
bestAmount = curAmount
curAmount <- 0
curAmount <- curAmount + amountSold
A map, sometimes called a dictionary, is an appropriate datastructure for this.
With a map you can map keys to values.
Define a map that maps dates to integers.
Then loop over your data set, find the date in the map, add it if it doesn't exist, and if it does exist, overwrite the value if it is higher than the current value.