How would you describe the following in Big-O notation?
rotors = [1,2,3,4,5 ...] widgets = ['a', 'b', 'c', 'd', 'e' ...] assert len(rotors) == len(widgets) for r in rotors: for w in widgets: ... del widgets[0]
It O (n ^ 2). You can see that the amount of execution of the inner loop:
n + (n - 1) + (n - 2) + ... + 1
because one widget is removed every iteration of the outer loop. This is (n ^ 2 + n) / 2, which is O (n ^ 2).
as you go through both O (n ^ 2) loops.
- :
assert len(rotors) == len(widgets)
O (n 2) , , , O (m * n).
O (n ^ 2).
O (n ^ 2), , .
The first loop you have n widgets. The second loop you have n-1 widgets. ... The n-1 loop you have 2 widgets. The n loop you have 1 widgets.
, Big-O, 1/2.
, n + (n-1) +... + 2 + 1 = n (n + 1)/2. N , n ^ 2/2, O (n ^ 2)
, , , . , , O (n ^ 2), , , . , , , , O (1), O (n ^ 2).
Yes, why these big problems are always hard to understand. But if I were to assume that I would say O (n ^ 2), since you go through 2 for loops doing some operation each time.
Source: https://habr.com/ru/post/1744174/More articles:Sending information over a socket in C # - c #Missing jQuery calendar arrows - jquerySound player jQuery - javascriptHow can I use Tier Pricing with custom products? (Magento 1.4+) - zend-frameworkPrevent Excel 2007 from showing a message box of specific names? - vbaHow #import searches for files in Objective-C - importtor api in own project - torhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1744177/using-tcp-acks-to-measure-latency-to-a-server&usg=ALkJrhiYR_HoowEwamacFh9krukO6uH5TwIs there a way to reduce the verbosity of using String.Format (...., p1, p2, p3)? - c #Finding Dojo TreeGrid Workspace Widget Examples and Source - dojoAll Articles