Skip to content

Instantly share code, notes, and snippets.

@s-petersson
Created May 21, 2017 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-petersson/750bef567178221b7cfcb412dcf0aab0 to your computer and use it in GitHub Desktop.
Save s-petersson/750bef567178221b7cfcb412dcf0aab0 to your computer and use it in GitHub Desktop.
solveWithRepa :: (R.Array R.U R.DIM1 Int) -> IO (Int, Int, Int)
solveWithRepa stockPrices = do
let indices = R.fromListUnboxed (R.Z R.:. (8::Int)) [0..(S.size $ R.extent stockPrices)-1] :: R.Array R.U R.DIM1 Int
temp <- (R.computeP $ R.zipWith (\a b -> (a,b)) indices indices) :: IO(R.Array R.U R.DIM1 (Int, Int))
mapped <- (R.computeP $ R.zipWith (\(a,b) c -> (a,b,c)) temp indices) :: IO(R.Array R.U R.DIM1 (Int, Int, Int))
-- let temp = R.foldS foldF (0::Int, 0::Int, 0::Int) mapped
temp <- R.foldP foldF (0::Int, 0::Int, 0::Int) mapped
let (buyIdx, sellIdx, minIdx) = head $ R.toList temp
let buyAmount = stockPrices R.! (R.Z R.:. buyIdx)
let sellAmount = stockPrices R.! (R.Z R.:. sellIdx)
return (buyIdx, sellIdx, sellAmount - buyAmount)
where
foldF :: (Profit, Int) -> (Profit, Int) -> (Profit, Int)
foldF ((b1, s1), min) ((b2, s2), min2) = (maxProfit, )
where
amount idx = stockPrices R.! (R.Z R.:. idx)
minIdx = min (amount min) (amount min2)
-- YOU WERE HERE, DETERMINE HOW TO GET MAX PROFIT
maxProfit = max lProfit $ max mProfit rProfit
lProfit = (amount s1) - (amount b1)
rProfit = (amount s2) - (amount b2)
mProfit = max ((amount s2) - (amount b1)) ((amount s2) - (amount min))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment