We're a place where coders share, stay up-to-date and grow their careers.
My solutions in Haskell.
The first solution - inefficient(O(n) = n^3), yet very elegant looking:
O(n) = n^3
import Data.List (nub) solve :: Eq a => [[a]] -> Int solve = product . map (length . nub)
Slightly better solution (O(n) = n^2 * log n) using containers library:
O(n) = n^2 * log n
containers
import qualified Data.Set as Set solve :: Ord a => [[a]] -> Int solve = product . map (Set.size . Set.fromList)
And best solution (O(n) = n^2), but accepts only integers as input:
O(n) = n^2
import qualified Data.IntSet as Set solve :: [[Int]] -> Int solve = product . map (Set.size . Set.fromList)
My solutions in Haskell.
The first solution - inefficient(
O(n) = n^3
), yet very elegant looking:Slightly better solution (
O(n) = n^2 * log n
) usingcontainers
library:And best solution (
O(n) = n^2
), but accepts only integers as input: