#πŸ”’ Improving Performance Of Colored Line Problem

41 messages Β· Page 1 of 1 (latest)

kindred mauve
#

I was doing a problem on a leetcode style website but I was failing some of the test cases due to my program being too slow. I will list the problem and my sudo code below. I would appreciate someone helping me find how I could have reduced the time complexity.

Problem:
You are given a number line of length length. You are given a list of instructions prompts that contains tuples of length 2. prompts[i][0] is a number corresponding with a number on the number line. prompts[i][1] is a number corresponding with a color. Go through the prompts and for each prompt color the number on the number line the given color. Then add the number of sequential pairs (zero does not count) to a results array. EX: line = [0,1,1] = 1 pair, line = [1,1,1] = 2 pair, line = [1,2,3] = 0 pair.

My strategy

def solution(length, prompts):
  nums = {p[0] for p in prompts}
  results = []
  line = {}
  
  for p in prompts:
    if p[0] - 1 not in nums and p[0] + 1 not in nums:
      results.append(results[-1])
      continue
    line[p[0]] = p[1]

    count = 0
    for key in line.keys():
      if key == line.get(key + 1):
        count += 1
    results.append(count)
  return results
cosmic nacelleBOT
#

@kindred mauve

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

kindred mauve
#

I am now seeing that a single prompt could not change the number of pairs by more than a couple

elder orchid
#

Is this the exact phrasing?

#

Just curious, I hate it.

kindred mauve
#

It could create 1 pair:
0100 -> 0110
0010 -> 0110
It could create 2 pair:
0101 -> 0111
It could remove 1 pair: reverse of above
It could remove 2 pair: reverse of above

This would make it O(len(prompts)) rather than O(len(prompts) ** 2)

kindred mauve
elder orchid
#

Is the number line an input?

kindred mauve
#

It is colored to all zeros to start

elder orchid
#

Oh, so you have a list of zeros of length length.

#

And, for each position in the prompts (the first element of the tuple), you're replacing that position with the second element of the tuple

kindred mauve
#

yes

elder orchid
#

So, each prompt changes exactly one element.

kindred mauve
#

yes

elder orchid
#

So, one strategy would be to sort the prompts, and iterate over them

outer pollen
# kindred mauve No i summarized because I dont have access to it anymore

What is the purpose of the length parameter? Is that just in case the programmer is using C?

What is the purpose of the colors? You talk about needing to "color" something, but it is very unclear what that really means.

Your examples are just more confusing.

line = [0,1,1] = 1 pair
What even is line? It's not an input parameter, you don't directly return it... I'm confused
Also what is that "1 pair" supposed to mean. You return a list, but this sounds like you need to return the number of pairs you can create

elder orchid
#

Ignoring the number line to begin with.

kindred mauve
outer pollen
kindred mauve
#

Colors are just a number. You can color a cell of the number line 1, or 2, or 3, etc.
I dont use length because I dont think I need it

kindred mauve
outer pollen
#

You usually don't get input parameters that don't affect function behaviour, especially in such challenges. Possible? Maybe. Odd? Definitely

elder orchid
#

Another strategy would be to iterate over each prompt, and keep track of which prompts have been visited

elder orchid
kindred mauve
kindred mauve
elder orchid
kindred mauve
#

yes

elder orchid
#

Oh, so only the last prompt for each position matters

kindred mauve
#

for the final state yes. But I have to calculate the number of pairs after each prompt is applied

elder orchid
#

Got it. So, sounds like a nice job for a dictionary.

kindred mauve
#

Yeah, I did use a dict. I think the core problem was that I was iterating over the entire "number line" dict even though each prompt cannot effect pairs far away from it

elder orchid
#

Could you; iterate over each prompt. For each prompt position, see if position-1 or +1 has the same color (a pair is formed).

#

So for each prompt, one dictionary value set, and two lookups

#

You don't need to even track the line itself.

kindred mauve
#

yeah I think that works

elder orchid
#

You'd still have to do a final count at the end, tho, unless you were also detecting 'breaking' of pairs

kindred mauve
#

It should be simple to detect breaking pairs. Just check if the value you are replacing was the same as the item before or after and if it was it broke a pair

#

!close

cosmic nacelleBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.