Is the time limit too strict for Ruby?
First I tried a solution based on Ugly Number II, but got TLE.\n\n    def nth_super_ugly_number(n, primes)\n        nums = Array.new(n, 1)\n      qs, is = Array
First I tried a solution based on Ugly Number II, but got TLE.\n\n    def nth_super_ugly_number(n, primes)\n        nums = Array.new(n, 1)\n      qs, is = Array.new(primes.size, 1), Array.new(primes.size, 0)\n    \n      1.upto(n - 1) do |i|\n        primes.each_with_index { |prime, idx| qs[idx] = nums[is[idx]] * prime }\n        nums[i] = qs.min\n        is.each_with_index { |_, idx| is[idx] += 1 if qs[idx] == nums[i] }\n      end\n    \n      nums[n - 1]\n    end\n\n\nThen I tryied to solve it using a priority queue, but still got LTE. (**Note Ruby has no built-in priority queue, you have to implement it yourself**)\n\n    class PriorityQueue\n      def initialize(cmp)\n        @queue, @cmp = [], cmp\n      end\n    \n      def <<(value)\n        @queue << value; _swim_(@queue.size - 1); self\n      end\n    \n      def shift\n        @queue[0], @queue[-1] = @queue[-1], @queue[0]\n        @queue.pop.tap { _sink_(0) }\n      end\n    \n      def peek\n        @queue[0]\n      end\n    \n      def empty?\n        @queue.empty?\n      end\n    \n      private def _swim_(k)\n        while k > 0 && @cmp.call(@queue[k], @queue[(k - 1) / 2])\n          @queue[k], @queue[(k - 1) / 2] = @queue[(k - 1) / 2], @queue[k]\n          k = (k - 1) / 2\n        end\n      end\n    \n      private def _sink_(k)\n        while k * 2 + 1 < @queue.size\n          kk  = k * 2 + 1\n          kk += 1 if @queue[kk + 1] && @cmp.call(@queue[kk + 1], @queue[kk])\n          break if @cmp.call(@queue[k], @queue[kk])\n    \n          @queue[k], @queue[kk] = @queue[kk], @queue[k]\n          k = kk\n        end\n      end\n    end\n    \n    \n    # @param {Integer} n\n    # @param {Integer[]} primes\n    # @return {Integer}\n    def nth_super_ugly_number(n, primes)\n       nums = Array.new(n, 1)\n      minpq = PriorityQueue.new(Proc.new{ |(_, _, n1), (_, _, n2)| n1 < n2 })\n    \n      primes.each_with_index do |prime, index|\n        minpq << [index, prime, prime]\n      end\n    \n      1.upto(n - 1) do |i|\n        nums[i] = minpq.peek[2]\n    \n        loop do\n          index, prime, _ = minpq.shift\n          minpq << [index + 1, prime, nums[index] * prime]\n          break if minpq.empty? || minpq.peek[2] != nums[i]\n        end\n      end\n    \n      nums[n - 1]\n    end\n\n\nI did some tricks to speed up, but failed. Any suggestion?
['Ruby']