当前位置:网站首页>Leetcode 926. Flip string to monotonically increasing [prefix and]
Leetcode 926. Flip string to monotonically increasing [prefix and]
2022-06-13 02:23:00 【JackDawn!?】
subject

Ideas
The property of the string we want to solve is , There is one located at s[i],s[i+1] Previous position , The characters to the left of this position are all 0, The characters on the right are all 1 . We can flip some values to construct a string that satisfies the above properties , So what we are looking for is such a position , Make it to the left 1 Plus the number on the right 0 Minimum number of .
Code
class Solution {
private:
int* pre;
public:
int minFlipsMonoIncr(string s) {
pre = new int[s.size()+1];
pre[0] = 0;
for(int i = 0; i < s.size(); ++i) {
pre[i+1] = pre[i]+s[i]-'0';
}
int _min = INT_MAX;
for(int i = -1; i < (int)s.size(); ++i) {
// from -1 It starts because this position may precede all characters
// because s.size() Returns an unsigned number , So you have to cast
int t = s.size()-(i+1);
t -= (pre[s.size()]-pre[i+1]);
_min = min(_min, pre[i+1]+t);
/* printf("%d %d\n", pre[i+1], t); */
}
return _min;
}
};
class Solution {
// Just found me sb 了 , There is no need to calculate the prefix and , The thought set is terrible
public:
int minFlipsMonoIncr(string s) {
int lo = 0, rz = 0;
for(int i = 0; i < s.size(); ++i) {
if(s[i]=='0') ++rz;
}
int _min = lo+rz;
for(int i = 0; i < s.size(); ++i) {
if(s[i]=='1') {
lo += 1;
} else {
rz -= 1;
}
_min = min(lo+rz, _min);
}
return _min;
}
};
Running results

边栏推荐
- [keras] generator for 3D u-net source code analysis py
- [work notes] xr872 codec driver migration and application program example (with chip debugging method)
- Cumulative tax law: calculate how much tax you have paid in a year
- Graph theory, tree based concept
- [single chip microcomputer] single timer in front and back platform program framework to realize multi delay tasks
- Stm32+ze-08 formaldehyde sensor tutorial
- uniapp 预览功能
- Fast Color Segementation
- Application and examples of C language structure
- Introduction to armv8/armv9 - learning this article is enough
猜你喜欢

Jump model between mirrors

Armv8-m (Cortex-M) TrustZone summary and introduction

Common web page status return code crawler

柏瑞凯电子冲刺科创板:拟募资3.6亿 汪斌华夫妇为大股东

Understanding and thinking about multi-core consistency

What are the differences in cache/tlb?

传感器:SHT30温湿度传感器检测环境温湿度实验(底部附代码)

Review the history of various versions of ITIL, and find the key points for the development of enterprise operation and maintenance
![Leetcode 473. 火柴拼正方形 [暴力+剪枝]](/img/3a/975b91dd785e341c561804175b6439.png)
Leetcode 473. 火柴拼正方形 [暴力+剪枝]
![How to solve the problem of obtaining the time through new date() and writing out the difference of 8 hours between the database and the current time [valid through personal test]](/img/c5/f17333cdb72a1ce09aa54e38dd0a8c.png)
How to solve the problem of obtaining the time through new date() and writing out the difference of 8 hours between the database and the current time [valid through personal test]
随机推荐
Microsoft Pinyin opens U / V input mode
Mbedtls migration experience
[pytorch] kaggle large image dataset data analysis + visualization
Anti crawling strategy (IP proxy, setting random sleep time, bilbili video information crawling, obtaining real URLs, processing special characters, processing timestamp, and multithreading)
Graph theory, tree based concept
STM32 timer interrupt learning notes
redis
[reading point paper] deeplobv3+ encoder decoder with Atlas separable revolution
Leetcode 93 recovery IP address
Huawei equipment is configured with IP and virtual private network hybrid FRR
Number of special palindromes in basic exercise of test questions
[learning notes] xr872 GUI littlevgl 8.0 migration (file system)
Sensor: sht30 temperature and humidity sensor testing ambient temperature and humidity experiment (code attached at the bottom)
[work notes] xr872 codec driver migration and application program example (with chip debugging method)
Think about the possibility of attacking secure memory through mmu/tlb/cache
[learning notes] xr872 audio driver framework analysis
ROS learning-7 error in custom message or service reference header file
0- blog notes guide directory (all)
An image is word 16x16 words: transformers for image recognition at scale
Review the history of various versions of ITIL, and find the key points for the development of enterprise operation and maintenance