题目

https://leetcode.com/problems/flip-string-to-monotone-increasing/

题解

方法1:前缀和

class Solution {public int minFlipsMonoIncr(String s) {int L = s.length();// i位置左边1的个数int[] leftOne = new int[L];for (int i = 0; i < L - 1; i++) {if (s.charAt(i) == '1') leftOne[i + 1] = leftOne[i] + 1;else leftOne[i + 1] = leftOne[i];}// i位置右边0的个数int[] rightZero = new int[L];for (int i = L - 1; i > 0; i--) {if (s.charAt(i) == '0') rightZero[i - 1] = rightZero[i] + 1;else rightZero[i - 1] = rightZero[i];}// 将i左边全变成1,i右边全变成0,总共需要改的个数int minSum = Integer.MAX_VALUE;for (int i = 0; i < L; i++) {minSum = Math.min(minSum, leftOne[i] + rightZero[i]);}return minSum;}
}

方法2:dp

int minFlipsMonoIncr(char *s) {int z = *s == '0' ? 0 : 1;int o = *s == '0' ? 1 : 0;for (s++; *s; s++){if (*s == '0'){o = (z < o ? z : o) + 1;}else{o = (z < o ? z : o);z++;}}return z < o ? z : o;
}

leetcode 926. Flip String to Monotone Increasing | 926. 将字符串翻转到单调递增(前缀和)相关推荐

  1. LeetCode 926. 将字符串翻转到单调递增(动态规划)

    文章目录 1. 题目 2. 解题 1. 题目 如果一个由 '0' 和 '1' 组成的字符串,是以一些 '0'(可能没有 '0')后面跟着一些 '1'(也可能没有 '1')的形式组成的,那么该字符串是单 ...

  2. Leetcode 738. Monotone Increasing Digits

    原题链接:https://leetcode.com/problems/monotone-increasing-digits/description/ 描述: Given a non-negative ...

  3. leetcode 1239. Maximum Length of a Concatenated String with Unique Characters | 1239. 串联字符串的最大长度(回溯)

    题目 https://leetcode.com/problems/contiguous-array/ 题解 回溯法,每个字符串都有可能加或者不加. class Solution {int N;publ ...

  4. LeetCode算法入门- String to Integer (atoi)-day7

    LeetCode算法入门- String to Integer (atoi)-day7 String to Integer (atoi): Implement atoi which converts ...

  5. 【To Understand !!! DP or 递归】LeetCode 87. Scramble String

    LeetCode 87. Scramble String 参考链接:[1]https://blog.csdn.net/makuiyu/article/details/44926439 [2]http: ...

  6. 【重点!DP】LeetCode 97. Interleaving String

    LeetCode 97. Interleaving String 参考网址:[1]https://www.youtube.com/watch?v=HmAF9xeS_2I [2]http://www.c ...

  7. leetcode 971. Flip Binary Tree To Match Preorder Traversal

    leetcode 971. Flip Binary Tree To Match Preorder Traversal 题意:给一颗二叉树,再给一个的数组,能否通过交换两个左右两个子节点,使得二叉树的前 ...

  8. [LeetCode]844. Backspace String Compare 解题报告(C++)

    [LeetCode]844. Backspace String Compare 解题报告(C++) 题目描述 Given two strings S and T, return if they are ...

  9. 738. Monotone Increasing Digits

    内容: Given a non-negative integer N, find the largest number that is less than or equal to N with mon ...

最新文章

  1. ROS2概述和实践入门
  2. VMware下redhat9.0的上网设置
  3. 神奇的bibtex插件:你的论文文献小助手
  4. python web自动化测试实验报告_Python:web自动化测试
  5. iframe-父子-兄弟页面相互传值(jq和js两种方法)
  6. android接支付宝授权和支付功能
  7. java keytool用法,java中Keytool的使用总结
  8. 双线性变换(Tustin transform/bilinear transformation)
  9. 基于Java毕业设计在线商城系统源码+系统+mysql+lw文档+部署软件
  10. python通过selenium模块模拟自动登录(以QQ空间为例)
  11. 两组数据的偏差率_析数据 察得失 明对策 促提高 —记东升一中2021级月考二成绩分析会...
  12. 塑料齿轮模具设计指南
  13. java 逃逸分析_JVM之逃逸分析
  14. php 模板对象,php面向对象--PHP模板 ppt
  15. unity使用Sprite Editor图片切割功能减少性能损耗
  16. 最新------如何不通过AppStore,直接安装IOS应用程序 兼容最新版IOS系统
  17. 【微信H5】分享出去是链接,不是卡片的原因及解决方案
  18. 【罗塞塔石碑】—My Lover(One.iso)
  19. 从一台 Windows 10 上共享文件夹到Docker中的Volume卷
  20. C语言——十进制转化为二进制

热门文章

  1. CodeForces - 1220D Alex and Julian(思维+数论)
  2. raid卡组不同raid_RAID有哪几种?有什么区别?
  3. Win32多线程编程(3) — 线程同步与通信
  4. 散谈游戏保护那点事~就从_TP开始入手吧
  5. 【白话科普】上网时遇到的 404 是什么意思?
  6. 高级数据结构与算法 | B树、B+树、B*树
  7. 经典面试|为何Kafka这么快?
  8. 你对JVM三色标记的理解嘛?
  9. 什么是真正的架构设计?
  10. 忠于职守 —— sysmon 线程到底做了什么?(九)