496. Next Greater element I Another array that corresponds to larger elements

Source: Internet
Author: User

[Copy question]:

You are given arrays (without duplicates) and nums1 nums2 where's nums1 elements is 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 was the first Greater number to their right in nums1 nums2 . If It does not exist, output-1 for this number.

Example 1:

INPUT:NUMS1 = [4,1,2], NUMS2 = [1,3,4,2]. Output: [ -1,3,-1]explanation:    for number 4 in the first array, cannot find the next greater number for it in the S Econd array, so output-1.    For number 1 with the first array, the next greater number for it in the second array is 3.    For number 2 in the first array, there are no next greater number for it in the second array, so output-1.

Example 2:

INPUT:NUMS1 = [2,4], NUMS2 = [1,2,3,4]. Output: [3,-1]explanation:    for number 2 in the first array, the next greater number for it in the second array is 3.
   for number 4 in the first array, there are no next greater number for it in the second array, so output-1.

[Brute force solution]:

Time Analysis:

Spatial Analysis:

[After optimization]:

Time Analysis:

Spatial Analysis:

[Wonderful output CONDITIONS]:

[Wonderful corner case]:

[Thinking questions]:

Feel the problem with stack is very difficult, basic backrest. Summarized below.

[a sentence of thought]:

[input]: null: Normal: Large: Extra Small: Special cases handled in the program: abnormal conditions (unreasonable input):

[Drawing]:

[One brush]:

[Two brushes]:

[Three brushes]:

[Four brushes]:

[Five brushes]:

[Results of five-minute visual debug]:

[Summary]:

[Complexity]:time Complexity:o () Space complexity:o ()

[English data structure or algorithm, why not other data structures or algorithms]:

[Key templating code]:

[Other solutions]:

[Follow up]:

Next Greater Element II still uses a stack

[The problem given by the LC becomes variable]:

[Code Style]:

496. Next Greater element I Another array that corresponds to larger elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.