-1

So my professor asked this on the test the other day and I am not sure I got it right. A mailman has only one hour to deliver as many letters as possible(one by one). In order to deliver a letter in distance n it takes 5 + 0 .5logn minutes (log with base 3). He decides which letters he will deliver using a greedy algorithm. If the distances of each letter is given in the matrix how many letters will he deliver? A = 3, 3, 3, 9, 9, 3, 3, 27, 27, 27, 81, 9, 81, 81

I thought that he will choose to deliver the letters with the minimum distance first because these take less time (so the ones with distance 3, then those with a distance of 9 etc...). So my answer was 5 letters with distance 3 (time = 27.5), then 3 letters with distance 9 (18 additional minutes, so the total is 45.5) and then 2 of distance 27 so the total time is 58.5 . Am I thinking this right???

Katerina
  • 7
  • 1
  • It is not clear what the "distances of each letter" represents: are all mailboxes distributed on one straight line? Are all the distances measured from the starting point or are they relative to each other? Are all mailboxes at the same side of the starting point? You speak of a *matrix*, but then provide a one dimensional array... – trincot Jul 07 '22 at 06:01

1 Answers1

1

If A represents the distances between postoffice and mailbox for the letter and mailman can only deliver one letter per run, then yes. Actually this problem is called "Knapsack problem". You can find further information on the internet.

eightlay
  • 423
  • 2
  • 9