Solving the Burst Balloon problem using Dynamic Programming
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
Continuing where I left off previously to solve the problem described here, I've now solved the same using dynamic programming (following Tikhon Jelvis blog on DP).
To refresh, the challenge is to find a sequence in which to burst a row of balloons that will earn the maximum number of coins. Each time balloon $i$ is burst, we earn $C_i-1 cdot C_i cdot C_i+1$ coins, then balloons $i-1$ and $i+1$ become adjacent to each other.
import qualified Data.Array as Array
burstDP :: [Int] -> Int
burstDP l = go 1 len
where
go left right | left <= right = maximum [ds Array.! (left, k-1)
+ ds Array.! (k+1, right)
+ b (left-1)*b k*b (right+1) | k <- [left..right]]
| otherwise = 0
len = length l
ds = Array.listArray bounds
[go m n | (m, n) <- Array.range bounds]
bounds = ((0,0), (len+1, len+1))
l' = Array.listArray (0, len-1) l
b i = if i == 0 || i == len+1 then 1 else l' Array.! (i-1)
I'm looking for:
- Correctness
- Program structure
- Idiomatic Haskell
- Any other higher order functions that can be used
- Other optimizations that can be done
algorithm programming-challenge haskell dynamic-programming
add a comment |Â
up vote
2
down vote
favorite
Continuing where I left off previously to solve the problem described here, I've now solved the same using dynamic programming (following Tikhon Jelvis blog on DP).
To refresh, the challenge is to find a sequence in which to burst a row of balloons that will earn the maximum number of coins. Each time balloon $i$ is burst, we earn $C_i-1 cdot C_i cdot C_i+1$ coins, then balloons $i-1$ and $i+1$ become adjacent to each other.
import qualified Data.Array as Array
burstDP :: [Int] -> Int
burstDP l = go 1 len
where
go left right | left <= right = maximum [ds Array.! (left, k-1)
+ ds Array.! (k+1, right)
+ b (left-1)*b k*b (right+1) | k <- [left..right]]
| otherwise = 0
len = length l
ds = Array.listArray bounds
[go m n | (m, n) <- Array.range bounds]
bounds = ((0,0), (len+1, len+1))
l' = Array.listArray (0, len-1) l
b i = if i == 0 || i == len+1 then 1 else l' Array.! (i-1)
I'm looking for:
- Correctness
- Program structure
- Idiomatic Haskell
- Any other higher order functions that can be used
- Other optimizations that can be done
algorithm programming-challenge haskell dynamic-programming
This code isn't complete. What'sArray
?
â Zeta
May 24 at 17:36
@Zeta Data.Array imported from the array package
â user3169543
May 25 at 16:55
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Continuing where I left off previously to solve the problem described here, I've now solved the same using dynamic programming (following Tikhon Jelvis blog on DP).
To refresh, the challenge is to find a sequence in which to burst a row of balloons that will earn the maximum number of coins. Each time balloon $i$ is burst, we earn $C_i-1 cdot C_i cdot C_i+1$ coins, then balloons $i-1$ and $i+1$ become adjacent to each other.
import qualified Data.Array as Array
burstDP :: [Int] -> Int
burstDP l = go 1 len
where
go left right | left <= right = maximum [ds Array.! (left, k-1)
+ ds Array.! (k+1, right)
+ b (left-1)*b k*b (right+1) | k <- [left..right]]
| otherwise = 0
len = length l
ds = Array.listArray bounds
[go m n | (m, n) <- Array.range bounds]
bounds = ((0,0), (len+1, len+1))
l' = Array.listArray (0, len-1) l
b i = if i == 0 || i == len+1 then 1 else l' Array.! (i-1)
I'm looking for:
- Correctness
- Program structure
- Idiomatic Haskell
- Any other higher order functions that can be used
- Other optimizations that can be done
algorithm programming-challenge haskell dynamic-programming
Continuing where I left off previously to solve the problem described here, I've now solved the same using dynamic programming (following Tikhon Jelvis blog on DP).
To refresh, the challenge is to find a sequence in which to burst a row of balloons that will earn the maximum number of coins. Each time balloon $i$ is burst, we earn $C_i-1 cdot C_i cdot C_i+1$ coins, then balloons $i-1$ and $i+1$ become adjacent to each other.
import qualified Data.Array as Array
burstDP :: [Int] -> Int
burstDP l = go 1 len
where
go left right | left <= right = maximum [ds Array.! (left, k-1)
+ ds Array.! (k+1, right)
+ b (left-1)*b k*b (right+1) | k <- [left..right]]
| otherwise = 0
len = length l
ds = Array.listArray bounds
[go m n | (m, n) <- Array.range bounds]
bounds = ((0,0), (len+1, len+1))
l' = Array.listArray (0, len-1) l
b i = if i == 0 || i == len+1 then 1 else l' Array.! (i-1)
I'm looking for:
- Correctness
- Program structure
- Idiomatic Haskell
- Any other higher order functions that can be used
- Other optimizations that can be done
algorithm programming-challenge haskell dynamic-programming
edited May 27 at 9:25
Vogel612â¦
20.9k345124
20.9k345124
asked May 23 at 22:23
user3169543
1604
1604
This code isn't complete. What'sArray
?
â Zeta
May 24 at 17:36
@Zeta Data.Array imported from the array package
â user3169543
May 25 at 16:55
add a comment |Â
This code isn't complete. What'sArray
?
â Zeta
May 24 at 17:36
@Zeta Data.Array imported from the array package
â user3169543
May 25 at 16:55
This code isn't complete. What's
Array
?â Zeta
May 24 at 17:36
This code isn't complete. What's
Array
?â Zeta
May 24 at 17:36
@Zeta Data.Array imported from the array package
â user3169543
May 25 at 16:55
@Zeta Data.Array imported from the array package
â user3169543
May 25 at 16:55
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
Your use of Array
for memoization can be extracted into array-memoize
.
If one can stop instead of having negative balloons decrease score, go
can be condensed into one case.
import Data.Function.ArrayMemoize (arrayMemoFix)
import Data.Array ((!), listArray)
burstDP :: [Int] -> Int
burstDP l = arrayMemoFix ((0,0), (len+1, len+1)) go (1, len) where
go ds (left, right) = maximum $ 0 :
[ds (left, k-1) + ds (k+1, right) + b (left-1)*b k*b (right+1) | k <- [left..right]]
b = (!) $ listArray (0, len+1) (1 : l ++ [1])
len = length l
If you don't care too much about performance, we can also memoize
directly on the balloon list:
burstDP :: [Int] -> Int
burstDP = memoFix3 go 1 1 where go ds l r b = maximum
[ ds left l x + ds right x r + l*x*r
| (left, x:right) <- zip (inits b) (tails b)
]
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Your use of Array
for memoization can be extracted into array-memoize
.
If one can stop instead of having negative balloons decrease score, go
can be condensed into one case.
import Data.Function.ArrayMemoize (arrayMemoFix)
import Data.Array ((!), listArray)
burstDP :: [Int] -> Int
burstDP l = arrayMemoFix ((0,0), (len+1, len+1)) go (1, len) where
go ds (left, right) = maximum $ 0 :
[ds (left, k-1) + ds (k+1, right) + b (left-1)*b k*b (right+1) | k <- [left..right]]
b = (!) $ listArray (0, len+1) (1 : l ++ [1])
len = length l
If you don't care too much about performance, we can also memoize
directly on the balloon list:
burstDP :: [Int] -> Int
burstDP = memoFix3 go 1 1 where go ds l r b = maximum
[ ds left l x + ds right x r + l*x*r
| (left, x:right) <- zip (inits b) (tails b)
]
add a comment |Â
up vote
0
down vote
Your use of Array
for memoization can be extracted into array-memoize
.
If one can stop instead of having negative balloons decrease score, go
can be condensed into one case.
import Data.Function.ArrayMemoize (arrayMemoFix)
import Data.Array ((!), listArray)
burstDP :: [Int] -> Int
burstDP l = arrayMemoFix ((0,0), (len+1, len+1)) go (1, len) where
go ds (left, right) = maximum $ 0 :
[ds (left, k-1) + ds (k+1, right) + b (left-1)*b k*b (right+1) | k <- [left..right]]
b = (!) $ listArray (0, len+1) (1 : l ++ [1])
len = length l
If you don't care too much about performance, we can also memoize
directly on the balloon list:
burstDP :: [Int] -> Int
burstDP = memoFix3 go 1 1 where go ds l r b = maximum
[ ds left l x + ds right x r + l*x*r
| (left, x:right) <- zip (inits b) (tails b)
]
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Your use of Array
for memoization can be extracted into array-memoize
.
If one can stop instead of having negative balloons decrease score, go
can be condensed into one case.
import Data.Function.ArrayMemoize (arrayMemoFix)
import Data.Array ((!), listArray)
burstDP :: [Int] -> Int
burstDP l = arrayMemoFix ((0,0), (len+1, len+1)) go (1, len) where
go ds (left, right) = maximum $ 0 :
[ds (left, k-1) + ds (k+1, right) + b (left-1)*b k*b (right+1) | k <- [left..right]]
b = (!) $ listArray (0, len+1) (1 : l ++ [1])
len = length l
If you don't care too much about performance, we can also memoize
directly on the balloon list:
burstDP :: [Int] -> Int
burstDP = memoFix3 go 1 1 where go ds l r b = maximum
[ ds left l x + ds right x r + l*x*r
| (left, x:right) <- zip (inits b) (tails b)
]
Your use of Array
for memoization can be extracted into array-memoize
.
If one can stop instead of having negative balloons decrease score, go
can be condensed into one case.
import Data.Function.ArrayMemoize (arrayMemoFix)
import Data.Array ((!), listArray)
burstDP :: [Int] -> Int
burstDP l = arrayMemoFix ((0,0), (len+1, len+1)) go (1, len) where
go ds (left, right) = maximum $ 0 :
[ds (left, k-1) + ds (k+1, right) + b (left-1)*b k*b (right+1) | k <- [left..right]]
b = (!) $ listArray (0, len+1) (1 : l ++ [1])
len = length l
If you don't care too much about performance, we can also memoize
directly on the balloon list:
burstDP :: [Int] -> Int
burstDP = memoFix3 go 1 1 where go ds l r b = maximum
[ ds left l x + ds right x r + l*x*r
| (left, x:right) <- zip (inits b) (tails b)
]
edited May 27 at 21:20
answered May 27 at 15:31
Gurkenglas
2,658411
2,658411
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195049%2fsolving-the-burst-balloon-problem-using-dynamic-programming%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
This code isn't complete. What's
Array
?â Zeta
May 24 at 17:36
@Zeta Data.Array imported from the array package
â user3169543
May 25 at 16:55