Horje
maximise the rides with the given tokens java Code Example
maximise the rides with the given tokens java
from heapq import *

def process(arr,n):
    count = 0
    
    heap = []
    
    for i in range(len(arr)):
        heappush(heap,(arr[i],-(len(arr)-i))) # Constructing min-heap with second index as negative of maximum number of rides 
    
    while(n>0 and heap):
        cost,no_of_rides = heappop(heap)
        no_of_rides = -1 * no_of_rides # Changing maximum no_of_rides from negative to positive
        
        div = n//cost

        # If the amount of money is not sufficient to calculate the last number of rides user could take
        if(div<no_of_rides):
            count += div
            break

        # Else decrement the number of tokens by minimum cost * maximum no_of_rides
        else:
            count += no_of_rides
            n -= no_of_rides*cost
    

    return count;




Java

Related
interview questions on ds and algo Code Example interview questions on ds and algo Code Example
rerun main method Code Example rerun main method Code Example
Split String regex java Code Example Split String regex java Code Example
gc algorithms java 8 Code Example gc algorithms java 8 Code Example
Algorithms - transformation Code Example Algorithms - transformation Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
11