Skip to content

Commit dfbc11a

Browse files
committed
day 10
1 parent a87f835 commit dfbc11a

File tree

2 files changed

+75
-11
lines changed

2 files changed

+75
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Flip String to Monotone Increasing
3+
==================================
4+
5+
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).
6+
7+
You are given a binary string s. You can flip s[i] changing it from 0 to 1 or from 1 to 0.
8+
9+
Return the minimum number of flips to make s monotone increasing.
10+
11+
Example 1:
12+
Input: s = "00110"
13+
Output: 1
14+
Explanation: We flip the last digit to get 00111.
15+
16+
Example 2:
17+
Input: s = "010110"
18+
Output: 2
19+
Explanation: We flip to get 011111, or alternatively 000111.
20+
21+
Example 3:
22+
Input: s = "00011000"
23+
Output: 2
24+
Explanation: We flip to get 00000000.
25+
26+
Constraints:
27+
1 <= s.length <= 105
28+
s[i] is either '0' or '1'.
29+
*/
30+
31+
class Solution
32+
{
33+
public:
34+
int dfs(string &s, int i, int prev, int memo[100001][2])
35+
{
36+
if (i == s.size())
37+
return 0;
38+
39+
if (memo[i][prev] != -1)
40+
return memo[i][prev];
41+
42+
int ans = INT_MAX;
43+
44+
if (prev == 0)
45+
{
46+
ans = min(ans, (s[i] == '1' ? 1 : 0) + dfs(s, i + 1, 0, memo));
47+
ans = min(ans, (s[i] == '1' ? 0 : 1) + dfs(s, i + 1, 1, memo));
48+
}
49+
else
50+
{
51+
ans = min(ans, (s[i] == '1' ? 0 : 1) + dfs(s, i + 1, 1, memo));
52+
}
53+
54+
return memo[i][prev] = ans;
55+
}
56+
57+
int minFlipsMonoIncr(string s)
58+
{
59+
int memo[100001][2];
60+
memset(memo, -1, sizeof memo);
61+
return dfs(s, 0, 0, memo);
62+
}
63+
};
+12-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# August 2021 LeetCoding Challenge
22

3-
| Day | Question Links | Solutions |
4-
| :-: | :------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------: |
5-
| | [Optimize Water Distribution in a Village](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3834/) | [cpp](./Optimize%20Water%20Distribution%20in%20a%20Village.cpp) |
6-
| 1. | [Making A Large Island](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3835/) | [cpp](./01.%20Making%20A%20Large%20Island.cpp) |
7-
| 2. | [Two Sum](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3836/) | [cpp](./02.%20Two%20Sum.cpp) |
8-
| 3. | [Subsets II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3837/) | [cpp](./03.%20Subsets%20II.cpp) |
9-
| 4. | [Path Sum II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3838/) | [cpp](./04.%20Path%20Sum%20II.cpp) |
10-
| 5. | [Stone Game](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3870/) | [cpp](./05.%20Stone%20Game.cpp) |
11-
| 6. | [N-ary Tree Level Order Traversal](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3871/) | [cpp](./06.%20N-ary%20Tree%20Level%20Order%20Traversal.cpp) |
12-
| 7. | [Palindrome Partitioning II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3872/) | [cpp](./07.%20Palindrome%20Partitioning%20II.cpp) |
13-
| 9. | [Add Strings](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3875/) | [cpp](./09.%20Add%20Strings.cpp) |
3+
| Day | Question Links | Solutions |
4+
| :-: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------: |
5+
| | [Optimize Water Distribution in a Village](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3834/) | [cpp](./Optimize%20Water%20Distribution%20in%20a%20Village.cpp) |
6+
| 1. | [Making A Large Island](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3835/) | [cpp](./01.%20Making%20A%20Large%20Island.cpp) |
7+
| 2. | [Two Sum](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3836/) | [cpp](./02.%20Two%20Sum.cpp) |
8+
| 3. | [Subsets II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3837/) | [cpp](./03.%20Subsets%20II.cpp) |
9+
| 4. | [Path Sum II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3838/) | [cpp](./04.%20Path%20Sum%20II.cpp) |
10+
| 5. | [Stone Game](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3870/) | [cpp](./05.%20Stone%20Game.cpp) |
11+
| 6. | [N-ary Tree Level Order Traversal](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3871/) | [cpp](./06.%20N-ary%20Tree%20Level%20Order%20Traversal.cpp) |
12+
| 7. | [Palindrome Partitioning II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3872/) | [cpp](./07.%20Palindrome%20Partitioning%20II.cpp) |
13+
| 9. | [Add Strings](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3875/) | [cpp](./09.%20Add%20Strings.cpp) |
14+
| 10. | [Flip String to Monotone Increasing](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3876/) | [cpp](./10.%20Flip%20String%20to%20Monotone%20Increasing.cpp) |

0 commit comments

Comments
 (0)