optimization - Haskell: Can this code be optimized? -


i have following code. want know whether there way can optimized runs faster.

i wanted solve using segmenttree not versed haskell, why took following list (non-tree) approach.

-- program execution begins here main :: io() main =   _ <- getline   arr <- map (read :: string -> integer) . words <$> getline   querylist <- readdata   let queries = map (read :: string -> integer) querylist   putstrln ""   mapm_ print $ compute queries arr    -- construct sublist sublist :: integer -> integer -> [integer] -> [integer] sublist start end list = take (frominteger end - frominteger start) . drop (frominteger start) $ list  -- calculate resulting list compute :: [integer] -> [integer] -> [integer] compute [_] [_] = [] compute [_] [] = [] compute [_] (_:_) = [] compute [] (_:_) = [] compute [] [] = [] compute (x1:x2:xs) list = result : compute xs list   result = frequency $ sublist x1 x2 list   -- read query list, end @ terminating condition readdata :: io [string] readdata =   x <- getline   if x == "0"     return []     else xs <- readdata             return (words x ++ xs)  -- return count of frequent element in list frequency :: [integer] -> integer frequency list = tointeger (snd $ maximumby (compare `on` snd) counts)   counts = nub [(element, count) | element <- list, let count = length (filter (element ==) list)] 

thanks.

precompute frequencies of each prefix of list. compute frequencies of sublist, subtract frequencies of 2 prefixes ending @ 2 ends of sublist. reduce cost of each query o(n^2) o(n). compute frequencies, use counting sort. reduce cost of precomputation o(n^2) o(n log n).


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -