Maximum Possible Rating of a Coding Contest | Python3 program to find the Maximum Possible Rating of a Coding Contest ; Function to sort all problems descending to upvotes ; Function to return maximum rating ; Declaring vector of pairs ; Each pair represents a problem with its points and upvotes ; Step ( 1 ) - Sort problems by their upvotes value in decreasing order ; Declaring min_heap or priority queue to track of the problem with minimum points . ; Step ( 2 ) - Loop for i = 0 to K - 1 and do accordingly ; Step ( 3 ) - Loop for i = K to N - 1 and do accordingly ; Driver code
import heapq NEW_LINE def Comparator ( p1 ) : NEW_LINE INDENT return p1 [ 1 ] NEW_LINE DEDENT def FindMaxRating ( N , Point , Upvote , K ) : NEW_LINE INDENT vec = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT vec . append ( [ Point [ i ] , Upvote [ i ] ] ) NEW_LINE DEDENT vec . sort ( reverse = True , key = Comparator ) NEW_LINE pq = [ ] NEW_LINE heapq . heapify ( pq ) NEW_LINE total_points , max_rating = 0 , 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT total_points = ( total_points + vec [ i ] [ 0 ] ) NEW_LINE max_rating = max ( max_rating , total_points * vec [ i ] [ 1 ] ) NEW_LINE heapq . heappush ( pq , vec [ i ] [ 0 ] ) NEW_LINE DEDENT for i in range ( K , N ) : NEW_LINE INDENT if pq [ 0 ] < vec [ i ] [ 0 ] : NEW_LINE INDENT total_points = ( total_points - pq [ 0 ] + vec [ i ] [ 0 ] ) NEW_LINE max_rating = max ( max_rating , total_points * vec [ i ] [ 1 ] ) NEW_LINE heapq . heappop ( pq ) NEW_LINE heapq . heappush ( pq , vec [ i ] [ 0 ] ) NEW_LINE DEDENT DEDENT return max_rating NEW_LINE DEDENT Point = [ 2 , 10 , 3 , 1 , 5 , 8 ] NEW_LINE Upvote = [ 5 , 4 , 3 , 9 , 7 , 2 ] NEW_LINE N = len ( Point ) NEW_LINE K = 2 NEW_LINE print ( " Maximum ▁ Rating ▁ of ▁ Coding ▁ Contest ▁ is : " , FindMaxRating ( N , Point , Upvote , K ) ) NEW_LINE