简体   繁体   English

查找大于当前数字的最接近数字的索引

[英]Find the index of the closest number that is greater than the current number

For each integer in an array of positive integers, find the index of the closest integer that is greater than the current integer.对于正整数数组中的每个 integer,找到大于当前 integer 的最接近 integer 的索引。 Also, we need to search for the answer only to the left of the current integer.此外,我们只需要在当前 integer 的左侧搜索答案。

For example -例如 -

Input array - [ 5, 4, 3, 6, 2, 3]
Output array - [ -1, 0, 1, -1, 3, 3]

Assign -1 to those numbers which don't have an answer.将 -1 分配给那些没有答案的数字。

There is a simple O(n^2) method, for each number run a for loop from the previous number to the beginning of the array.有一个简单的 O(n^2) 方法,对于每个数字运行一个从前一个数字到数组开头的 for 循环。

for(int i=0; i<n; ++i)
{
    output[i] = -1;
    for(int j=i-1; j>=0; --j)
    {
        if(input[j] > input[i])
        {
            output[i] = j;
            break;
        }
    }
}

This method is inefficient when 'n' is large.当“n”很大时,此方法效率低下。 Is there a more efficient way?有没有更有效的方法?

The proposed answer is an adaption of: https://www.geeksforgeeks.org/find-the-nearest-smaller-numbers-on-left-side-in-an-array/建议的答案是对以下内容的改编: https://www.geeksforgeeks.org/find-the-nearest-smaller-numbers-on-left-side-in-an-array/

The main idea is to use a stack to remember processed value.主要思想是使用堆栈来记住处理后的值。 In the link, they care about the value but it can easily be adapted to output indices.在链接中,他们关心值,但它可以很容易地适应 output 索引。

#include <iostream>
#include <vector>
#include <stack>

std::vector<int> function(std::vector<int> input) {
    std::vector<int> output;
    output.reserve(input.size());

    // Create an empty stack 
    // first element of the pair is the index. second is the value
    std::stack<std::pair<int,int>> S; 
  
    // Traverse all array elements 
    for (int i=0; i<input.size(); i++) 
    { 
        // Keep removing top element from S while the top 
        // element is less than or equal to arr[i] 
        while (!S.empty() && S.top().second <= input[i]) 
            S.pop(); 
  
        // If all elements in S were greater than arr[i] 
        if (S.empty()) 
            output.push_back(-1);
        else  //Else print the nearest smaller element
            output.push_back(S.top().first);

  
        // Push this element 
        S.push({i, input[i]}); 
    } 
    
    return output;
}

int main() {
    std::vector<int> input{5, 4, 3, 6, 2, 3};
    std::vector<int> output = function(input);

    for(int index : output) {
        std::cout << index << ' ';
    }

    return 0;
}

Output: Output:

-1 0 1 -1 3 3

Compiler explorer: https://godbolt.org/z/8W3ecv编译器资源管理器: https://godbolt.org/z/8W3ecv

I believe one popular O(n) solution is to use a stack, maintaining a descending sequence (hopefully the algorithm is clear enough from the commented code):我相信一个流行的O(n)解决方案是使用堆栈,保持降序(希望算法从注释代码中足够清楚):

 function f(A){ let stack = [] let output = [] for (let i=0; i<A.length; i++){ // While there are lower or // equal elements on top of // the stack while (stack.length && A[ stack[stack.length-1] ] <= A[i]) stack.pop(); // The next greater element // to the left if (stack.length) output.push(stack[stack.length-1]); // There was none else output.push(-1); stack.push(i); } return output; } var As = [ [5, 4, 3, 6, 2, 3], [1, 2, 3, 4, 5], [5, 4, 3, 2, 1], [0, 3, -1, 5, 4] ]; for (let A of As){ console.log(`${ A }`); console.log(`${ f(A) }`); console.log(''); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM