当前位置:网站首页>926. flip string to monotonic increment
926. flip string to monotonic increment
2022-06-11 22:27:00 【Biqiliang】
926. Flip the string to monotonic increment 【 Medium question 】【 A daily topic 】
Ideas :
【 Dynamic programming 】
Refer to official solution code , And write the solution ideas in the notes .
Code :
class Solution {
public int minFlipsMonoIncr(String s) {
int n = s.length();
//dp0 and dp1 Respectively represent the current subscript i The corresponding characters are flipped to 0 and 1 Under the circumstances , Make substring [0,i] Incremental minimum number of flips , The initial values are all 0
int dp0 = 0,dp1 = 0;
for (int i = 0; i < n; i++) {
//dp1 The meaning is , take i The position character is flipped to 1 when , Ensure substring [0,i] Incremental minimum number of flips , But will i The position is flipped to 1 There are two situations :i-1 The position is 0 perhaps by 1
// therefore , To ensure the minimum number of turns , take dp1 Updated to dp0 and dp1 The lesser of
dp1 = Math.min(dp0,dp1);
// Take out the current subscript i The character of position
char c = s.charAt(i);
// If the current actual character is 1, Then flip it to 0 after ,dp0++, Flip it to 1 after ,dp1 unchanged
if (c == '1'){
dp0++;
}else {
// If the current actual character is 0, Then flip it to 0,dp0 unchanged , Flip it to 1,dp1++
dp1++;
}
}
// At this time dp0 and dp1 It means The string s The last character is flipped to 0 Or flip to 1, The minimum cumulative number of flips to keep the whole substring increasing , Just return the smaller value between the two
return Math.min(dp0,dp1);
}
}
边栏推荐
- C language implements eight sorts of sort merge sort
- Determine whether the linked list is palindrome structure
- Tkinter学习笔记(四)
- Using the command line to call shell in unity
- Exercise 8-2 finding a specified element in an array (15 points)
- 69. square root of X
- STM32 development note 113:ads1258 drive design - reading temperature value
- Matplotlib和tkinter学习笔记(一)
- C language to achieve eight sorts (2)
- Leetcode stack topic summary
猜你喜欢

论文阅读《Dense Visual SLAM for RGB-D Cameras》

Maze problem in C language

論文閱讀《Dense Visual SLAM for RGB-D Cameras》
![[Yu Yue education] Yancheng Normal University Advanced Algebra reference](/img/3f/cd7f6f420fb1d453acca9aa73665ba.jpg)
[Yu Yue education] Yancheng Normal University Advanced Algebra reference

Lecture de l'article dense Visual SLAM for RGB - D Cameras

Basic operation and question type summary of binary tree

【解决】修改子物体Transform信息导致变换不对称、异常问题的解决方案

MySQL事务简介

Tkinter学习笔记(四)

C language to achieve eight sorts (2)
随机推荐
Matplotlib和tkinter学习笔记(一)
[uniapp native plug-in] shangmi cashbox plug-in
Determine whether the linked list is palindrome structure
启牛推荐开通的证券账户安全吗?靠谱吗
Leetcode stack topic summary
Exercise 9-1 time conversion (15 points)
如果重来一次高考,我要好好学数学!
图的基本操作(C语言)
论文阅读《Dense Visual SLAM for RGB-D Cameras》
Computer forced shutdown Oracle login failed
A simple example of linear regression in machine learning
什么是死锁?(把死锁给大家讲明白,知道是什么,为什么用,怎么用)
PHP+MYSQL图书管理系统(课设)
3.2 naming rules of test classes
Analysis of the implementation principle of an open source markdown to rich text editor
[Yu Yue education] calculus of Zhejiang University in autumn and winter 2021 (I) reference materials
Lecture de l'article dense Visual SLAM for RGB - D Cameras
判断链表是否为回文结构
Tkinter学习笔记(二)
Prefabricated dishes in the trillion market have also begun to roll inside. How can brands stand out in the fierce competition?