Python Forum
[split] Time Complexity of Counting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Time Complexity of Counting
#3
What you wrote is O(n^2).  The version I wrote is O(n).
Counting all elements (even of a sublist) every time through the loop is inefficient and pushes the algorithm to quadratic time.

Also note you could just use list.count (or tuple.count depending) for your method:
sequence = 48,52,55,48,52,55,60,62,48
result = [sequence[:i].count(val) for i,val in enumerate(sequence, 1)] 
but this is still O(n^2) which should be avoided.
Reply


Messages In This Thread
[split] Time Complexity of Counting - by Mekire - Jan-18-2017, 07:52 PM
RE: [split] Time Complexity of Counting - by wavic - Jan-18-2017, 10:48 PM
RE: [split] Time Complexity of Counting - by buran - Jan-10-2019, 10:11 AM
RE: List showing running total of instances - by Mekire - Jan-18-2017, 09:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  counting lines in split data Skaperen 6 3,231 Oct-07-2022, 07:09 PM
Last Post: Skaperen
  Split gps files based on time (text splitting) dervast 0 2,419 Nov-09-2020, 09:19 AM
Last Post: dervast
  What is the run time complexity of this code and please explain? samlee916 2 3,070 Nov-06-2020, 02:37 PM
Last Post: deanhystad
  Basic Time Complexity Help Adakip 1 2,432 Aug-25-2019, 09:12 AM
Last Post: ThomasL
  Time complexity of dict vs attributes heras 6 5,298 Aug-24-2018, 10:18 PM
Last Post: Gribouillis
  How to calculate the mccabe complexity in module falke8? fuqianz 1 3,802 Oct-16-2017, 02:08 AM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020