tutorialcup
Next Greater Element I Leetcode Solution
Problem Statement
In this problem, we are given two lists in which first list is subset of second list. For each element of first list, we have to find out next greater element in the second list.
Example
nums1 = , nums2 =
Explanation:
for first element of list1 i.e. for 4 there is no next greater element in list2, thus its answer is -1.
for second element of list1 i.e. for 1 there is 3 greater than 1 in list 2, thus its answer is 3.
for third element of list1 i.e. for 2 there is no next greater element in list2, thus its answer is -1.
nums1 = , nums2 =
Approach 1 (Brute Force)
In this approach, we simply find next greater element for each element of list1 by doing linear traversal in list2 using a nested for loop.
Each element of list1 is first searched in list2, then afterwards its next greater element is searched. We are doing this by using a flag variable. For each element of list1, it is first set to false. When we have found the element in list2, it is set to true. After that, when we will find the next greater, we will set it to false again. By doing so, we will get to know that there is any next greater element in list2 for that variable or there isn't any.
- If the flag variable is true, it means we could not find any next greater value for that element.
- If the flag variable is false, it means that we have found one next greater value for that element.
So, at end of each inner loop, according to flag value we will insert -1 as our answer.
www.tutorialcup.com/leetcode-solutions/next-greater-eleme...
Next Greater Element I Leetcode Solution
Problem Statement
In this problem, we are given two lists in which first list is subset of second list. For each element of first list, we have to find out next greater element in the second list.
Example
nums1 = , nums2 =
Explanation:
for first element of list1 i.e. for 4 there is no next greater element in list2, thus its answer is -1.
for second element of list1 i.e. for 1 there is 3 greater than 1 in list 2, thus its answer is 3.
for third element of list1 i.e. for 2 there is no next greater element in list2, thus its answer is -1.
nums1 = , nums2 =
Approach 1 (Brute Force)
In this approach, we simply find next greater element for each element of list1 by doing linear traversal in list2 using a nested for loop.
Each element of list1 is first searched in list2, then afterwards its next greater element is searched. We are doing this by using a flag variable. For each element of list1, it is first set to false. When we have found the element in list2, it is set to true. After that, when we will find the next greater, we will set it to false again. By doing so, we will get to know that there is any next greater element in list2 for that variable or there isn't any.
- If the flag variable is true, it means we could not find any next greater value for that element.
- If the flag variable is false, it means that we have found one next greater value for that element.
So, at end of each inner loop, according to flag value we will insert -1 as our answer.
www.tutorialcup.com/leetcode-solutions/next-greater-eleme...