496. Next Greater Element I

496. Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are 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, 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, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is 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 is no next greater number for it in the second array, so output -1.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/next-greater-element-i
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Think and method:

  1. Violent solution
    Obviously in this topic, violent method is feasible, because we consider the fact violence operation also need only to a traversal of two arrays, according to the elements in the nums1 search, once found in nums2 under one of the biggest is added to the array, otherwise it returns 1, very simple ideas are more easy to implement, of course, according to the definition of the next largest element, we need to in the process of traversing nums1 elements in nums2 position to make a judgment, and then to find

Time complexity: O(n^2) two-round traversal

Space complexity: O(n)

  1. Monotone stack
    As you can see in the violent method, the time consumption is actually quite serious, and we are thinking about how to optimize the traversal process, obviously the monotone stack can help us solve this problem.The most obvious way to reduce time complexity is to traverse less.
    We consider recording a larger value with a hash table: we traverse nums2, record a larger value if the current nums2 element is larger than the top of the stack, and then push that element onto the stack.
    After doing this, we just need to go through nums1 and look it up one by one.

Time complexity: O(n) Obviously time complexity goes down to O(n)

Space complexity: O(n)

Codes:
1、Violent solution

func nextGreaterElement(nums1 []int, nums2 []int) []int {
	stack := make([]int,0)
                //Directly traverses the two arrays, according to the elements in nums1, 
                //if you can find the output then push into the result else return -1
	for i:=0;i <= len(nums1) - 1;i++ {
		for j:=0;j <= len(nums2) - 1;j++ {
			if nums1[i] == nums2[j] {
				symbol := false
				for k := j+1; k <= len(nums2) - 1;k++ {
					if nums1[i] < nums2[k] {
						symbol = true
						stack = append(stack,nums2[k])
						break
					}
				}
				if symbol == false {
					stack = append(stack,-1)
				}
				break
			}
		}
	}
	return stack
}

2、Monotone stack

func nextGreaterElement(nums1 []int, nums2 []int) []int {
	a := make(map[int]int)
      //var result []int
	var stack []int
	//Go through nums2, push it into the stack
      for _, v := range nums2 {
        //As soon as a larger number is found, pop the top number 
        //and update the next maximum numeric value for the top element
		for len(stack) != 0 && v > stack[len(stack)-1] {
			a[stack[len(stack)-1]] = v
			stack = stack[:len(stack)-1]
		}
		stack = append(stack, v)
	}
      //go through nums1 if find the Corresponding value otherwise return -1 
	for i, j := range nums1 { 
		if value, ok := a[j]; ok {
			nums1[i] = value
		} else {
			nums1[i] = -1
		}
	}
	return nums1
}

Test:
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
在这里插入图片描述

Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4].
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值