[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:新建res数组存nums1的结果

[优化后]:

时间分析:

空间分析:反正是查询index,就地存储即可

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

感觉用stack的题都挺难的,基本靠背。总结下。

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. stack应该搭配while循环,别粗心写成if

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

stack当备胎池,有用的结果有选择性地放在别的地方

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

stack当备胎池,只有最上端开口,只处理当下元素

有用的结果有选择性地放在hashmap里

[关键模板化代码]:

[其他解法]:

[Follow Up]:

Next Greater Element II 还是用stack

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
//ini: stack, map
Stack<Integer> stack = new Stack<>();
Map<Integer, Integer> map = new HashMap<>(); //store in stack and map
for (int num : nums2) {
//bigger,put into map
while (!stack.isEmpty() && num > stack.peek()) {
map.put(stack.pop(), num);
}
stack.push(num);
} //get res from map
for (int i = 0; i < nums1.length; i++) {
nums1[i] = map.getOrDefault(nums1[i], -1);
} //return
return nums1;
}
}

496. Next Greater Element I 另一个数组中对应的更大元素的更多相关文章

  1. C#实现如何判断一个数组中是否有重复的元素

    如何判断一个数组中是否有重复的元素 实现判断数组中是否包含有重复的元素方法 这里用C#代码给出实例 方法一:可以新建一个hashtable利用hashtable的Contains方法进行查找 /// ...

  2. C#实现如何判断一个数组中是否有重复的元素 返回一个数组升序排列后的位置信息--C#程序举例 求生欲很强的数据库 别跟我谈EF抵抗并发,敢问你到底会不会用EntityFramework

    C#实现如何判断一个数组中是否有重复的元素   如何判断一个数组中是否有重复的元素 实现判断数组中是否包含有重复的元素方法 这里用C#代码给出实例 方法一:可以新建一个hashtable利用hasht ...

  3. 如何寻找无序数组中的第K大元素?

    如何寻找无序数组中的第K大元素? 有这样一个算法题:有一个无序数组,要求找出数组中的第K大元素.比如给定的无序数组如下所示: 如果k=6,也就是要寻找第6大的元素,很显然,数组中第一大元素是24,第二 ...

  4. Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)

    题目原文 Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respec ...

  5. [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 ...

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

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

  7. [leetcode]496. Next Greater Element I下一个较大元素

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

  8. Leetcode703.Kth Largest Element in a Stream数据流中的第K大元素

    设计一个找到数据流中第K大元素的类(class).注意是排序后的第K大元素,不是第K个不同的元素. 你的 KthLargest 类需要一个同时接收整数 k 和整数数组nums 的构造器,它包含数据流中 ...

  9. 寻找无序数组中的前k大元素

    题目描述 以尽可能小的代价返回某无序系列中的两个最大值,当有重复的时设置某种机制进行选择. 题解 首先要考虑的是重复的数的问题. A.不处理重复数据方法:在处理第k大的元素时不处理重复的数据,也就是将 ...

随机推荐

  1. 【MFC】断言(ASSERT)的用法

    摘自:Moondark  http://www.cnblogs.com/moondark/archive/2012/03/12/2392315.html 断言(ASSERT)的用法   我一直以为as ...

  2. LUAROCKS 报错解决办法

    用luarocks 加载包时报错 Warning: falling back to curl - install luasec to get native HTTPS support 此时先安装 ./ ...

  3. HihoCoder1622 : 有趣的子区间(预处理+组合数)

    有趣的子区间 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 如果一个区间[a, b]内恰好包含偶数个回文整数,我们就称[a, b]是有趣的区间. 例如[9, 12]包含 ...

  4. Mac OS X显示隐藏文件命令

    defaults write com.apple.finder AppleShowAllFiles Yes && killall Finder //显示隐藏文件 defaults wr ...

  5. 转载pll工作模式解析

    PLL共有四种工作模式,只有理解了这四种工作模式的特点,才能在设计中选用恰当的模式,完成自己设计的预期功能.这四种工作模式分别是普通模式(Normal Mode).零延迟缓冲模式(Zero Delay ...

  6. springboot 不同环境不同的配置

    前言 我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发.测试.生产等.其中每个环境的数据库地址.服务器端口等等配置都会不同,如果在为不同环境打包时都要频 ...

  7. miniofs 配置使用

    1. rpm  // RPM 包下载 https://github.com/minio/minfs/releases/tag/RELEASE.2017-02-26T20-20-56Z // 安装 yu ...

  8. nginx time_wait 较多优化

    1. 查看命令   netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'   结果 ESTABLISHED 22 F ...

  9. 【精品分享一】JAVA 精品图书高清PDF下载

    更多图书尽在第一云教育book.1eduyun.com JAVA核心技术卷2:高级特征(原书第8版) JAVA软件开发从新手到高手(Java从入门到精通(第3版)+Java项目开发全程实录(第3版)) ...

  10. ③SpringBoot中Redis的使用

    本文基于前面的springBoot系列文章进行学习,主要介绍redis的使用. SpringBoot对常用的数据库支持外,对NoSQL 数据库也进行了封装自动化. redis介绍 Redis是目前业界 ...