Observation from the info provided in problem statement: 1. If I do not remember the order in which I visited these places, than off course i dont remember the starting location. 2. But I know my current location – end of my journey. 3. End place of one journey is starting place for another journey. So on the basis of these observation here is my algorithm: String[] places = new [N+1]; // N-1 links required to connect N places Step 1: Store all tickets in a HashMap with end location as key and ticket as value. It will take O(N) time and O(N) space. Where N = number of tickets Step 2:
Read full article from One-way flight trip problem « Ajeet Singh
No comments:
Post a Comment