Lazy evaluation makes it simple (Haskell)


  • 0
    K

    Time complexity: O(KN)

    generateMuls :: [Integer] -> [Integer]
    generateMuls as = ds
      where
        ds = 1 : (merge $ map (\x -> map (*x) ds) as)
    
    merge :: [[Integer]] -> [Integer]
    merge ints = minV : merge left
      where minV = minimum $ map head ints
            left = do
                vs <- ints
                if (head vs == minV) then (return $ tail vs) else (return vs)
    
    nthSuperUglyNumber ::  Int -> [Integer] -> Integer
    nthSuperUglyNumber n as = (generateMuls as ) !! (n - 1)
    
    

Log in to reply
 

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