当前位置:网站首页>Li Kou today's question 926 Flip string to monotonic increment
Li Kou today's question 926 Flip string to monotonic increment
2022-06-12 17:05:00 【Struggling young man】
926. Flip the string to monotonic increment
Ideas :
Dynamic programming , State transition equation : When there is a 0 When , It can be done to 0 Conduct +1 Operate or make all of the preceding 1 become 0, State transition equation ret=min(ret + 1, num_1);
class Solution {
public int minFlipsMonoIncr(String s) {
char[] cs = s.toCharArray();
// First convert the string of words into an array ,ret Is used to record the minimum operand ,num_1 It's for recording 1 The number of
int n = cs.length, ret = 0 , num_1 = 0, num_0 = 0;
// for(int i = 0;i <n; i++){
// if(cs[i] == '0'){
// // encounter 0 There are two operations , One is to 0 Additive transformation 1, Operands +1. Or put this 0 Ahead 1 become 0, The operand depends on 1 The number of , That is to say num_1.
// ret = Math.min(ret+1,num_1);
// } else{
// // As long as you don't meet 0 That's it 1, therefore num_1 want ++ ,
// num_1++;
// }
// }
// The same way of thinking , This time it is traversal from back to front , encounter 1 There are two operations , One is to 1 become 0 Operands +1, The other is to put this 1 After that 0 All become one
for(int i = n; i > 0 ; i--){
if(cs[i-1] == '1'){
ret = Math.min(ret + 1, num_0);
}else{
num_0++;
}
}
return ret;
}
}
2022/6/11
边栏推荐
- 卖疯了的临期产品:超低价、大混战与新希望
- redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required
- R语言使用plot函数可视化数据散点图,使用font.axis参数指定坐标轴刻度标签的字体类型为斜体字体(italic)
- 软件工程 学生信息管理系统 结构化的需求分析
- Su directly switches to super administrator mode, so that many error reports can be avoided
- Idea how to set the guide package without * sign
- C # final review programming question (guessed by the teacher)
- \begin{algorithm} 笔记
- The R language uses the pyramid function of epidisplay package to visualize the pyramid graph and the pyramid graph based on the existing summary data (table data)
- Golang recursively encrypts and decrypts all files under the specified folder
猜你喜欢

Atlas conflict Remote Code Execution Vulnerability (cve-2022-26134) vulnerability recurrence

Atlassian Confluence 远程代码执行漏洞(CVE-2022-26134)漏洞复现

redis. clients. jedis. exceptions. JedisConnectionException: Could not get a resource from the pool

5、Embedding

邱盛昌:OPPO商业化数据体系建设实战

Some minor problems and solutions encountered when using ubantu

Extract the new Chinese cross modal benchmark zero from 5billion pictures and texts, and Qihoo 360's new pre training framework surpasses many SOTAS

从50亿图文中提取中文跨模态新基准Zero,奇虎360全新预训练框架超越多项SOTA

Cloud development kunkun chicken music box wechat applet source code

idea如何设置导包不带*号
随机推荐
Cicada mother talks to rainbow couple: 1.3 billion goods a year, from e-commerce beginners to super goods anchor
添加静态路由
R语言使用pdf函数将可视化图像结果保存到pdf文件中、使用pdf函数打开图像设备、使用dev.off函数关闭图像设备、自定义width参数和height参数指定图像的宽度和高度
redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required
Recommend AI intelligent drawing repair software
Doctor application | National University of Singapore, Xinchao Wang, teacher recruitment, doctor / postdoctoral candidate in the direction of graph neural network
Loading shellcode in C and go languages
selenium元素定位
Possible problems of long jump in gaussdb
pytorch和torchvision官方文档使用方法
What is compound interest financial product?
反馈式编译
R language calculates data Table specifies the mean value of a numeric variable when the value of one grouped variable is fixed and another grouped variable
Golang recursively encrypts and decrypts all files under the specified folder
Swin transformer code explanation
Male god goddess voting source code v5.5.21 voting source code
\begin{algorithm} 笔记
Différence entre le mode grand et le mode petit
idea如何设置导包不带*号
goland变成中文版了怎么修改回英文版