当前位置:网站首页>April 26, 2021: the length of the integer array arr is n (3 < = n < = 10^4), and each number is
April 26, 2021: the length of the integer array arr is n (3 < = n < = 10^4), and each number is
2022-06-24 15:55:00 【Fuda scaffold constructor's daily question】
2021-04-26: integer array arr The length is n(3 <= n <= 10^4), Initially, each number was <=200 Is positive and satisfies the following conditions : 1. arr0 <= arr1.2.arrn-1 <= arrn-2.3. arri <= max(arri-1, arri+1). But in arr Some numbers are missing , such as k The number of positions is preceded by a positive number , After losing k The number of positions is 0. Please... According to the above conditions , Calculate how many different arr Can meet the above conditions . such as 6,0,9 Only restore to 6,9,9 All three conditions are met , So back 1 Kind of .
Fuda answer 2021-04-26:
This problem is difficult . The answer is dynamic programming . It's too late , So it's simple .
The code to use golang To write . The code is as follows :
package main import ( "fmt" ) func main() { arr := []int{6, 0, 9} ret := ways3(arr) fmt.Println(ret) } func ways3(arr []int) int { N := len(arr) dp := make([][][]int, N) for i := 0; i < N; i++ { dp[i] = make([][]int, 201) for j := 0; j < 201; j++ { dp[i][j] = make([]int, 3) } } if arr[0] != 0 { dp[0][arr[0]][0] = 1 dp[0][arr[0]][1] = 1 } else { for v := 1; v < 201; v++ { dp[0][v][0] = 1 dp[0][v][1] = 1 } } presum := make([][]int, 201) for i := 0; i < 201; i++ { presum[i] = make([]int, 3) } for v := 1; v < 201; v++ { for s := 0; s < 3; s++ { presum[v][s] = presum[v-1][s] + dp[0][v][s] } } for i := 1; i < N; i++ { for v := 1; v < 201; v++ { for s := 0; s < 3; s++ { if arr[i] == 0 || v == arr[i] { if s == 0 || s == 1 { dp[i][v][s] += sum(1, v-1, 0, presum) } dp[i][v][s] += dp[i-1][v][1] dp[i][v][s] += sum(v+1, 200, 2, presum) } } } for v := 1; v < 201; v++ { for s := 0; s < 3; s++ { presum[v][s] = presum[v-1][s] + dp[i][v][s] } } } if arr[N-1] != 0 { return dp[N-1][arr[N-1]][2] } else { return sum(1, 200, 2, presum) } } func sum(begin int, end int, relation int, presum [][]int) int { return presum[end][relation] - presum[begin-1][relation] }
The results are as follows :
***
边栏推荐
- 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.
- 个人常用的高效工具
- [log service CLS] Tencent cloud log4j/logback log collection best practices
- Three solutions for Jenkins image failing to update plug-in Center
- Still worried about missing measurements? Let's use Jacobo to calculate the code coverage
- leetcode 139. Word Break 單詞拆分(中等)
- 为什么企业实施WMS仓储管理系统很容易失败
- VNC Viewer方式的远程连接树莓派
- How to easily realize online karaoke room and sing "mountain sea" with Wang Xinling
- 2021-04-25: given an array arr and a positive number m, the
猜你喜欢
Logging is not as simple as you think
Software test [high frequency] interview questions sorted out by staying up late (latest in 2022)
Here comes Wi Fi 7. How strong is it?
Nifi from introduction to practice (nanny level tutorial) - environment
运营商5G用户渗透远远比4G慢,5G的普及还得看中国广电
Implement Domain Driven Design - use ABP framework - domain logic & application logic
为什么企业实施WMS仓储管理系统很容易失败
一文理解OpenStack网络
nifi从入门到实战(保姆级教程)——环境篇
国产芯片的赶超,让美国手机芯片龙头高通害怕了,出招应对竞争
随机推荐
Mysql之Binlog
Design of CAN bus controller based on FPGA (Part 2)
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
MySQL development specification
Step by step import RHEL image to Tencent cloud
Flink Kubernetes Application部署
MySQL binlog
几种常见的DoS攻击
great! The novel website project is completely open source
Why is it easy for enterprises to fail in implementing WMS warehouse management system
推荐几款超级实用的数据分析利器
Detailed explanation of estab of Stata regression table output
【应用推荐】最近大火的Apifox & Apipost 上手体验与选型建议
2021-04-18: given a two-dimensional array matrix, the value in it is either 1 or 0,
Linux record -4.22 MySQL 5.37 installation (supplementary)
Fine! Huawei firewall dual computer hot standby Technology: HRP, vgmp, VRRP
Arrays API
国产芯片的赶超,让美国手机芯片龙头高通害怕了,出招应对竞争
一文理解OpenStack网络
Wechat official account debugging and natapp environment building