zoukankan      html  css  js  c++  java
  • LeetCode——Next Greater Element I

    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;
        }
    };
    
  • 相关阅读:
    统计中的f检验和t检验的区别
    统计中的t检验
    降维工具箱drtool
    pca主成份分析方法
    经典相关分析,典型关分析, CCA,Canonical Correlation Analysis,多元变量分析,线性组合,相关系数最大化
    博客中插入公式——之在线数学公式生成
    Mathtype常用快捷键
    下标运算符重载
    赋值运算符的重载
    算术和关系运算符重载
  • 原文地址:https://www.cnblogs.com/zhonghuasong/p/6658245.html
Copyright © 2011-2022 走看看