C++ Priority Queue Lazy Solution


  • 0
    F

    There are some wicked three pointer solutions floating around, but actually there is no need to think about this problem very hard if the C++ priority queue is used.

    class Solution {
    public:
      int nthUglyNumber(int n) {
        set<long long> Q;
        Q.insert(1);
        int elts_seen = 1;
        for ( const long long & q : Q ) {
          if ( elts_seen == n ) {
            return q;
          }
          Q.insert(2*q);
          Q.insert(3*q);
          Q.insert(5*q);
          ++elts_seen;
        }
        return -1;
      }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.