vendredi 19 février 2021

Finding Next Greater Element i? [closed]

I was trying to solve this: https://leetcode.com/problems/next-greater-element-i/

You are given two integer arrays nums1 and nums2 both of unique elements, where nums1 is a subset of nums2.

Find all the next greater numbers for nums1's elements in the corresponding places of nums2.

The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, return -1 for this number.

But couldn't find any efficient and clear solution in C++

How can this be solved in linear time, I think it's not possible since for each element in nums1 first we need to iterate overs nums2 to find it and from there count how many are bigger than him which's O(n_1 * n_2) where n_1 is size of num1.

Aucun commentaire:

Enregistrer un commentaire