Skip to content

Latest commit

 

History

History
64 lines (56 loc) · 1.61 KB

496 Next Greater Element I cde72f68940548f486ee5c69f41d5f66.md

File metadata and controls

64 lines (56 loc) · 1.61 KB

496. Next Greater Element I

LeetCode - The World's Leading Online Programming Learning Platform

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int[] nums3 = new int[nums1.length];

        for (int i = 0; i < nums3.length ; i++)
        {
            nums3[i] =  findNextMaxNum(nums2 , nums1[i]);
        }
        return nums3;
    }

    public static int findNextMaxNum(int[] arr , int n )
    {
         int temp = -1 ;
        for (int i = 0 ; i < arr.length ; i++)
        {
            if (arr[i] == n) {
                while ((i + 1) < arr.length && temp <= n) {
                    i++;
                    temp = Math.max(n, arr[i]);
                }
                break;
            }
        }
        return (temp == n) ? -1 : temp ;
    }
}
class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int[] nums3 = new int[nums1.length];
        ArrayList<Integer> arr = new ArrayList<>(nums2.length + 1);
        for (int n : nums2){
            arr.add(n);
         }
           arr.add(-1);

        for (int i = 0; i < nums1.length ; i++)
        {
            nums3[i] =  findMax(arr , nums1[i]);
        }
        return nums3;
    }
        
    public static int findMax(ArrayList arr , int n )
    {
        int temp = -1 ;
        for (int i = arr.indexOf(n) + 1 ; i < arr.size() -1 ; i++) 
        {
            temp = Math.max(n , (Integer) arr.get(i));
             if (temp > n ) break;
        }
        
        return (temp == n) ? -1 : temp ;
    }
}