Created
April 26, 2021 11:25
-
-
Save fakruboss/f9479a9f227709d651fbe3d572951e66 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution{ | |
public static long[] nextLargerElement(long[] arr, int n) { | |
Stack<Integer> stack = new Stack<>(); | |
stack.push(0); | |
long[] result = new long[n]; | |
Arrays.fill(result, -1); | |
for (int i = 1; i < n; ++i) { | |
while (!stack.isEmpty() && arr[i] > arr[stack.peek()]) { | |
result[stack.pop()] = arr[i]; | |
} | |
stack.push(i); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment