Skip to content

Commit 523a4f6

Browse files
committed
feat: add solutions to lc problems: No.2609~2612
* No.2609.Find the Longest Balanced Substring of a Binary String * No.2610.Convert an Array Into a 2D Array With Conditions * No.2611.Mice and Cheese * No.2612.Minimum Reverse Operations
1 parent c9e7bdb commit 523a4f6

File tree

29 files changed

+1565
-4
lines changed

29 files changed

+1565
-4
lines changed

solution/2500-2599/2550.Count Collisions of Monkeys on a Polygon/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<p>现在有一个正凸多边形,其上共有 <code>n</code> 个顶点。顶点按顺时针方向从 <code>0</code> 到 <code>n - 1</code> 依次编号。每个顶点上 <strong>正好有一只猴子</strong> 。下图中是一个 6 个顶点的凸多边形。</p>
1010

11-
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2500-2599/2550.Count%20Collisions%20of%20Monkeys%20on%20a%20Polygon/images/hexagon.jpg" style="width: 300px; height: 293px;"></p>
11+
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2500-2599/2550.Count%20Collisions%20of%20Monkeys%20on%20a%20Polygon/images/hexagon.jpg" style="width: 300px; height: 293px;" /></p>
1212

1313
<p>每个猴子同时移动到相邻的顶点。顶点 <code>i</code> 的相邻顶点可以是:</p>
1414

@@ -17,7 +17,7 @@
1717
<li>逆时针方向的顶点 <code>(i - 1 + n) % n</code> 。</li>
1818
</ul>
1919

20-
<p>如果移动后至少有两个猴子位于同一顶点,则会发生 <strong>碰撞</strong> 。</p>
20+
<p>如果移动后至少有两只猴子停留在同一个顶点上或者相交在一条边上,则会发生 <strong>碰撞</strong> 。</p>
2121

2222
<p>返回猴子至少发生 <strong>一次碰撞 </strong>的移动方法数。由于答案可能非常大,请返回对 <code>10<sup>9</sup>+7</code> 取余后的结果。</p>
2323

@@ -27,7 +27,8 @@
2727

2828
<p><strong>示例 1:</strong></p>
2929

30-
<pre><strong>输入:</strong>n = 3
30+
<pre>
31+
<strong>输入:</strong>n = 3
3132
<strong>输出:</strong>6
3233
<strong>解释:</strong>共计 8 种移动方式。
3334
下面列出两种会发生碰撞的方式:
@@ -38,7 +39,8 @@
3839

3940
<p><strong>示例 2:</strong></p>
4041

41-
<pre><strong>输入:</strong>n = 4
42+
<pre>
43+
<strong>输入:</strong>n = 4
4244
<strong>输出:</strong>14
4345
<strong>解释:</strong>可以证明,有 14 种让猴子碰撞的方法。</pre>
4446

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# [2609. 最长平衡子字符串](https://leetcode.cn/problems/find-the-longest-balanced-substring-of-a-binary-string)
2+
3+
[English Version](/solution/2600-2699/2609.Find%20the%20Longest%20Balanced%20Substring%20of%20a%20Binary%20String/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个仅由 <code>0</code> 和 <code>1</code> 组成的二进制字符串 <code>s</code> 。<span style="">&nbsp;</span><span style="">&nbsp;</span></p>
10+
11+
<p>如果子字符串中 <strong>所有的<span style=""> </span></strong><code><span style="">0</span></code><strong><span style=""> </span>都在 </strong><code>1</code><strong> 之前</strong> 且其中 <code>0</code> 的数量等于 <code>1</code> 的数量,则认为 <code>s</code> 的这个子字符串是平衡子字符串。请注意,空子字符串也视作平衡子字符串。<span style="">&nbsp;</span></p>
12+
13+
<p>返回&nbsp;<span style=""> </span><code>s</code> 中最长的平衡子字符串长度。</p>
14+
15+
<p>子字符串是字符串中的一个连续字符序列。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong>s = "01000111"
23+
<strong>输出:</strong>6
24+
<strong>解释:</strong>最长的平衡子字符串是 "000111" ,长度为 6 。
25+
</pre>
26+
27+
<p><strong>示例 2:</strong></p>
28+
29+
<pre>
30+
<strong>输入:</strong>s = "00111"
31+
<strong>输出:</strong>4
32+
<strong>解释:</strong>最长的平衡子字符串是 "0011" ,长度为 <span style="">&nbsp;</span>4 。
33+
</pre>
34+
35+
<p><strong>示例 3:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong>s = "111"
39+
<strong>输出:</strong>0
40+
<strong>解释:</strong>除了空子字符串之外不存在其他平衡子字符串,所以答案为 0 。
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= s.length &lt;= 50</code></li>
49+
<li><code>'0' &lt;= s[i] &lt;= '1'</code></li>
50+
</ul>
51+
52+
## 解法
53+
54+
<!-- 这里可写通用的实现逻辑 -->
55+
56+
<!-- tabs:start -->
57+
58+
### **Python3**
59+
60+
<!-- 这里可写当前语言的特殊实现逻辑 -->
61+
62+
```python
63+
class Solution:
64+
def findTheLongestBalancedSubstring(self, s: str) -> int:
65+
def check(i, j):
66+
cnt = 0
67+
for k in range(i, j + 1):
68+
if s[k] == '1':
69+
cnt += 1
70+
elif cnt:
71+
return False
72+
return cnt * 2 == (j - i + 1)
73+
74+
n = len(s)
75+
ans = 0
76+
for i in range(n):
77+
for j in range(i + 1, n):
78+
if check(i, j):
79+
ans = max(ans, j - i + 1)
80+
return ans
81+
```
82+
83+
### **Java**
84+
85+
<!-- 这里可写当前语言的特殊实现逻辑 -->
86+
87+
```java
88+
class Solution {
89+
public int findTheLongestBalancedSubstring(String s) {
90+
int n = s.length();
91+
int ans = 0;
92+
for (int i = 0; i < n; ++i) {
93+
for (int j = i + 1; j < n; ++j) {
94+
if (check(s, i, j)) {
95+
ans = Math.max(ans, j - i + 1);
96+
}
97+
}
98+
}
99+
return ans;
100+
}
101+
102+
private boolean check(String s, int i, int j) {
103+
int cnt = 0;
104+
for (int k = i; k <= j; ++k) {
105+
if (s.charAt(k) == '1') {
106+
++cnt;
107+
} else if (cnt > 0) {
108+
return false;
109+
}
110+
}
111+
return cnt * 2 == j - i + 1;
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
int findTheLongestBalancedSubstring(string s) {
122+
int n = s.size();
123+
int ans = 0;
124+
auto check = [&](int i, int j) -> bool {
125+
int cnt = 0;
126+
for (int k = i; k <= j; ++k) {
127+
if (s[k] == '1') {
128+
++cnt;
129+
} else if (cnt) {
130+
return false;
131+
}
132+
}
133+
return cnt * 2 == j - i + 1;
134+
};
135+
for (int i = 0; i < n; ++i) {
136+
for (int j = i + 1; j < n; ++j) {
137+
if (check(i, j)) {
138+
ans = max(ans, j - i + 1);
139+
}
140+
}
141+
}
142+
return ans;
143+
}
144+
};
145+
```
146+
147+
### **Go**
148+
149+
```go
150+
func findTheLongestBalancedSubstring(s string) (ans int) {
151+
n := len(s)
152+
check := func(i, j int) bool {
153+
cnt := 0
154+
for k := i; k <= j; k++ {
155+
if s[k] == '1' {
156+
cnt++
157+
} else if cnt > 0 {
158+
return false
159+
}
160+
}
161+
return cnt*2 == j-i+1
162+
}
163+
for i := 0; i < n; i++ {
164+
for j := i + 1; j < n; j++ {
165+
if check(i, j) {
166+
ans = max(ans, j-i+1)
167+
}
168+
}
169+
}
170+
return
171+
}
172+
173+
func max(a, b int) int {
174+
if a > b {
175+
return a
176+
}
177+
return b
178+
}
179+
```
180+
181+
### **...**
182+
183+
```
184+
185+
```
186+
187+
<!-- tabs:end -->

0 commit comments

Comments
 (0)