当前位置:网站首页>1529. Minimum number of suffix flips
1529. Minimum number of suffix flips
2022-07-06 16:08:00 【mrbone9】
Address :
Power button
https://leetcode-cn.com/problems/minimum-suffix-flips/
subject :
Give you a length of n 、 Subscript from 0 Starting binary string target . You have another length of n Binary string of s , At first, everyone was 0 . You want to make s and target equal .
In one step , You can choose the subscript i(0 <= i < n) And flip on Closed interval [i, n - 1] All bits in . Flipping means '0' Turn into '1' , and '1' Turn into '0' .
Return to make s And target Equal the minimum number of flips required .
Example 1:
| Input :target = "10111" Output :3 explain : first ,s = "00000" . Select subscript i = 2: "00000" -> "00111" Select subscript i = 0: "00111" -> "11000" Select subscript i = 1: "11000" -> "10111" To achieve the goal , You need at least 3 Time flip . |
Example 2:
| Input :target = "101" Output :3 explain : first ,s = "000" . Select subscript i = 0: "000" -> "111" Select subscript i = 1: "111" -> "100" Select subscript i = 2: "100" -> "101" To achieve the goal , You need at least 3 Time flip . |
Example 3:
| Input :target = "00000" Output :0 explain : because s Already equal to the goal , So no operation is required |
Tips :
| n == target.length 1 <= n <= 105 target[i] by '0' or '1' |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/minimum-suffix-flips
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
First of all, flip the figure under the hand animation , Finding the law
such as :10111
00000
00111
01000
10111
If you can't see clearly for the time being , On the contrary, you can see clearly , Change the topic to how to change a string into 0 The number of times
Then from left to right , We saw 1, You have to do a flip , Then next , It happens to be in reverse order
But how to implement the code ? A little bit of a problem
Then watch , If the intermediate element is adjacent and of the same type , Such as fellow 1, Or both 0
When we flip, we actually flip together , Then we can regard these same types as only one
such as :1010001, Let's reverse the order
| 1010001 | 0101110 |
| 0101110 | 0010001 |
| 0010001 | 0001110 |
| 0001110 | 0000001 |
| 0000001 | 0000000 |
After optimization, it is :10101 Flip of
The exact number of flips is Of this string 1 Starting length ,
If it is 0 Opening remarks :00110, In fact, it is seen as 10, The length is 2
If you don't believe it, you can try
When such a rule is found, it is much easier to implement the code
Method 1 、 Count the number of strings
int minFlips(char * target){
char *s = target;
int cnt = 0;
int i =0;
char prev = '0';
while(s[i])
{
if(s[i] != prev)
{
cnt++;
prev = s[i];
}
i++;
}
return cnt;
}边栏推荐
- Hdu-6025-prime sequence (girls' competition)
- 【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
- Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
- Opencv learning log 30 -- histogram equalization
- 信息安全-安全专业名称|CVE|RCE|POC|VUL|0DAY
- X-Forwarded-For详解、如何获取到客户端IP
- 想应聘程序员,您的简历就该这样写【精华总结】
- 渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
- C language must memorize code Encyclopedia
- MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
猜你喜欢

Penetration test (4) -- detailed explanation of meterpreter command

Penetration test (3) -- Metasploit framework (MSF)
![MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’](/img/e6/f4a696179282fe1f4193410c5a493a.png)
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’

【高老师UML软件建模基础】20级云班课习题答案合集

X-forwarded-for details, how to get the client IP

Penetration test (1) -- necessary tools, navigation

渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具

Vs2019 initial use

STM32 learning record: LED light flashes (register version)

渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
随机推荐
Opencv learning log 24 -- Hough transform 2 (maximum interval and minimum length can be limited)
双向链表—全部操作
628. Maximum product of three numbers
Programmers, what are your skills in code writing?
HDU - 6024 Building Shops(女生赛)
对iptables进行常规操作
If you want to apply for a programmer, your resume should be written like this [essence summary]
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
【练习-2】(Uva 712) S-Trees (S树)
Information security - Analysis of security orchestration automation and response (soar) technology
【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
Alice and Bob (2021牛客暑期多校训练营1)
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
[exercise -11] 4 values why sum is 0 (and 4 values of 0)
Shell Scripting
信息安全-威胁检测引擎-常见规则引擎底座性能比较
Penetration test (1) -- necessary tools, navigation
Raspberry pie csi/usb camera uses mjpg to realize web camera monitoring
【练习-7】Crossword Answers
Basic Q & A of introductory C language