Sunday, October 13, 2013

Merge K sorted Lists@leetcode

刷题必备书籍Cracking the Coding Interview: 150 Programming Questions and Solutions 

简历:The Google Resume: How to Prepare for a Career and Land a Job at Apple, Microsoft, Google, or any Top Tech Company
算法学习书籍:Introduction to Algorithms
编程珠玑:Programming Pearls (2nd Edition)
C++ 学习:The C++ Programming Language, 4th Edition
经典操作系统书籍,龙书:Operating System Concepts
创业:The Start-up of You: Adapt to the Future, Invest in Yourself, and Transform Your Career

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
两种办法,第一种很straight forward, 直接merge two sorted lists, 然后对于每一个lists,都如此合并,但时间复杂度消耗比较高。e.g, n lists, every list has k nodes. O(knn). 因为每次合并之后的lists长度都会变长。
第二种用min-heap来动态地给minimun node in all lists. time complexity is O(knlogk). (介个我不太会。。因为没咋建过堆)
第三种是我最稀饭的,用multiset来动态地保存当前最小的元素,然后比较完一个就把那个list的后面那个扔到multiset里面去。

Multisets are containers that store elements following a specific order, and where multiple elements can have equivalent values.

In a multiset, the value of an element also identifies it (the value is itself the key, of type T). The value of the elements in a multiset cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.

Internally, the elements in a multiset are always sorted following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).

multiset containers are generally slower than unordered_multiset containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.

Multisets are typically implemented as binary search trees.




5 comments:

  1. It seems the multiset functions similarly as priority queue. Any difference between them? Some threads suggests differences in performance for some compilers.

    ReplyDelete
  2. 写的好,继续跪读中...

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

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

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

    ReplyDelete

Leetcode 316. Remove Duplicate Letters

 这道题表面问的是如何删除重复,实际在问如何从多个字符选取一个保留,从而让整个字符串按升序排列。那么策略就是对于高顺位的字符比如‘a',就要选靠前位置的保留,而低顺位字符如’z'则应该尽量选取靠后位置保留。 算法大概思路:每看到一个字符,我们要决定是否保留 1. ...