Question:
Return an array of leaders in order of their appearance using Python3

Lets assume:

Input parameters

arr, n

Method

leaders


Solution

class Solution:

    #Back-end complete function Template for Python 3

    

    #Function to find the leaders in the array.

    def leaders(self, arr, n):

        

        maxi = float('-inf') 

        res = []

        for i in range(len(arr)-1, -1, -1):

            if arr[i] >= maxi:

                res.append(arr[i]) 

            

            maxi = max(maxi, arr[i]) 

            

        res.reverse()

        return res 

                

        

        # brute force  (TLE)

        # res = []

        # for i in range(len(arr)):

        #     leader = True

        #     for j in range(i+1, len(arr)):

        #         if arr[j] > arr[i]:

        #             leader = False 

        #             break 

                

        #     if leader == True:

        #         res.append(arr[i]) 

                

        # return res 


Answered by:> >pythonninja

Credit:> >GeekforGeeks


Suggested blogs:

>How to configure python in jmeter?

>How to mock a constant value in Python?

>Creating a pivot table by 6 month interval rather than year

>How to Install mariaDB client efficiently inside docker?

>Can I call an API to find out the random seed in Python `hash()` function?

>How remove residual lines after merge two regions using Geopandas in python?


Submit
0 Answers

What Our Clients Say About Us

Client satisfaction is our ultimate goal. Here are some kind words of our precious clients they have used to express their satisfaction with our service.

Why Choose Us ?

We endow businesses with flexible engagement models based on their unique needs. Our strength lies in state-of-the-art technology and affordable consulting services. Try us for fast POCs, full-fledged applications, or technology consulting. Always available for your service.