C# Soln Beats 85%


  • 0
    V

    A simple solution scanning for both the arrays by checking the index

    public class Solution {
        public int[] NextGreaterElement(int[] findNums, int[] nums) {
            if(findNums == null)
                return null;
            int[] ans = new int[findNums.Length];
            int numsIndex = 0;
            bool isFound = false;
            for(int i=0;i<findNums.Length;i++)
            {
                numsIndex = Array.FindIndex(nums, x => x == findNums[i]);
                isFound = false;
                while(numsIndex < nums.Length)
                {
                    if(nums[numsIndex] > findNums[i])
                    {
                        isFound = true;
                        ans[i] = nums[numsIndex];
                        break;
                    }
                    numsIndex++;
                }
                if(!isFound) ans[i] = -1;
            }
            return ans;
        }
    }
    

  • 0

    A space saving and straightforward solution!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.