【问题标题】:Need to find the next greater element of every element in an array [duplicate]需要找到数组中每个元素的下一个更大的元素[重复]
【发布时间】:2014-07-28 23:26:59
【问题描述】:

算法说明

对于输入数组中的每个元素,对应的输出是输入元素后面的第一个数字,大于输入元素。

换句话说,对于给定的 input[i],output[i] 是某个元素 input[j],其中 j 是满足 j > i 且 input[j] > input[i] 的最小索引

示例

Input  12 15 22  9  7  2 18 23 27 
Output 15 22 23 18 18 18 23 27 -1

例如,对应于 9 的输出是 18,因为 18 是数组中满足这些要求的第一个数字

  1. 在输入数组中跟随 9
  2. 大于 9

问题

谁能给我推荐一个比 O(n^2) 更好的算法?

【问题讨论】:

  • 您对此事有何看法?你认为什么可行?
  • 我一定错过了什么。为什么18重复了3次,为什么最后是-1?
  • 18 被重复,因为 9 , 7 , 2 - 18 是下一个更大的数字,而 -1 是因为 27 之后没有更大的数字。
  • @instance 你的问题是模棱两可的 - 你想用原始序列中大于它的 x 之后的第一个值替换 x ,还是用所有元素中大于 x 的最小值在 x 之后?
  • @templatetypedef 我想用新元素替换每个 a[i],它是下一个比 a[i] 更大的元素。

标签: algorithm


【解决方案1】:

一种方法是使用堆栈,堆栈中的每个条目都是一个值:索引对。遍历输入数组,从堆栈中弹出值小于输入数组中当前项的值的项。从堆栈中弹出所有较小的值后,将当前值:索引对压入堆栈。当到达输入数组的末尾时,堆栈中的任何剩余条目都会得到输出值 -1,表示没有找到更大的数字。

使用问题中的示例,以下是算法的工作原理

input item 12
    stack = 12:0

input item 15
    pop 12:0 and set output[0] = 15
    stack = 15:1

input item 22
    pop 15:1 and set output[1] = 22
    stack = 22:2

input item 9
    stack = 9:3, 22:2

input item 7
    stack = 7:4, 9:3, 22:2

input item 2
    stack = 2:5, 7:4, 9:3, 22:2

input item 18
    pop 2:5 and set output[5] = 18
    pop 7:4 and set output[4] = 18
    pop 9:3 and set output[3] = 18
    stack = 18:6, 22:2

input item 23
    pop 18:6 and set output[6] = 23
    pop 22:2 and set output[2] = 23
    stack = 23:7

input item 27
    pop 23:7 set output[7]= 27
    stack = 27:8

end of array
    pop 27:8 and set output[8] = -1
    done

【讨论】:

    【解决方案2】:

    这可以在O(N) 时间和O(N) 额外内存空间中借助两个堆栈(一个用于索引,另一个用于值)来完成。

    我会在你的例子的帮助下解释算法。

    Input  12 15 22  9  7  2 18 23 27 
    
    Initialize Output Array O[] as all -1.
    
    1. Start from the first element. Set CurrentElement = A[0] (12). index = 0   
    2. Push A[index] in a Stack S_values. Push index in a Stack S_indices.  
    3. Increment index.  
    4. while ( S_values is not empty && A[index] is > than S_values.top() )  
       - Set output_index = S_indices.top()
       - set O[output_index] = A[index].  
       - S_values.pop()
       - S_indices.pop().      
    5. If index < length(Input)-1 Goto Step 2.
    6. Set O[index] = -1. // Last element.
    

    这是可行的,因为堆栈顶部S_values 始终具有最低值,并且元素应按升序从其中弹出。类似地,堆栈S_indices 应始终在顶部具有最大值,并且应按降序弹出索引。

    编辑:C++ 中的代码

    #include <vector>
    #include <stack>
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::vector;
    using std::stack;
    
    int main()
    {
        vector<int> Input = { 12, 15, 22,  9,  7,  2, 18, 23, 27};
        vector<int> Output( Input.size(), -1 );
    
        stack<int> S_values, S_indices;
        S_values.push( Input[0] );
        S_indices.push( 0 );
        for ( size_t index = 1; index < Input.size(); ++index )
        {
            while ( !S_values.empty() && Input[index] > S_values.top() )
            {
                size_t output_index = S_indices.top();
                Output[ output_index ] = Input[ index ];
                S_values.pop();
                S_indices.pop();
    
            }
            S_values.push( Input[index] );
            S_indices.push( index );
        }
    
        for ( auto &x : Output )
            cout << x << " ";
        cout << endl;
    
        return 0;
    }
    

    输出:

    15 22 23 18 18 18 23 27 -1
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 2022-12-31
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    相关资源
    最近更新 更多