Skip to content

Commit bb62734

Browse files
committedJan 18, 2023
Add solution #926
1 parent 9e24209 commit bb62734

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@
208208
916|[Word Subsets](./0916-word-subsets.js)|Medium|
209209
922|[Sort Array By Parity II](./0922-sort-array-by-parity-ii.js)|Easy|
210210
925|[Long Pressed Name](./0925-long-pressed-name.js)|Easy|
211+
926|[Flip String to Monotone Increasing](./0926-flip-string-to-monotone-increasing.js)|Medium|
211212
929|[Unique Email Addresses](./0929-unique-email-addresses.js)|Easy|
212213
966|[Vowel Spellchecker](./0966-vowel-spellchecker.js)|Medium|
213214
970|[Powerful Integers](./0970-powerful-integers.js)|Easy|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 926. Flip String to Monotone Increasing
3+
* https://leetcode.com/problems/flip-string-to-monotone-increasing/
4+
* Difficulty: Medium
5+
*
6+
* A binary string is monotone increasing if it consists of some number of 0's
7+
* (possibly none), followed by some number of 1's (also possibly none).
8+
*
9+
* You are given a binary string s. You can flip s[i] changing it from 0 to 1
10+
* or from 1 to 0.
11+
*
12+
* Return the minimum number of flips to make s monotone increasing.
13+
*/
14+
15+
/**
16+
* @param {string} s
17+
* @return {number}
18+
*/
19+
var minFlipsMonoIncr = function(s) {
20+
let result = 0;
21+
let count = 0;
22+
23+
for (const str of s) {
24+
if (str == '1') {
25+
count++;
26+
} else if (str =='0' && count > 0) {
27+
result++;
28+
count--;
29+
}
30+
}
31+
32+
return result;
33+
};

0 commit comments

Comments
 (0)
Please sign in to comment.