c++ - Most efficient way to find whether all objects belong to the same group? -


Then I have every item with the integer field group number. I want to find the most efficient time that all the objects are same Whether or not the group belongs to. Can it be done in C ++ in less time than C (O)? Without doing some preprocessing outside of your function, you can not do better than O (n)

, Such as o (1).

To know that all the elements belong to the same group (at least in the worst case), you will need to look at all the elements to determine your group, which makes it instantly Is (n).

The simplest algorithm looks like this (Python Pseudocode):

  def all_same_group (element): group 0 = element [0]. In # group elements, first element # on the remaining elements of the element, repeat [1:]: if element.group! = Group0: return False # element has a different group, not exactly the same True # We did not come back in the loop, so all the same   

Only two groups believe that this is the most Will be in good case (where there are different groups of first and second elements) perform in o (1). Assuming the same possibilities of the random sequence of two groups and elements, the log (n) elements will need to be examined. In the worst case it is still O (N) where you need to check all the elements.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -