Minimum window of days required to travel all cities

2024/9/19 17:55:07

This is an interesting question that I came across in a coding challenge:

There are k cities and n days. A travel agent is going to show you city k on day n. You're supposed to find the minimum number of days in which you can visit all cities. You're also allowed to visit cities more than once, but ideally you wouldn't want to do that since you want to minimize the number of days.

Input :You're given an array of days and cities where days are indices and cities are values. A=[7,4,7,3,4,1,7] So A[0]=7 means travel agent will show you city 7 on day 0, city 4 on day 1 etc.

So Here if you start out on day 0, you'll have visited all cities by day 5, but you can also start on day 2 and finish up on day 5.

Output:4 Because it took you 4 days to visit all the cities at least once

My solution : I do have an O(N^2) solution that tries out all combinations of cities. But the test said that the ideal time and space complexity should be O(N). How do I do this?

def findmin(A):hashtable1={}locationcount=0#get the number of unique locationsfor x in A:if A[x] not in hashtable1:locationcount+=1index1=0daycount=sys.maxinthashtable2={}#brute forcewhile index1<len(A):index2=index1prevday=index2ans=0count1=0while index2<len(A):if A[index2] not in hashtable2:count1+=1ans+=(index2-prevday)hashtable2[A[index2]]=1index2+=1if count1==count:daycount=min(ans,daycount)hashtable2.clear()index1+=1return daycount+1
Answer

This problem might be solved with two-pointer approach.

Some data structure should contain element counts in current window. Perhaps your hash table is suitable.

Set left and right pointer to the start of list.

Move right pointer, incrementing table entries for elements like this:

  hashtable2[A[rightindex]] = hashtable2[A[rightindex]] + 1

When all (locationcount) table entries become non-zero, stop moving right pointer. You have left-right interval covering all cities. Remember interval length.

Now move left pointer, decrementing table entries. When some table entry becomes zero, stop moving left pointer.

Move right pointer again. Repeat until the list end.

Note that indexes run the list only once, and complexity is linear (if table entry update is O(1), as hash map provides in average)

https://en.xdnf.cn/q/72523.html

Related Q&A

Force Nosetests to Use Python 2.7 instead of 3.4

Ive been learning Python using version 3.4. I recently started learning Web.py so have been using Python 2.7 for that, since web.py not supported in Python 3.4. I have nose 1.3.4 module installed for …

Python regex not to match http://

I am facing a problem to match and replace certain words, not contained in http:// Present Regex: http://.*?\s+This matches the pattern http://www.egg1.com http://www.egg2.com I need a regex to matc…

how can I maintain sequence of my list using set?

In [1]: l1 = [a,2,3,0,9.0,0,2,6,b,a]In [2]: l2 = list(set(l1))In [3]: l2 Out[3]: [a, 0, 2, 3, 6, 9.0, b]Here you can see the the list l2 is falling with different sequence then the original l1, I need …

How to reference a dict object?

I have a Python dict object d. d = {a: 1, b: 2, c: 3}. My problem is really simple. I want to reference a variable to the elements of d. For example, something like:In[1]: p = d[a] >>> p = 1 I…

Python 3.3: DeprecationWarning when using nose.tools.assert_equals

I am using nosetest tools for asserting a python unittest:... from nose.tools import assert_equals, assert_almost_equalclass TestPolycircles(unittest.TestCase):def setUp(self):self.latitude = 32.074322…

Panel/Hvplot interaction when variable is changing

Im trying to create a dashboard with two holoviews objects: a panel pn.widgets.Select object that contains a list of xarray variables, and a hvplot object that takes the selected variable on input, lik…

asyncio matplotlib show() still freezes program

I wish to run a simulation while at the same time output its progress in a plot. Ive been looking through a lot of examples of threading and multiprocessing, but they are all pretty complex. So I thoug…

celery .delay hangs (recent, not an auth problem)

I am running Celery 2.2.4/djCelery 2.2.4, using RabbitMQ 2.1.1 as a backend. I recently brought online two new celery servers -- I had been running 2 workers across two machines with a total of ~18 thr…

Python ttk.combobox force post/open

I am trying to extend the ttk combobox class to allow autosuggestion. the code I have far works well, but I would like to get it to show the dropdown once some text has been entered without removing fo…

How to pull from the remote using dulwich?

How to do something like git pull in python dulwich library.