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:

  1. Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
  2. Output: [-1,3,-1]
  3. Explanation:
  4. For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
  5. For number 1 in the first array, the next greater number for it in the second array is 3.
  6. For number 2 in the first array, there is no next greater number for it in the second array, so output -1.

题意:

给定数组nums, 又给定它的子集subNums, 求subNums中每个元素在nums中右边第一个较大元素(即Next Greater Element)

  1. subNums = [,1,2] 当扫到元素4 该元素在 nums = [1,3,,2] 中右边第一个数为2 并不greater than 4, 返回-1
  1. subNums = [4,,2] 当扫到元素1 该元素在 nums = [,3,4,2] 中右边第一个数为3 确实greater than 1, 返回 3
  1. subNums = [4,1,] 当扫到元素2 该元素在 nums = [1,3,4,] 中右边没有元素, 不存在greater than 2, 返回-1

思路:

两个指针同时扫 subNums 和 nums

用一个boolean变量做标记,若当前 subNums 元素等于当前nums元素,则标记 found = true ,说明找到了corresponding元素。

nums指针继续往右,找greater element, 直到找到符合 found && nums[j] > subNums[i] 条件的元素。否则返回-1

代码一:

  1. class Solution {
  2. public int[] nextGreaterElement(int[] subNums, int[] nums) {
  3. int[] res = new int[subNums.length];
  4. for (int i = 0; i < subNums.length; i++) {
  5. boolean found = false;
  6. int j = 0;
  7. for (; j < nums.length; j++) {
  8. if (found && nums[j] > subNums[i]) {
  9. res[i] = nums[j];
  10. break;
  11. }
  12. if (found && nums[j] < subNums[i]) {
  13. res[i] = -1;
  14. }
  15. if (nums[j] == subNums[i]) {
  16. found = true;
  17. }
  18. }
  19. if (j == nums.length) {
  20. res[i] = -1;
  21. }
  22. }
  23. return res;
  24. }
  25. }

另外一个思路就是用单调栈 + HashMap

为何用单调栈?  为任意一个元素找左边和右边第一个比自己大/小的位置,用单调栈。

先只take care of nums : 从右往左扫nums, 用Stack维护递减栈,留下波峰,剔除波谷。比较栈顶元素跟即将入栈元素大小,

用HashMap来记录其比较结果。再take care of nums: 扫一遍subNums,在HashMap中找到对应的value,返回即可。

  1. subNums = [4,1,2], nums = [1,0,3,4,2]
    Stack HashMap
    ^ [2] 2 | -1
    ^ [4] 4 | -1
    ^ [4, 3] 3 | 4
    ^ [4, 3, 0] 0 | 3
    ^ [4, 3, 1] 1 | 3
  1. 代码二:
  1. public int[] nextGreaterElement(int[] subNums, int[] nums) {
  2. int[] res = new int[subNums.length];
  3. Stack<Integer> stack = new Stack<>();
  4. HashMap<Integer, Integer> map = new HashMap<>();
  5. for (int i = nums.length - 1; i >= 0; i--) {
  6. while (!stack.empty() && nums[i] > stack.peek()) {
  7. stack.pop();
  8. }
  9. if (stack.empty()) {
  10. map.put(nums[i], -1);
  11. } else {
  12. map.put(nums[i], stack.peek());
  13. }
  14. stack.push(nums[i]);
  15. }
  16.  
  17. for (int i = 0; i < subNums.length; i++) {
  18. res[i] = map.get(subNums[i]);
  19. }
  20. return res;
  21. }
  1.  

[leetcode]496. Next Greater Element I下一个较大元素的更多相关文章

  1. [LeetCode] 496. Next Greater Element I 下一个较大的元素 I

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  2. 496 Next Greater Element I 下一个更大元素 I

    给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值.nums1 中数字 x 的下一个更大 ...

  3. [LeetCode] 503. Next Greater Element II 下一个较大的元素 II

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  4. [LeetCode] 556. Next Greater Element III 下一个较大的元素 III

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  5. 503 Next Greater Element II 下一个更大元素 II

    给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它 ...

  6. Leetcode496.Next Greater Element I下一个更大的元素1

    给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更 ...

  7. [LeetCode] Next Greater Element II 下一个较大的元素之二

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  8. [LeetCode] Next Greater Element III 下一个较大的元素之三

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  9. [LeetCode] Next Greater Element I 下一个较大的元素之一

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

随机推荐

  1. 将ESXI所有的端口组迁移到分布式交换机的步骤

    1.如果是DELL服务器,一般有2-4个网口,那么所有的网口都把网线插到交换机上:2.DELL安装ESXI系统,根据不同的DELL硬件,要安装不同的ESXI版本.原则上越高版本,支持的硬件越多向下兼容 ...

  2. Spark分析之BlockManager

    BlockManager中存储block的流程: doPut()方法   入参:blockId, data, level, tellMaster 1)为block创建BlockInfo并加锁使其不能被 ...

  3. PHP流程控制 - if 语句

    PHP - if 语句 if 语句用于仅当指定条件成立时执行代码. 语法 if (条件) { 条件成立时要执行的代码; } 如果当前时间小于 20,下面的实例将输出 "Have a good ...

  4. border-radius bug 收集

    border-radius我相信对于老一辈的前端们有着特殊的感情,在经历了没有圆角的蛮荒时代,到如今 CSS3 遍地开花,我们还是很幸福的. 然而即使到了三星大脸流行时代,border-radius在 ...

  5. swagger配置

    1.pom.xml <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <a ...

  6. ubuntu16.04安装python3,numpy,pandas等量化计算库

    ubunt安装python3 sudo add-apt-repository ppa:fkrull/deadsnakessudo apt-get updatesudo apt-get install ...

  7. jsp 传多个值给后端

     页面上是这样 http://localhost:8080/smartcloset/getClothByCategory/1/11 直接用/分 后台是这样取的 @RequestMapping(valu ...

  8. 40. Linux下7-zip解压到当前目录的命令

    7z x test.zip 解压到当前目录下,但保留原来的目录结构 7z e test.zip 解压到当前目录下,不保留原来的目录结构

  9. Hystrix-超时设置

    由于客户端请求服务端方法时,服务端方法响应超过1秒将会触发降级,所以我们可以配置Hystrix默认的超时配置 如果我们没有配置默认的超时时间,Hystrix将取default_executionTim ...

  10. Apache Hive 执行HQL语句报错 ( 10G )

    # 故障描述: hive > , ) as uuid, count(distinct(request_body["uuid"])) as count from log_bft ...