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)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
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:Slightly better solution (
O(n) = n^2 * log n
) usingcontainers
library:And best solution (
O(n) = n^2
), but accepts only integers as input: