LeetCode——Next Greater Element I

Question

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.

Note:

All elements in nums1 and nums2 are unique.

The length of both nums1 and nums2 would not exceed 1000.

解题思路

想的就是过滤一遍第二个数组,把每个数右边比它大的第一个数存起来,然后遍历第一个数组,为每个元素找到第一个大的元素。

具体实现

class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
map<int, int> dict;
for (int i = 0; i < nums.size(); i++) {
int flag = 0;
int j = i + 1;
for (; j < nums.size(); j++) {
if (nums[j] > nums[i]) {
flag = 1;
break;
}
}
if (flag) {
dict[nums[i]] = nums[j];
} else {
dict[nums[i]] = -1;
}
} vector<int> res;
for (int i : findNums) {
res.push_back(dict[i]);
}
return res;
}
};

相关解答中,用到了栈来遍历第二个数组,这样的时间复杂度会降低到O(n),而以上这个算法的时间复杂度为O(n^2)。

class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
stack<int> s;
unordered_map<int, int> m;
for (int n : nums) {
while (s.size() && s.top() < n) {
m[s.top()] = n;
s.pop();
}
s.push(n);
}
vector<int> ans;
for (int n : findNums) ans.push_back(m.count(n) ? m[n] : -1);
return ans;
}
};

LeetCode——Next Greater Element I的更多相关文章

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

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

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

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

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

  4. LeetCode Next Greater Element III

    原题链接在这里:https://leetcode.com/problems/next-greater-element-iii/description/ 题目: Given a positive 32- ...

  5. LeetCode: Next Greater Element I

    stack和map用好就行 public class Solution { public int[] nextGreaterElement(int[] findNums, int[] nums) { ...

  6. [leetcode]Next Greater Element

    第一题:寻找子集合中每个元素在原集合中右边第一个比它大的数. 想到了用哈希表存这个数的位置,但是没有想到可以直接用哈希表存next great,用栈存还没找到的数,没遍历一个数就考察栈中的元素小,小的 ...

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

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

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

随机推荐

  1. ilbc编解码在android实现

    iLBC 是为专为提供稳健的 IP 语音通信而开发的语音 codec,以窄带语音为设计基础,具有 8 kHz 的采样率.iLBC codec 支持两种基本的帧长度:13.3 kbps 比特率下编码帧长 ...

  2. JZOJ.3769【NOI2015模拟8.14】A+B

    Description 对于每个数字x,我们总可以把它表示成一些斐波拉切数字之和,比如8 = 5 + 3,  而22 = 21 + 1,因此我们可以写成  x = a1 * Fib1 + a2 * F ...

  3. asp.net 上传XML,txt 直接读取文件内容

    if (GetUploadFileContent.PostedFile.InputStream.Length < 1) { Msg.Text = "请选择文件"; retur ...

  4. 【BZOJ1367】[Baltic2004]sequence 左偏树

    [BZOJ1367][Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sampl ...

  5. 160809、tomcat中配置多个域名及将tomcat配置成系统服务

    本地测试用的(注意红色部分) 第一步.自己的windows电脑,在c盘中有个hosts文件(搜索一下),做以下修改(其中127.0.0.1是本机地址,192.1638.10.139是我虚拟机中linu ...

  6. 自动适应label

    CGFloat btnH = 300; NSString *text=@"你在这是NSString的对象方法,一个字符串实例调用该方法时,方法会通过传入的参数返回一个CGRect型数据,这个 ...

  7. MyISAM Key Buffer 读/写/利用率(%) MylSAM平均每秒Key Buffer利用率(%) MylSAM平均每秒Key Buffer读命中率(%) MylSAM平均每秒Key Buffer写命中率(%)

    MyISAM Key Buffer 读/写/利用率(%) MylSAM平均每秒Key Buffer利用率(%)MylSAM平均每秒Key Buffer读命中率(%)MylSAM平均每秒Key Buff ...

  8. 请写出用于校验HTML文本框中输入的内容全部为数字的javascript代码

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html ...

  9. detectron安装+caffe2安装

    detectron安装+caffe2安装 因为想跑一下facebook最近开源的detectron物体检测平台,所以安装caffe2+detectron 总结: 一定要好好看官方安装教程:https: ...

  10. 通过文件对照工具Merge数据库

    项目分成线下开发版.线上測试版.线上生产版,因此相应有三个数据库. 对于一些静态数据.经常须要同步.改动了线下的开发版本号,同一时候也须要更新线上的測试版和线上生产版数据库,有时候线上的一些数据库改动 ...