Find the minimum time after which one can exchange notes in Python


Suppose there are n number of cashiers exchanging the money, at the moment, the i-th cashier had ki number of people in front of him/her. Now, the j-th person in the line to i-th cashier had m[i,j] notes. We have to find how early can one exchange his/her notes. We have to keep in mind that the cashier spent 5 seconds to scan a single note.After completing scanning of every note for the customer, he/she took 15 seconds to exchange the notes.

So, if the input is like Input : n = 6, k = [12, 12, 12, 12, 12, 12]

7897961099678
10710989999656
9889867910667
769669896689
98765108107668
876579796557

then the output will be 585, as the cashier needs 5 secs for scanning every note of each customer, so add 5*m[I,j]. Now each cashier takes 15 seconds for every customer, so add 15*k[] to the answer. The minimum time taken after computing the time taken by each cashier will be the answer. So cashier m[5] takes the minimum time 585.

To solve this, we will follow these steps −

  • n := size of k

  • minimum := 99999

  • for i in range 0 to n, do

    • temp := k[i] * 15

    • for j in range 0 to k[i], do

      • temp := temp + m[i, j] * 5

    • if temp < minimum, then

      • minimum := temp

  • return minimum

Example

Let us see the following implementation to get better understanding −

 Live Demo

def minTimeToExchange(k, m):
   n = len(k)
   minimum = 99999
   for i in range(n):
      temp = k[i] * 15
   for j in range(k[i]):
      temp += m[i][j] * 5
   if temp < minimum:
      minimum = temp
   return minimum

k = [12, 12, 12, 12, 12, 12]
m = [
   [7,8,9,7,9,6,10,9,9,6,7,8],
   [10,7,10,9,8,9,9,9,9,6,5,6],
   [9,8,8,9,8,6,7,9,10,6,6,7],
   [7,6,9,6,6,9,8,9,6,6,8,9],
   [9,8,7,6,5,10,8,10,7,6,6,8],
   [8,7,6,5,7,9,7,9,6,5,5,7]]
print(minTimeToExchange(k, m))

Input

[12, 12, 12, 12, 12, 12],
[[7,8,9,7,9,6,10,9,9,6,7,8],
[10,7,10,9,8,9,9,9,9,6,5,6],
[9,8,8,9,8,6,7,9,10,6,6,7],
[7,6,9,6,6,9,8,9,6,6,8,9],
[9,8,7,6,5,10,8,10,7,6,6,8],
[8,7,6,5,7,9,7,9,6,5,5,7]]

Output

585

Updated on: 27-Aug-2020

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements