【问题标题】:Finding the lowest missing integer in a vector containing positive and negative int's?在包含正整数和负整数的向量中找到最小的缺失整数?
【发布时间】:2017-01-11 14:31:51
【问题描述】:

我正在编写一个操作来查找向量的最低缺失元素,V = 1..N + 1。这必须以 O(N) 时间复杂度执行。

解决方案一:

std::vector<int> A {3,4,1,4,6,7};

int main()
{
    int max_el = *std::max_element(A.begin(), A.end()); //Find max element
    std::vector<int> V(max_el);
    std::iota(V.begin(), V.end(), 1) //Populate V with all int's up to max element

    for(unsigned into i {0}; i < A.size(); i++)
    {
       int index = A[i] - 1;
       if(A[i] == V[index]) //Search V in O(1)
       {
         V[index] = max_el; //Set each to max_el, leaving the missing int 
       }
    }
    return *std::min_element(V.begin(), V.end()); //Find missing int as its the lowest (hasn't been set to max_el)
}

//Output: 2

这完全没问题。

但是,我现在正试图让它与包含负整数的向量一起使用。

解决方案二:

我的逻辑是采用相同的方法,但是根据向量的大小和向量中负整数的数量对索引进行“加权”:

std::vector<int> A {-1, -4, -2, 0, 3, 2, 1}
int main()
{
   int max_el = *std::max_element(A.begin(), A.end());
   int min_el = *std::min_element(A.begin(), A.end());
   int min_el_abs = abs(min_el); //Convert min element to absolute
   int total = min_el_abs + max_el;

   std::vector<int> V(total + 1);
   std::iota(V.begin(), V.end(), min_el);
   int index;

   //Find amount of negative int's
   int first_pos;
   for(unsigned int i {0}; i < A.size(); i++)
   {
      if(A[i] >= 0) {first_pos = i; break;}
   }

   for(unsigned int i {0}; i < A.size(); i++)
   {
      if(A[i] <= 0) //If negative
      {
          index = (A.size() - first_pos) - abs(A[i]);
       } else 
       {
          index = (A[i] + 1) + first_pos;
       }
       if(A[i] == V[index])
       {
          V[index] = 0;
       }
    } 
    return *std::min_element(V.begin(), V.end());
 } 

 //Output: -3

解决方案 2 无法比较两个向量(A 和 V)的值,因为使用上述 positive int 方法计算 index 不起作用。

1) 如何让我的解决方案 2 使用负整数的无序向量?

2) 如何编辑我的解决方案 2 以使用正向量和负整数向量?

【问题讨论】:

  • 为什么不起作用?它做了什么不该做或不该做的事情?
  • 为负整数创建另一个向量并使用i*(-1)而不是i作为索引
  • 如果您在向量中放置一个较大的值(如果 N 是向量的大小,而不是最大值),那么您的解决方案 1 甚至不是 O(N)。如果对值有限制,请将它们放在问题中。将该解决方案扩展到负值就像找到最小值并从所有值中减去它,然后将其添加回最终答案一样简单。
  • 分配V向量并用std::iota填充都是O(max_el)。

标签: c++ vector


【解决方案1】:

您的第一个解决方案似乎是 O(max(N,M)),其中我认为 N 是向量 A 中的元素数,M 是向量 V 的大小(或 max(Ai)),但是您正在多次循环遍历这两个向量(std::min_elementstd::max_elementfor 循环、V 和 std::iota 的分配也是如此)。

此外,一旦更正了几个拼写错误(缺少;into 而不是int),您的程序会返回找到的值...来自main(),这有点奇怪。

您的第一个算法总是在范围 [1,A 中的最大值] 中搜索最小缺失值,但它可以概括为在范围 [ min(Ai), max(Ai)],即使是负数。

我的方法类似于L.Senioins,但我使用了不同的库函数来尽量减少循环数。

#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

template <class ForwardIt>
typename std::iterator_traits<ForwardIt>::value_type
lowest_missing(ForwardIt first, ForwardIt last)
{
    if ( first == last )
        throw std::string {"The range is empty"};
    // find both min and max element with one function
    auto result = std::minmax_element(first, last);

    // range is always > 0  
    auto range = *result.second - *result.first + 1;
    if ( range < 2 )
        throw std::string {"Min equals max, so there are no missing elements"};
    std::vector<bool> vb(range); // the initial value of all elements is false

    for (auto i = first; i != last; ++i)
        vb[*i - *result.first] = true;

    // search the first false
    auto pos = std::find(vb.cbegin(), vb.cend(), false);
    if ( pos == vb.cend() )  // all the elements are true
        throw std::string {"There are no missing elements"};

    return std::distance(vb.cbegin(), pos) + *result.first;
}

template <class ForwardIt>
void show_the_first_missing_element(ForwardIt first, ForwardIt last)
{
    try
    {
        std::cout << lowest_missing(first, last) << '\n';
    }
    catch(const std::string &msg)
    {
        std::cout << msg << '\n';
    }
}

int main() {
    std::vector<int> a { 1, 8, 9, 6, 2, 5, 3, 0 };
    show_the_first_missing_element(a.cbegin(), a.cend());

    std::vector<int> b { -1, -4, 8, 1, -3, -2, 10, 0 };
    show_the_first_missing_element(b.cbegin(), b.cend());
    show_the_first_missing_element(b.cbegin() + b.size() / 2, b.cend());

    std::vector<int> c { -2, -1, 0, 1, 2, 3 };
    show_the_first_missing_element(c.cbegin(), c.cend());

    std::vector<int> d { 3, 3, 3 };
    show_the_first_missing_element(d.cbegin(), d.cend());

    std::vector<int> e;
    show_the_first_missing_element(e.cbegin(), e.cend());

    return 0;
}

我的测试用例输出的结果是:

4 2 -1 没有缺失的元素 最小值等于最大值,所以没有缺失元素 范围为空

【讨论】:

    【解决方案2】:

    我的解决方案是制作一个bool 向量(或char 向量,以避免关于转换为bool 的编译警告),它具有所有可能元素的大小。所有元素都初始化为 0,然后分配为 1,这表明该元素没有丢失。然后,您需要做的就是找到第一个 0 元素的索引,它是最低缺失元素。

    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    std::vector<int> A{ -1, 0, 11, 1, 10, -5 };
    
    int main() {
        if (A.size() > 1) {
            int max_el = *std::max_element(A.begin(), A.end());
            int min_el = *std::min_element(A.begin(), A.end());
            int range = abs(max_el - min_el) + 1;
    
            std::vector<int> V(range, 0);
    
            for (size_t i = 0; i < A.size(); i++)
                V[A[i] - min_el] = 1;
    
            if (*std::min_element(V.begin(), V.end()) == 0)
                std::cout << std::distance(V.begin(), std::find(V.begin(), V.end(), 0)) + min_el;
            else
                std::cout << "There are no missing elements" << std::endl;
        }
        else
            std::cout << "There are no missing elements" << std::endl;
    
        std::cin.get();
    }
    

    【讨论】:

      【解决方案3】:

      在考虑了一段时间后,我将尝试给自己的问题一个答案:

      int main()
      {
        std::vector<int> A {-3, -1, 0, 1, 3, 4};
        auto relative_pos = std::minmax_elment(A.begin(), A.end());
        std::vector<bool> Litmus( *(relative_pos.second) - *(relative_pos.first), false); //Create vector of size max val - min val)
      
        auto lowest_val = *(relative_pos.first);
        for(auto x : A)
        {
           Litmus[i - lowest_val] = true;
        }
        auto pos = std::find(Litmus.begin(), Litmus.end(), false); //Find the first occurring false value
        std::cout<< (pos - Litmus.begin()) + lower<<std::endl; //Print the val in A relative to false value in Litmus
      }
      

      此解决方案适用于负数并且是线性的。

      【讨论】:

        【解决方案4】:
        #include <vector>
        #include <iostream>
        #include <string>
        #include <algorithm>
        #include <numeric>
        int solution(vector<int> &A) {
            std::vector<int>::iterator it = std::max_element(A.begin(),A.end()); 
            try
            {
                sort(A.begin(),A.end());    
                std::vector<int>::iterator it = std::unique(A.begin(),A.end());
                A.resize(std::distance(A.begin(),it));
        
                for(int i = 0, j = 1; i < A.size(); i++)
                {
                    if( A[i] != j)          
                    {
                        return j;
                    }           
                    j++;
                }
        
            }
            catch(exception &e)
            {
                std::cout<<e.what()<<std::endl;
            }
            return ++(*it);
        }
        

        【讨论】:

          猜你喜欢
          • 2019-04-27
          • 2017-01-20
          • 1970-01-01
          • 2017-06-24
          • 2022-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多