fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. class solution
  7. {
  8. public static void main (String[] args)
  9. {
  10. int[] nums={1,7,5,1,9,2,5,1};
  11. int[] res=new int[nums.length];
  12. Nextgreat(nums, res);
  13. for (int e: res)
  14. System.out.println(e);
  15. }
  16. public static void Nextgreat(int[] nums, int[] res)
  17. {
  18. Stack<Integer>stack=new Stack<>();
  19. for (int i=0; i<nums.length; i++)
  20. {
  21. while (!stack.isEmpty()&&nums[stack.peek()]<nums[i])
  22. res[stack.pop()]=nums[i];
  23. stack.push(i);
  24. }
  25. }
  26. }
  27.  
  28.  
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
7
9
9
9
0
5
0
0