当前位置:网站首页>2021-12-25: given a string s consisting only of 0 and 1, assume that the subscript is from
2021-12-25: given a string s consisting only of 0 and 1, assume that the subscript is from
2022-06-23 21:14:00 【Fuda scaffold constructor's daily question】
2021-12-25: Given a given one, only 0 and 1 Composed string S, Suppose the subscript is from 1 Start , Regulations i Character value of position Vi The calculation method is as follows :
1 i == 1 when ,Vi = 1;
2 i > 1 when , If Si != Si-1,Vi = 1;
3 i > 1 when , If Si == Si-1,Vi = Vi-1 + 1.
You can delete it at will S The characters in , Back to the whole S Maximum value of ,
String length <=5000.
From Tencent .
answer 2021-12-25:
recursive . Try the model from left to right .
At present index The character of the position is reserved ; At present index The character of position is not reserved . The maximum of these two cases .
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
ret := max1("000001100000")
fmt.Println(ret)
}
func max1(s string) int {
if len(s) == 0 {
return 0
}
str := []byte(s)
arr := make([]int, len(str))
for i := 0; i < len(arr); i++ {
if str[i] == '0' {
} else {
arr[i] = 1
}
}
return process1(arr, 0, 0, 0)
}
// Recursive meaning :
// Currently in arr[index...] Make a choice on , str[index...] Left side , The latest figure is lastNum
// also lastNum Value brought , Has been pulled up to baseValue
// Back in the str[index...] Make a choice on , The ultimate maximum value
// index -> 0 ~ 4999
// lastNum -> 0 or 1
// baseValue -> 1 ~ 5000
// 5000 * 2 * 5000 -> 5 * 10^7( too !)
func process1(arr []int, index, lastNum, baseValue int) int {
if index == len(arr) {
return 0
}
curValue := 0
if lastNum == arr[index] {
curValue = baseValue + 1
} else {
curValue = 1
}
// At present index The character of the position is reserved
next1 := process1(arr, index+1, arr[index], curValue)
// At present index The character of position is not reserved
next2 := process1(arr, index+1, lastNum, baseValue)
return getMax(curValue+next1, next2)
}
func getMax(a, b int) int {
if a > b {
return a
} else {
return b
}
}The results are as follows :
边栏推荐
- . NET Framework . Net core and Net standard
- How does PMO select and train project managers?
- How to make a material identification sheet
- ASP. Net MVC and asp Net web form
- Talk about how to customize data desensitization
- [redis] intersection and union of ordered sets
- Excel text function
- JS to get the screen size, current web page and browser window
- QPS fails to go up due to frequency limitation of public network CLB bandwidth
- Bi-sql index
猜你喜欢

How PMO uses two dimensions for performance appraisal

Four aspects of PMO Department value assessment

New SQL syntax quick manual!

JS advanced programming version 4: generator learning

What are the main dimensions of PMO performance appraisal?

Applet development framework recommendation

Steps for formulating the project PMO strategic plan
Application of JDBC in performance test

I am 30 years old, no longer young, and have nothing

3000 frame animation illustrating why MySQL needs binlog, redo log and undo log
随机推荐
Sharelist supports simultaneous mounting of Google drive/onedrive multiple network disks
ASP. Net MVC and asp Net web form
Four aspects of PMO Department value assessment
Overview of digital circuits
This article introduces you to the necessity of database connection pooling
How to log in to the server through the fortress machine to transfer files? What are the specific steps?
游戏安全丨喊话CALL分析-写代码
JS namespace
How to make a commodity price tag
Advantages of short video automatic audit? What are the difficulties of manual audit?
【Debian】Debian使用笔记
CPS 22 January additional incentive rules
How to dispose of the words on the picture? How do I add text to a picture?
Applet development framework recommendation
How to Net project migration to NET Core
Thinking about distributed system consensus
How to build a personal cloud game server? How many games can the cloud game platform install?
How to gradually improve PMO's own ability and management level
Why is it invalid to assign values to offsetwidth and offsetHeight
Configure two databases in master-slave database mode (master and slave)