当前位置:网站首页>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 :
***
边栏推荐
- 几种常见的DoS攻击
- Wechat official account debugging and natapp environment building
- Istio practical skill: hide the automatically added server header
- 一文理解OpenStack网络
- 【应用推荐】最近大火的Apifox & Apipost 上手体验与选型建议
- 10 hands-free idea plug-ins. These codes do not need to be written (the second bullet)
- 安装ImageMagick7.1库以及php的Imagick扩展
- Flink kubernetes application deployment
- Why is the blackmail virus that shut down half of America's energy system terrible? Interpretation of authoritative reports
- Several common DoS attacks
猜你喜欢

还在担心漏测吗?快来使用jacoco统计下代码覆盖率

Recommend several super practical data analysis tools

Several common DoS attacks
![[my advanced OpenGL learning journey] learning notes of OpenGL coordinate system](/img/21/48802245fea2921fd5e4a9a2d9ad18.jpg)
[my advanced OpenGL learning journey] learning notes of OpenGL coordinate system

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

Understanding openstack network

Logging is not as simple as you think

存在安全隐患 部分冒险家混动版将召回

Still worried about missing measurements? Let's use Jacobo to calculate the code coverage

Most common usage of vim editor
随机推荐
Jenkins的便捷式安装
Linux record -4.22 MySQL 5.37 installation (supplementary)
高速公路服务区智能一体机解决方案
2021-04-24: handwriting Code: topology sorting.
在Gradle 中对Junit5 测试框架引用
为什么企业实施WMS仓储管理系统很容易失败
Precautions for using JMeter suite to build a pressure test environment
Step by step import RHEL image to Tencent cloud
Special topic of IM code scanning login Technology (III): easy to understand. A detailed principle of IM code scanning login function is enough
leetcode 139. Word Break 单词拆分(中等)
Two problems of qtreewidget returning as DLL in singleton mode
Installer la Bibliothèque imagemagick 7.1 et l'extension imagick de PHP
Solution to the problem that FreeRTOS does not execute new tasks
Mysql之Binlog
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
HMM to CRF understanding and learning notes
Vim编辑器的最常用的用法
Wechat official account debugging and natapp environment building
Network engineers must know the network essence knowledge!
New de debugging