当前位置:网站首页>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 :
***
边栏推荐
- 打破内存墙的新利器成行业“热搜”!持久内存让打工人也能玩转海量数据+高维模型
- Why is the blackmail virus that shut down half of America's energy system terrible? Interpretation of authoritative reports
- Tencent cloud native intelligent data Lake Conference will be held, revealing the panoramic matrix of Tencent cloud data Lake products for the first time
- 【C语言刷题——Leetcode12道题】带你起飞,飞进垃圾堆
- 2021-04-22: given many line segments, each line segment has two numbers [start, end],
- 【附下载】汉化版Awvs安装与简单使用
- 国产芯片的赶超,让美国手机芯片龙头高通害怕了,出招应对竞争
- 60 个神级 VS Code 插件!!
- 期货公司开户安全吗
- Instruction document for online written examination assistance of smart side school recruitment
猜你喜欢

【C语言刷题——Leetcode12道题】带你起飞,飞进垃圾堆

Mysql之Binlog

如何轻松实现在线K歌房,与王心凌合唱《山海》

Using alicloud RDS for SQL Server Performance insight to optimize database load - first understanding of performance insight

日志记录真没你想的那么简单
![Software test [high frequency] interview questions sorted out by staying up late (latest in 2022)](/img/33/2c2256fd98b908ddaf5573f644ad7f.png)
Software test [high frequency] interview questions sorted out by staying up late (latest in 2022)

The catch-up of domestic chips has scared Qualcomm, the leader of mobile phone chips in the United States, and made moves to cope with the competition

Implement Domain Driven Design - use ABP framework - domain logic & application logic

【面试高频题】难度 3/5,可直接构造的序列 DP 题

Mongodb Getting started Practical Tutoriel: Learning Summary Table des matières
随机推荐
【C语言刷题——Leetcode12道题】带你起飞,飞进垃圾堆
一文详解JackSon配置信息
2021-04-25: given an array arr and a positive number m, the
实现领域驱动设计 - 使用ABP框架 - 领域逻辑 & 应用逻辑
手机同花顺股票开户安全吗!
Teach you how to view version information with mongodb
The catch-up of domestic chips has scared Qualcomm, the leader of mobile phone chips in the United States, and made moves to cope with the competition
Paper: Google TPU
Jenkins 镜像无法更新插件中心的3种解决方法
使用阿里云RDS for SQL Server性能洞察优化数据库负载-初识性能洞察
The cold winter can't stop the determination to enter the big factory. The Android interview has a complete knowledge structure, and everything you need to master in the interview is here!
Logging is not as simple as you think
Cloud + community [play with Tencent cloud] essay solicitation activity winners announced
Crmeb multi merchant system applet authorization problem solving paste
The 30 pictures bring the network protocol layer by layer to life. It's really fragrant!
如何扩展aws主机上的磁盘空间
Vim编辑器的最常用的用法
熬夜整理出的软件测试【高频】面试题大全(2022最新)
The decline of China's product managers: starting from the nostalgia for jobs
【Prometheus】6. Prometheus and kubernetes (incomplete)