Skip to content

Commit a009b6f

Browse files
committed
leetcode
1 parent da69a17 commit a009b6f

File tree

4 files changed

+321
-0
lines changed

4 files changed

+321
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
3+
4+
-* 926. Flip String to Monotone Increasing *-
5+
6+
A binary string is monotone increasing if it consists of some number of 0's (possibly none), followed by some number of 1's (also possibly none).
7+
8+
You are given a binary string s. You can flip s[i] changing it from 0 to 1 or from 1 to 0.
9+
10+
Return the minimum number of flips to make s monotone increasing.
11+
12+
13+
14+
Example 1:
15+
16+
Input: s = "00110"
17+
Output: 1
18+
Explanation: We flip the last digit to get 00111.
19+
Example 2:
20+
21+
Input: s = "010110"
22+
Output: 2
23+
Explanation: We flip to get 011111, or alternatively 000111.
24+
Example 3:
25+
26+
Input: s = "00011000"
27+
Output: 2
28+
Explanation: We flip to get 00000000.
29+
30+
31+
Constraints:
32+
33+
1 <= s.length <= 105
34+
s[i] is either '0' or '1'.
35+
36+
37+
*/
38+
39+
import 'dart:math';
40+
41+
class A {
42+
int minFlipsMonoIncr(String s) {
43+
// Initialize variables to store the minimum number of flips and the number of flips currently needed
44+
int ans = 0;
45+
int noOfFlip = 0;
46+
47+
// Iterate through each character in the input string
48+
for (int i = 0; i < s.length; i++) {
49+
// If the current character is a '0', update the minimum number of flips and add 1 to the current number of flips
50+
if (s.codeUnitAt(i) == '0')
51+
ans = min(noOfFlip, ans + 1);
52+
// If the current character is a '1', increment the number of flips
53+
else
54+
noOfFlip++;
55+
}
56+
// Return the minimum number of flips
57+
return ans;
58+
}
59+
}
60+
61+
class B {
62+
int minFlipsMonoIncr(String s) {
63+
int n = s.length;
64+
int ans = 100000;
65+
List<int> oneCount = List.filled(n, 0);
66+
List<int> zeroCount = List.filled(n, 0);
67+
oneCount[0] = 0;
68+
zeroCount[n - 1] = 0;
69+
for (int i = 1; i < n; i++) {
70+
if (s[i - 1] == '1')
71+
oneCount[i] = oneCount[i - 1] + 1;
72+
else
73+
oneCount[i] = oneCount[i - 1];
74+
if (s[n - i] == '0')
75+
zeroCount[n - i - 1] = zeroCount[n - i] + 1;
76+
else
77+
zeroCount[n - i - 1] = zeroCount[n - i];
78+
}
79+
for (int i = 0; i < n; i++) {
80+
ans = min(ans, zeroCount[i] + oneCount[i]);
81+
}
82+
return ans;
83+
}
84+
}
85+
86+
class C {
87+
//TLE
88+
int minFlipsMonoIncr(String s) {
89+
int n = s.length;
90+
int mini = 10000;
91+
List<int> ans = List.filled(n, 0);
92+
for (int i = 0; i < n; i++) {
93+
ans[i] += oneCount(s.substring(0, i));
94+
ans[i] += zeroCount(s.substring(i + 1));
95+
mini = min(mini, ans[i]);
96+
}
97+
return mini;
98+
}
99+
100+
int oneCount(String s) {
101+
int n = s.length;
102+
int cnt = 0;
103+
for (int i = 0; i < n; i++) {
104+
if (s[i] == '1') cnt++;
105+
}
106+
return cnt;
107+
}
108+
109+
int zeroCount(String s) {
110+
int n = s.length;
111+
int cnt = 0;
112+
for (int i = 0; i < n; i++) {
113+
if (s[i] == '0') cnt++;
114+
}
115+
return cnt;
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package main
2+
3+
// func minFlipsMonoIncr(s string) int {
4+
// var answer int = 0
5+
// var numberOfFlips = 0
6+
// for i := 0; i < len(s); i++ {
7+
// if s[i] == '0' {
8+
// answer = min(numberOfFlips, answer+1)
9+
// } else {
10+
// numberOfFlips++
11+
// }
12+
// }
13+
// return answer
14+
// }
15+
16+
func min(a int, b int) int {
17+
if a < b {
18+
return a
19+
} else {
20+
return b
21+
}
22+
}
23+
24+
func minFlipsMonoIncr(s string) int {
25+
var n int = len(s)
26+
var ans int = 100000
27+
var oneCount []int = make([]int, n)
28+
var zeroCount []int = make([]int, n)
29+
oneCount[0] = 0
30+
zeroCount[n-1] = 0
31+
32+
for i := 1; i < n; i++ {
33+
if s[i-1] == '1' {
34+
oneCount[i] = oneCount[i-1] + 1
35+
} else {
36+
oneCount[i] = oneCount[i-1]
37+
}
38+
if s[n-i] == '0' {
39+
zeroCount[n-i-1] = zeroCount[n-i] + 1
40+
} else {
41+
zeroCount[n-i-1] = zeroCount[n-i]
42+
}
43+
for i := 0; i < n; i++ {
44+
ans = min(ans, zeroCount[i]+oneCount[i])
45+
}
46+
}
47+
return ans
48+
}
49+
50+
/*
51+
52+
int minFlipsMonoIncr(String s) {
53+
int n = s.length;
54+
int ans = 100000;
55+
List<int> oneCount = List.filled(n, 0);
56+
List<int> zeroCount = List.filled(n, 0);
57+
oneCount[0] = 0;
58+
zeroCount[n - 1] = 0;
59+
for (int i = 1; i < n; i++) {
60+
if (s[i - 1] == '1')
61+
oneCount[i] = oneCount[i - 1] + 1;
62+
else
63+
oneCount[i] = oneCount[i - 1];
64+
if (s[n - i] == '0')
65+
zeroCount[n - i - 1] = zeroCount[n - i] + 1;
66+
else
67+
zeroCount[n - i - 1] = zeroCount[n - i];
68+
}
69+
for (int i = 0; i < n; i++) {
70+
ans = min(ans, zeroCount[i] + oneCount[i]);
71+
}
72+
return ans;
73+
}
74+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# 🔥 3 Approaches 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
### Approach
6+
7+
Skip the loop until we find the first '1'
8+
after finding first occurrence of '1' we have to keep a count of number of '1's in countOnes
9+
10+
if we find any '0' after occurrence of '1' it means turning that '0' to one may be one possible solution so we have to keep number of flip needed in countFlips variable
11+
12+
if value of countFlips is greater than countOnes(number of ones) then converting all '1' to '0' would be better option so assign value of countOnes to the countFlips
13+
14+
in the end we will return countFlips
15+
16+
### Complexity
17+
18+
#### Time complexity
19+
20+
O(n)
21+
22+
#### Space complexity
23+
24+
O(1)
25+
26+
```dart
27+
class Solution {
28+
int minFlipsMonoIncr(String s) {
29+
// Initialize variables to store the minimum number of flips and the number of flips currently needed
30+
int ans = 0;
31+
int noOfFlip = 0;
32+
33+
// Iterate through each character in the input string
34+
for (int i = 0; i < s.length; i++) {
35+
// If the current character is a '0', update the minimum number of flips and add 1 to the current number of flips
36+
if (s.codeUnitAt(i) == '0')
37+
ans = min(noOfFlip, ans + 1);
38+
// If the current character is a '1', increment the number of flips
39+
else
40+
noOfFlip++;
41+
}
42+
// Return the minimum number of flips
43+
return ans;
44+
}
45+
}
46+
```
47+
48+
## Solution - 2
49+
50+
### Intuition
51+
52+
Counting the no. of 1's to flip on left side + no. of 0's to flip on right side of each index i and then select minimum flips from it.
53+
54+
### Approach
55+
56+
Maintain a prefix and suffix array to count 1 on left side and 0 on right side.
57+
58+
#### Complexity
59+
60+
Time complexity:
61+
O(n)
62+
63+
#### Space complexity
64+
65+
O(n)
66+
67+
```dart
68+
class Solution {
69+
int minFlipsMonoIncr(String s) {
70+
int n = s.length;
71+
int ans = 100000;
72+
List<int> oneCount = List.filled(n, 0);
73+
List<int> zeroCount = List.filled(n, 0);
74+
oneCount[0] = 0;
75+
zeroCount[n - 1] = 0;
76+
for (int i = 1; i < n; i++) {
77+
if (s[i - 1] == '1')
78+
oneCount[i] = oneCount[i - 1] + 1;
79+
else
80+
oneCount[i] = oneCount[i - 1];
81+
if (s[n - i] == '0')
82+
zeroCount[n - i - 1] = zeroCount[n - i] + 1;
83+
else
84+
zeroCount[n - i - 1] = zeroCount[n - i];
85+
}
86+
for (int i = 0; i < n; i++) {
87+
ans = min(ans, zeroCount[i] + oneCount[i]);
88+
}
89+
return ans;
90+
}
91+
}
92+
```
93+
94+
### Solution - 3
95+
96+
```dart
97+
class Solution {
98+
//TLE
99+
int minFlipsMonoIncr(String s) {
100+
int n = s.length;
101+
int mini = 10000;
102+
List<int> ans = List.filled(n, 0);
103+
for (int i = 0; i < n; i++) {
104+
ans[i] += oneCount(s.substring(0, i));
105+
ans[i] += zeroCount(s.substring(i + 1));
106+
mini = min(mini, ans[i]);
107+
}
108+
return mini;
109+
}
110+
111+
int oneCount(String s) {
112+
int n = s.length;
113+
int cnt = 0;
114+
for (int i = 0; i < n; i++) {
115+
if (s[i] == '1') cnt++;
116+
}
117+
return cnt;
118+
}
119+
120+
int zeroCount(String s) {
121+
int n = s.length;
122+
int cnt = 0;
123+
for (int i = 0; i < n; i++) {
124+
if (s[i] == '0') cnt++;
125+
}
126+
return cnt;
127+
}
128+
}
129+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
186186
- [**1061.** Lexicographically Smallest Equivalent String](LexicographicallySmallestEquivalentString/lexicographically_smallest_equivalent_string.dart)
187187
- [**2421.** Number of Good Paths](NumberOfGoodPaths/number_of_good_paths.dart)
188188
- [**57.** Insert Interval](InsertInterval/insert_interval.dart)
189+
- [**926.** Flip String to Monotone Increasing](FlipStringToMonotoneIncreasing/flip_string_to_monotone_increasing.dart)
189190

190191
## Reach me via
191192

0 commit comments

Comments
 (0)