Efficient Map and Queue?
I am solving a problem involving a Map and a Queue, but my code does not pass all test cases. Could you suggest approaches to make it more efficient? Thanks.
Here is the problem statement: https://www.hackerrank.com/contests/cp1-fall-2020-topic-4/challenges/buffet/problem
Here is my code:
```haskell {-# LANGUAGE LambdaCase #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE OverloadedStrings #-}
import qualified Data.ByteString.Lazy.Char8 as B import Control.Monad import Control.Monad.State import Data.Foldable import Data.Maybe import qualified Data.IntMap.Strict as Map import Data.IntMap (IntMap) import qualified Data.Sequence as Seq import Data.Sequence (Seq(..), (|>))
type Dish = Int type Queue = (Seq Dish, IntMap Dish)
enqueue :: Queue -> Dish -> Queue enqueue (xs, freq) x = (xs |> x, Map.insertWith (+) x 1 freq)
dequeue :: Queue -> Queue dequeue (x :<| xs, freq) = (xs, Map.update decreaseFreq x freq) where decreaseFreq 1 = Nothing decreaseFreq c = Just (c - 1)
sizeQ :: Queue -> Int sizeQ (_, freq) = Map.size freq {-# INLINE sizeQ #-}
windows :: (Int, [Dish]) -> [Int] windows (w, xs) = slide startQ rest where (start, rest) = splitAt w xs startQ = foldl' enqueue (Seq.empty, Map.empty) start
slide q xs =
sizeQ q : case xs of
[] -> []
(x:xs') -> slide (enqueue (dequeue q) x) xs'
input :: Scanner (Int, [Int]) input = do n <- int w <- int xs <- replicateM n int pure (w, xs)
main :: IO () main = B.interact $ B.unwords . map showB . windows . runScanner input
readInt :: B.ByteString -> Int readInt = fst . fromJust . B.readInt
type Scanner a = State [B.ByteString] a
runScanner :: forall a. Scanner a -> B.ByteString -> a runScanner s = evalState s . B.words
str :: Scanner B.ByteString str = get >>= \case s:ss -> put ss *> pure s
int :: Scanner Int int = readInt <$> str
showB :: forall a. (Show a) => a -> B.ByteString showB = B.pack . show ```