当前位置:网站首页>2021-04-29: given an array arr, it represents a row of balloons with scores. One for each blow
2021-04-29: given an array arr, it represents a row of balloons with scores. One for each blow
2022-06-24 15:55:00 【Fuda scaffold constructor's daily question】
2021-04-29: Given an array arr, Represents a row of balloons with scores . Every time you blow up a balloon, you get a score , Suppose you blow up the gas The ball The score of is X, The rules for obtaining scores are as follows : 1) If there is an unexploded balloon on the left of the exploded balloon , Find the balloon closest to the exploded balloon , Suppose the score is L; If there is an unexploded balloon on the right of the exploded balloon , Find the balloon closest to the exploded balloon , Suppose the score is R. Get a score of L_X_R. 2) If there is an unexploded balloon on the left of the exploded balloon , Find the balloon closest to the exploded balloon , Suppose the score is L; If all the balloons on the right of the exploded balloon have been exploded . Get a score of L_X. 3) If all the balloons on the left of the exploded balloon have been exploded ; If there is one on the right side of the balloon that has been blasted balloon , Find the balloon closest to the exploded balloon , Suppose the score is R; If all the balloons to the right of the exploded balloon are already Be blasted . Get a score of X_R. 4) If all the balloons on the left and right of the exploded balloon have been exploded . Get a score of X. The goal is to blow up all the balloons , Get the score of each explosion . By choosing the order in which the balloons are blasted , You can get different total scores , Please return the maximum score you can get .【 give an example 】arr = {3,2,5} If you blow it up first 3, get 3_2; Blast again 2, get 2_5; Finally explode 5, get 5; The final total score 21 If you blow it up first 3, get 3_2; Blast again 5, get 2_5; Finally explode 2, get 2; The final total score 18 If you blow it up first 2, get 3_2_5; Blast again 3, get 3_5; Finally explode 5, get 5; The final total score 50 If you blow it up first 2, get 3_2_5; Blast again 5, get 3_5; Finally explode 3, get 3; The final total score 48 If you blow it up first 5, get 2_5; Blast again 3, get 3_2; Finally explode 2, get 2; The final total score 18 If you blow it up first 5, get 2_5; Blast again 2, get 3_2; Finally explode 3, get 3; The final total score 19 The maximum score you can get is 50.
Fuda answer 2021-04-29:
Dynamic programming .
The code to use golang To write . The code is as follows :
package main
import (
"fmt"
)
func main() {
arr := []int{2, 2, 2}
ret := maxCoins1(arr)
fmt.Println(ret)
ret = maxCoins2(arr)
fmt.Println(ret)
}
func maxCoins1(arr []int) int {
if len(arr) == 0 {
return 0
}
if len(arr) == 1 {
return arr[0]
}
N := len(arr)
help := make([]int, N+2)
help[0] = 1
help[N+1] = 1
for i := 0; i < N; i++ {
help[i+1] = arr[i]
}
return process(help, 1, N)
}
// Explode arr[L..R] All balloons in range , Returns the maximum score
// hypothesis arr[L-1] and arr[R+1] It must not have been blown up
func process(arr []int, L int, R int) int {
if L == R { // If arr[L..R] There is only one balloon in the range , Just blow it up
return arr[L-1] * arr[L] * arr[R+1]
}
// Finally explode arr[L] The plan , And finally explode arr[R] The plan , Let's compare
max := getMax(arr[L-1]*arr[L]*arr[R+1]+process(arr, L+1, R),
arr[L-1]*arr[R]*arr[R+1]+process(arr, L, R-1))
// Try every scheme in which the balloon in the middle is finally blown up
for i := L + 1; i < R; i++ {
max = getMax(max, arr[L-1]*arr[i]*arr[R+1]+process(arr, L, i-1)+process(arr, i+1, R))
}
return max
}
func maxCoins2(arr []int) int {
if len(arr) == 0 {
return 0
}
if len(arr) == 1 {
return arr[0]
}
N := len(arr)
help := make([]int, N+2)
help[0] = 1
help[N+1] = 1
for i := 0; i < N; i++ {
help[i+1] = arr[i]
}
dp := make([][]int, N+2)
for i := 0; i < N+2; i++ {
dp[i] = make([]int, N+2)
}
for i := 1; i <= N; i++ {
dp[i][i] = help[i-1] * help[i] * help[i+1]
}
for L := N; L >= 1; L-- {
for R := L + 1; R <= N; R++ {
ans := help[L-1]*help[L]*help[R+1] + dp[L+1][R]
ans = getMax(ans, help[L-1]*help[R]*help[R+1]+dp[L][R-1])
for i := L + 1; i < R; i++ {
ans = getMax(ans, help[L-1]*help[i]*help[R+1]+dp[L][i-1]+dp[i+1][R])
}
dp[L][R] = ans
}
}
return dp[1][N]
}
func getMax(a int, b int) int {
if a > b {
return a
} else {
return b
}
}The results are as follows :
***
边栏推荐
- 推荐几款超级实用的数据分析利器
- Mysql之Binlog
- Industry cases of successful digital transformation
- 如何轻松实现在线K歌房,与王心凌合唱《山海》
- 手机同花顺股票开户安全吗!
- Instruction document for online written examination assistance of smart side school recruitment
- 我与“Apifox”的网络情缘
- Install the imagemagick7.1 library and the imageick extension for PHP
- Cloud + community [play with Tencent cloud] essay solicitation activity winners announced
- Jenkins的便捷式安装
猜你喜欢

The penetration of 5g users of operators is far slower than that of 4G. The popularity of 5g still depends on China Radio and television

Wi-Fi 7 来啦,它到底有多强?

MySQL binlog

60 个神级 VS Code 插件!!
![clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]](/img/f0/42f394dbc989d381387c7b953d2a39.jpg)
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]

Using oasis to develop a hop by hop (I) -- Scene Building

60 divine vs Code plug-ins!!

MongoDB入門實戰教程:學習總結目錄

运营商5G用户渗透远远比4G慢,5G的普及还得看中国广电

SIGGRAPH 2022 | 真实还原手部肌肉,数字人双手这次有了骨骼、肌肉、皮肤
随机推荐
PHP application container deployment practice
Nifi from introduction to practice (nanny level tutorial) - environment
60 个神级 VS Code 插件!!
运营商5G用户渗透远远比4G慢,5G的普及还得看中国广电
Special topic of IM code scanning login Technology (III): easy to understand. A detailed principle of IM code scanning login function is enough
60 divine vs Code plug-ins!!
我与“Apifox”的网络情缘
Nature刊登量子计算重大进展:有史以来第一个量子集成电路实现
国产最长寿的热销手机,苹果也不是对手,总算让国产手机找回面子
2021-05-04: given a non negative integer C, you need to judge whether there are two integers a and B, so that a*a+b*b=c.
April 23, 2021: there are n cities in the TSP problem, and there is a distance between any two cities
Very exciting! 12000 words summarized the theory of network technology, reviewing the old and learning the new
Industry cases of successful digital transformation
Step by step import RHEL image to Tencent cloud
Easynvr has been connected to the third-party supervision platform. How to achieve local Internet access
asciinema 搭配 asciicast2gif 实现高效的命令行终端录制能力
Is it safe for futures companies to open accounts
"Industry foresight" future development trend of intelligent security monitoring industry
Most common usage of vim editor
Logging is not as simple as you think