当前位置:网站首页>926. Flip String to Monotone Increasing
926. Flip String to Monotone Increasing
2022-06-11 23:57:00 【SUNNY_CHANGQI】
The description of the problem
A binary string is monotone increasing if it consists of some number of 0's (possibly none), followed by some number of 1's (also possibly none).
You are given a binary string s. You can flip s[i] changing it from 0 to 1 or from 1 to 0.
Return the minimum number of flips to make s monotone increasing.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/flip-string-to-monotone-increasing
an example
Input: s = "00110"
Output: 1
Explanation: We flip the last digit to get 00111.
The codes for above problem
#include <string>
#include <iostream>
using namespace std;
class Solution {
public:
int minFlipsMonoIncr(string &s){
int n = s.size();
int dp[n+1][2];
dp[0][0] = 0;
dp[0][1] = 0;
for (int i = 0; i < n; i++) {
dp[i+1][0] = dp[i][0] + ((s[i] == '1') ? 1 : 0);
dp[i+1][1] = min(dp[i][0], dp[i][1]) + ((s[i] == '0') ? 1 : 0);
}
return min(dp[n][0], dp[n][1]);
}
};
int main()
{
Solution s;
string s1 = "00110";
cout << "minFlipsMonoIncr: " << s.minFlipsMonoIncr(s1) << endl;
}
The corresponding results
$ ./test
minFlipsMonoIncr: 1
边栏推荐
- (linear DP) acwing 898 Number triangle
- What is webstorage? And cookies
- Mmdetection custom fetch detection result script and image_ demo. Py parsing
- 二叉排序树
- Stm32f103c8t6 related knowledge
- 挂载smb共享提示目录无权限
- Jetpack架构组件学习(3)——Activity Results API使用
- [naturallanguageprocessing] [multimodal] albef: visual language representation learning based on momentum distillation
- 图及图的遍历
- sonarqube介紹和安裝步驟
猜你喜欢

Lake Shore—SuperTran-VP 连续流低温恒温器系统

预解析与作用域
![[signals and systems] (XXI) Laplace transform and complex frequency domain analysis -- Laplace transform and its properties](/img/aa/821804e951e2fbb63c72f4e28756a1.jpg)
[signals and systems] (XXI) Laplace transform and complex frequency domain analysis -- Laplace transform and its properties

自定义JSP标签->概念->生命周期

m-way search trees

Flex flexible layout tutorial and understanding of the main axis cross axis: Grammar

Custom JSP tag - > concept - > lifecycle

Binary sort tree

mysql5和mysql8同时安装

DPT-FSNET: DUAL-PATH TRANSFORMER BASED FULL-BAND AND SUB-BAND FUSION NETWORK FOR SPEECH ENHANCEMENT
随机推荐
C collection of questions for project review
Mathematical modeling experience ----- summary of three modeling
Jenkins基本配置
[naturallanguageprocessing] [multimodal] albef: visual language representation learning based on momentum distillation
Antigen products enter the family, and Chinese medical device enterprises usher in a new blue ocean
On the knowledge points of cookie attributes and the differences between webstorage and cookies?
预解析与作用域
Live broadcast preview | featurestore meetup V3 is coming!
Mysql5 and mysql8 are installed at the same time
Solr之基礎講解入門
voc数据格式转为coco数据格式
Use select to switch coroutines
Anaconda download package error: valueerror: check_ hostname requires server_ hostname
ETF operation record: March 1, 2022
The road of global evolution of vivo global mall -- multilingual solution
Binary sort tree
(dp+ longest common subsequence) acwing 897 Longest common subsequence
Two ways of using reuqests in RF
Jetpack architecture component learning (3) -- activity results API usage
(linear DP) acwing 898 Number triangle