当前位置:网站首页>leetcode:926. Flip the string to monotonically increasing [prefix and + analog analysis]
leetcode:926. Flip the string to monotonically increasing [prefix and + analog analysis]
2022-06-11 18:46:00 【Review of the white speed Dragon King】

analysis
If all 0 Or all 1 No need to analyze
The initial value is set to all 1 The number of
Then we consider a certain 1 Front set to 0, This 1 All the following settings are 1
This requires flipping s[:i] in 1 The number of and s[i + 1:] in 0 The number of
Use prefix and to solve
ac code
class Solution:
def minFlipsMonoIncr(self, s: str) -> int:
if s.count('0') * s.count('1') == 0:
return 0
minn = s.count('1')
n = len(s)
lst1 = list(map(int, s))
preSum1 = list(accumulate(lst1, initial = 0))
lst0 = [1 if c == '0' else 0 for c in s]
preSum0 = list(accumulate(lst0, initial = 0))
#print(preSum0)
#print(preSum1)
for i in range(n):
if s[i] == '1':
#now = s[:i].count('1') + s[i + 1:].count('0')
now = (preSum1[i] - preSum1[0]) + (preSum0[-1] - preSum0[i + 1])
minn = min(minn, now)
return minn
summary
Simulate and apply prefixes and optimizations
Then note that the initial value is all 1 Reverse into 0 The number of times
边栏推荐
- 实现可以继续上局
- 全志科技T3开发板(4核ARM Cortex-A7)——视频开发案例
- 记录一下phpstudy配置php8.0和php8.1扩展redis
- TI AM64x——最新16nm处理平台,专为工业网关、工业机器人而生
- Quanzhi technology T3 development board (4-core arm cortex-a7) - mqtt communication protocol case
- leetcode:剑指 Offer 59 - II. 队列的最大值[deque + sortedlist]
- Deploy a go MSSQL API endpoint on SAP kyma
- User group actions
- Specific methods for JS to realize full screen display
- 软件开发的整体流程
猜你喜欢

力扣刷题——二叉树的最近公共祖先
![leetcode:剑指 Offer 59 - II. 队列的最大值[deque + sortedlist]](/img/6b/f2e04cd1f3aaa9fe057c292301894a.png)
leetcode:剑指 Offer 59 - II. 队列的最大值[deque + sortedlist]

Niu Ke swipes the question -- converting a string to an integer

cf:D. Black and White Stripe【连续k个中最少的个数 + 滑动窗口】

MBA, EMBA, MPa, MEM, pre interview (pre interview) time batch of national colleges and universities has been released (continuously updated) - Wendu Management Institute

2022-2023年西安交通大学管理学院MEM提前批面试网报通知

Surveillance des fonctions de perte avec visdom

视觉SLAM十四讲笔记-10-1

Visual slam lecture notes-10-1

TI AM64x——最新16nm处理平台,专为工业网关、工业机器人而生
随机推荐
Quanzhi technology T3 development board (4-core arm cortex-a7) - mqtt communication protocol case
Mysql深入完全学习---阶段1---学习总述
Force deduction 23 questions, merging K ascending linked lists
Add your favorite background music
视觉SLAM十四讲笔记-10-2
The nearest common ancestor of binary tree
用户组的操作
KMP! You deserve it!!! Run directly!
Feign shares login information for request
炫酷的可视化工具:processing 初识
基于TI AM5728 + Artix-7 FPGA开发板(DSP+ARM) 5G通信测试手册
Teach you how to learn the first set and follow set!!!! Hematemesis collection!! Nanny level explanation!!!
Visual slam lecture notes-10-1
开发中必备的文件的上传与下载
Force buckle 31 next arrangement
Force deduction 32 questions longest valid bracket
cf:B. Array Decrements【模拟】
在 SAP Kyma 上部署一个 Go MSSQL API Endpoint
Let our tanks move happily
全国院校MBA、EMBA、MPA、MEM、提前面试(预面试)时间批次已出(持续更新中)-文都管联院