66
77def next_greatest_element_slow (arr : list [float ]) -> list [float ]:
88 """
9- Get the Next Greatest Element (NGE) for all elements in a list.
10- Maximum element present after the current one which is also greater than the
11- current one.
9+ Get the Next Greatest Element (NGE) for each element in the array
10+ by checking all subsequent elements to find the next greater one.
11+
12+ This is a brute-force implementation, and it has a time complexity
13+ of O(n^2), where n is the size of the array.
14+
15+ Args:
16+ arr: List of numbers for which the NGE is calculated.
17+
18+ Returns:
19+ List containing the next greatest elements. If no
20+ greater element is found, -1 is placed in the result.
21+
22+ Example:
1223 >>> next_greatest_element_slow(arr) == expect
1324 True
1425 """
@@ -28,9 +39,21 @@ def next_greatest_element_slow(arr: list[float]) -> list[float]:
2839
2940def next_greatest_element_fast (arr : list [float ]) -> list [float ]:
3041 """
31- Like next_greatest_element_slow() but changes the loops to use
32- enumerate() instead of range(len()) for the outer loop and
33- for in a slice of arr for the inner loop.
42+ Find the Next Greatest Element (NGE) for each element in the array
43+ using a more readable approach. This implementation utilizes
44+ enumerate() for the outer loop and slicing for the inner loop.
45+
46+ While this improves readability over next_greatest_element_slow(),
47+ it still has a time complexity of O(n^2).
48+
49+ Args:
50+ arr: List of numbers for which the NGE is calculated.
51+
52+ Returns:
53+ List containing the next greatest elements. If no
54+ greater element is found, -1 is placed in the result.
55+
56+ Example:
3457 >>> next_greatest_element_fast(arr) == expect
3558 True
3659 """
@@ -47,14 +70,23 @@ def next_greatest_element_fast(arr: list[float]) -> list[float]:
4770
4871def next_greatest_element (arr : list [float ]) -> list [float ]:
4972 """
50- Get the Next Greatest Element (NGE) for all elements in a list.
51- Maximum element present after the current one which is also greater than the
52- current one.
53-
54- A naive way to solve this is to take two loops and check for the next bigger
55- number but that will make the time complexity as O(n^2). The better way to solve
56- this would be to use a stack to keep track of maximum number giving a linear time
57- solution.
73+ Efficient solution to find the Next Greatest Element (NGE) for all elements
74+ using a stack. The time complexity is reduced to O(n), making it suitable
75+ for larger arrays.
76+
77+ The stack keeps track of elements for which the next greater element hasn't
78+ been found yet. By iterating through the array in reverse (from the last
79+ element to the first), the stack is used to efficiently determine the next
80+ greatest element for each element.
81+
82+ Args:
83+ arr: List of numbers for which the NGE is calculated.
84+
85+ Returns:
86+ List containing the next greatest elements. If no
87+ greater element is found, -1 is placed in the result.
88+
89+ Example:
5890 >>> next_greatest_element(arr) == expect
5991 True
6092 """
0 commit comments