The开发者_Python百科 approach I'm referring to is the dual-pointer technique. Where the first pointer is a straightforward iterator and the second pointer goes through only all previous values relative to the first pointer.
That way, less work is done than if, for each node, we compared against every other node. Which would end up being O(n^2).
My question is what is the speed of the dual-pointer method and why?
So if you have N
elements in the list, doing your de-duping on element i
will require i
comparisons (there are i
values behind it). So, we can set up the total number of comparisons as sum[i = 0 to N] i
. This summation evaluates to N(N+1)/2
, which is strictly less than N^2
for N > 1
.
Edit: To solve the summation, you can approach it like this.
1 + 2 + 3 + 4 + ... + (n-2) + (n-1) + n
From here, you can match up numbers from opposite sides. This can then become
2 + 3 + ... + (n-1) + (n+1)
by matching up the 1
at the start with the n
at the end. Do the same with 2
and (n-1)
.
3 + ... + (n-1+2) + (n+1)
simplify to become
3 + ... + (n+1) + (n+1)
You can repeat this n/2
times, since you are matching up two number each time. This will leave us with n/2
occurances of the term (n+1)
. Multiplying those and simplifying, we get (n+1)(n/2)
or n(n+1)/2
.
See here for more description.
Also, this suggests this summation still has a big-theta of n^2
.
精彩评论