当前位置:网站首页>[926. flip the string to monotonic increment]
[926. flip the string to monotonic increment]
2022-06-12 10:04:00 【Sugar_ wolf】
source : Power button (LeetCode)
describe :
If a binary string , With some 0( There may be no 0) Followed by some 1( Or maybe not 1) In the form of , Then the string is Monotone increasing Of .
Give you a binary string s, You can put any 0 Flip to 1 Or will 1 Flip to 0 .
Return to make s Monotonically increasing minimum number of flips .
Example 1:
Input :s = "00110"
Output :1
explain : Flip the last one to get 00111.
Example 2:
Input :s = "010110"
Output :2
explain : Flip to get 011111, Or is it 000111.
Example 3:
Input :s = "00011000"
Output :2
explain : Flip to get 00000000.
Tips :
- 1 <= s.length <= 105
- s[i] by ‘0’ or ‘1’
Method : Dynamic programming


Code :
class Solution {
public:
int minFlipsMonoIncr(string &s) {
int dp0 = 0, dp1 = 0;
for (char c: s) {
int dp0New = dp0, dp1New = min(dp0, dp1);
if (c == '1') {
dp0New++;
} else {
dp1New++;
}
dp0 = dp0New;
dp1 = dp1New;
}
return min(dp0, dp1);
}
};
Execution time :16 ms, In all C++ Defeated in submission 96.07% Users of
Memory consumption :10.8 MB, In all C++ Defeated in submission 73.81% Users of
Complexity analysis
Time complexity : O(n), among n Is string s The length of . You need to traverse the string once , The time to calculate the minimum number of flips for each character is O(1).
Spatial complexity : O(1). Using spatial optimization methods , The space complexity is O(1).
author:LeetCode-Solution
边栏推荐
- Pandorabox uses firewall rules to define non internet time
- 《保护我们的数字遗产:DNA数据存储》白皮书发布
- 005: difference between data lake and data warehouse
- 7-5 哲哲打游戏
- 日本经济泡沫与房价泡沫
- 5种最常见的CEPH失败方案
- Auto. JS debugging: use the network mode of lightning simulator for debugging
- Auto. JS learning notes 8: some common and important APIs
- one
- 【云原生 | Kubernetes篇】Kubernetes 网络策略(NetworkPolicy)
猜你喜欢
![[cloud native | kubernetes] kubernetes networkpolicy](/img/8b/9260fc39d3f595cdc2689a3ab26bd7.png)
[cloud native | kubernetes] kubernetes networkpolicy

005: difference between data lake and data warehouse

SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000

7-13 地下迷宫探索(邻接表)

FPGA基于DE2-115平台的VGA显示

MySQL optimized slow log query

行业分析怎么做

CentOS 7 installing MySQL 8

Auto. JS debugging: use the network mode of lightning simulator for debugging

SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000
随机推荐
FPGA VGA display based on de2-115 platform
链式哈希表
Enumerate all USB device codes
7-5 zhe zhe playing games
Explanation of the principle of MySQL's leftmost matching principle
Dazzle the "library" action - award solicitation from the golden warehouse of the National People's Congress - high availability failover and recovery of kingbasees cluster
How to implement Web3.0 and digital fashion?
UEFI edkii programming learning
Transport layer protocol -- TCP protocol
The Dragon Boat Festival is in good health -- people are becoming more and more important in my heart
June training (day 12) - linked list
UEFI EDKII 编程学习
2021-02-22
2021-02-21
软件定义存储概览(一篇就够)
HALCON联合C#检测表面缺陷——仿射变换(三)
SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000
002: what are the characteristics of the data lake
[preview of the open class of Jishu] arm's strongest MCU core cortex-m85 processor helps the innovation of the Internet of things in an all-round way (there is a lottery)
[untitled]