Dashboard Temp Share Shortlinks Frames API

HTMLify

next-element-with-greater-frequency.py
Views: 2 | Author: prakhardoneria
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from collections import Counter

class Solution:
    def nextFreqGreater(self, arr):
        n = len(arr)
        freq = Counter(arr)
        res = [-1] * n
        stack = []

        for i in range(n):
            while stack and freq[arr[stack[-1]]] < freq[arr[i]]:
                res[stack.pop()] = arr[i]
            stack.append(i)
            
        return res