Saturday, 24 March 2018

Given two sorted linked lists. Find the path of maximum sum.

Given two sorted linked lists. You start with a one of the two lists and then move till the end. You may switch to the other list only at the point of intersection (which mean the two node with the same value in different lists.) You have to find the path of maximum sum.

Eg
  1->3->30->90->120->240->511
  0->3->12->32->90->125->240->249
  You can switch at 3 90 or 240 so the max sum paths is
  1->3->12->32->90->125->240->511 
Sol:

This can be solved in O(m+n).

Take two pointers p1 and p2 for both lists. 
Maintain two sums curr1 and curr2 for each list init to 0.
  1. If p1 == p2: 
    • if curr1 > curr2: Choose LL1 as path upto this point.
    • else: Choose LL2 as path.
  2. If p1 < p2:
    • curr1 += p1
    • increment p1 to next node.
  3. else if p2 < p1:
    • curr2 += p2
    • increment p2 to next node
  4. if p1 == null:
    • traverse all of p2 and keep incrementing curr2.
    • Take the path with greater sum
  5. if p2 == null: // do as above for p1

41 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I am a regular reader of your blog and being students it is great to read that your responsibilities have not prevented you from continuing your study and other activities. Love
    selenium Training in chennai


    amazon web services Training in chennai


    Block Chain Training in velachery

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.


    rpa training in chennai | best rpa training in chennai | rpa training in chennai | rpa training in bangalore
    rpa training in pune | rpa online training

    ReplyDelete
  5. The site was so nice, I found out about a lot of great things. I like the way you make your blog posts. Keep up the good work and may you gain success in the long run.
    Data Science training in Chennai | Data science training in bangalore
    Data science training in pune| Data science online training
    Python training in Kalyan nagar

    ReplyDelete
  6. I am a regular reader of your blog and being students it is great to read that your responsibilities have not prevented you from continuing your study and other activities.
    python training in tambaram
    python training in annanagar
    python training in jayanagar

    ReplyDelete
  7. Thanks for such a great article here. I was searching for something like this for quite a long time and at last I’ve found it on your blog. It was definitely interesting for me to read  about their market situation nowadays.
    Best Devops Training in pune

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
    Selenium training in chennai
    Selenium training institute in Chennai
    iOS Course Chennai
    Digital Marketing Training in Chennai
    java classes in chennai
    java j2ee training
    core java training in chennai

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. I am really enjoying reading your well written articles.
    It looks like you spend a lot of effort and time on your blog.
    I have bookmarked it and I am looking forward to reading new articles. Keep up the good work..
    Java Coaching Institutes in Bangalore
    Advanced Java Training Institute in Bangalore
    Best Institute For Java Course in Bangalore
    Java Training Classes in Bangalore

    ReplyDelete
  12. Your blog is so inspiring for the young generations.thanks for sharing your information with us and please update
    more new ideas.
    Selenium Certification Training in T nagar
    Selenium Courses in T nagar
    Selenium Training in OMR
    Selenium Training in Sholinganallur

    ReplyDelete
  13. Visit for Blockchain training in Bangalore:- Blockhain training in Bangalore

    ReplyDelete
  14. Attend The Data Scientist Courses From ExcelR. Practical Data Scientist Courses Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Scientist Courses. Data Scientist Courses

    ReplyDelete
  15. "Thank you for sharing wonderful information with us.Really useful for everyone data scientist courses
    "

    ReplyDelete
  16. It's a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work.
    data scientist course in hyderabad

    ReplyDelete

  17. In the wake of perusing your article, I was astounded. I realize that you clarify it well overall. What's more, I trust that different perusers will likewise encounter how I feel in the wake of perusing your article.
    data analytics training in hyderabad

    ReplyDelete
  18. I would like to thank you for the efforts you have made in writing this article. I am hoping for the same best work from you in the future as well. In fact your creative writing abilities have inspired me to start my own Blog Engine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.
    data scientist training in hyderabad

    ReplyDelete
  19. Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article.
    data scientist course in hyderabad

    ReplyDelete
  20. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work
    data science course in malaysia

    ReplyDelete
  21. Great article I appreciate the time and work you put into writing this insightful and fascinating essay.
    CA Coaching Centers in Hyderabad

    ReplyDelete