--Returns last N elements in list
lastN :: Int -> [a] -> [a]
lastN n xs = let m = length xs in drop (m-n) xs
--create contiguous array starting from index b within list a
produceContiguous :: [a] -> Int -> [[a]]
produceContiguous [] _ = [[]]
produceContiguous arr ix = scanl (\acc x -> acC++ [x]) [arr !! ix] inset
where inset = lastN (length arr - (ix + 1)) arr
--Find maximum sum of all possible contiguous sub arrays, modulo [n]
--d is dummy data
let d = [1,2,3,10,6,3,1,47,10]
let maxResult = maximum $ map (\s -> maximum s) $ map (\c -> map (\ac -> (sum ac)`mod` (last n)) c) $ map (\n -> produceContiguous d n) [0..(length d) -1]
我是一个Haskell福利局 - 短短几天到它。如果我做的事情显然是错误的,whoopsies可以对此Haskell代码进行哪些优化?
如果代码正常工作,而您只是想要改进建议,则应该在codereview.stackexchange.com上发帖。否则,您需要明确指出您想解决的问题。 – chepner
这是最大的子阵列问题的一个奇怪的变化。我猜测模n'位是为了打破标准(高效)解决方案而设计的。我不确定是否有类似的有效解决方案,但我对此表示怀疑。一般来说,模块化算术和最大化并不能很好地啮合。 – dfeuer
在这类问题中,你应该总是包含一个规范,用简单的话来说明程序应该做什么。这将提供更多的可见性 - 我想大多数读者只会看到一段代码并停止阅读。代码中有一个非常简短的代码,在'd'上面的注释中,但是它在噪声中丢失了。 – chi