Skip to content

Commit d156493

Browse files
committed
feat: add solutions to lc problems: No.2608,2609
* No.2608.Shortest Cycle in a Graph * No.2609.Find the Longest Balanced Substring of a Binary String
1 parent f3de1c7 commit d156493

File tree

2 files changed

+35
-0
lines changed
  • solution/2600-2699

2 files changed

+35
-0
lines changed

solution/2600-2699/2608.Shortest Cycle in a Graph/README_EN.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@
4141

4242
## Solutions
4343

44+
**方法一:Enumerate edges + BFS**
45+
46+
We first construct the adjacency list $g$ of the graph according to the array $edges$, where $g[u]$ represents all the adjacent vertices of vertex $u$.
47+
48+
Then we enumerate the two-directional edge $(u, v)$, if the path from vertex $u$ to vertex $v$ still exists after deleting this edge, then the length of the shortest cycle containing this edge is $dist[v] + 1$, where $dist[v]$ represents the shortest path length from vertex $u$ to vertex $v$. We take the minimum of all these cycles.
49+
50+
The time complexity is $O(m^2)$ and the space complexity is $O(m + n)$, where $m$ and $n$ are the length of the array $edges$ and the number of vertices.
51+
52+
**Approach 2: Enumerate points + BFS**
53+
54+
Similar to Approach 1, we first construct the adjacency list $g$ of the graph according to the array $edges$, where $g[u]$ represents all the adjacent vertices of vertex $u$.
55+
56+
Then we enumerate the vertex $u$, if there are two paths from vertex $u$ to vertex $v$, then we currently find a cycle, the length is the sum of the length of the two paths. We take the minimum of all these cycles.
57+
58+
The time complexity is $O(m \times n)$ and the space complexity is $O(m + n)$, where $m$ and $n$ are the length of the array $edges$ and the number of vertices.
59+
4460
<!-- tabs:start -->
4561

4662
### **Python3**

solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README_EN.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@
4747

4848
## Solutions
4949

50+
**Approach 1: Brute force**
51+
52+
Since the range of $n$ is small, we can enumerate all substrings $s[i..j]$ to check if it is a balanced string. If so, update the answer.
53+
54+
The time complexity is $O(n^3)$, and the space complexity is $O(1)$. Where $n$ is the length of string $s$.
55+
56+
**Approach 2: Enumeration optimization**
57+
58+
We use variables $zero$ and $one$ to record the number of continuous $0$ and $1$.
59+
60+
Traverse the string $s$, for the current character $c$:
61+
62+
- If the current character is `'0'`, we check if $one$ is greater than $0$, if so, we reset $zero$ and $one$ to $0$, and then add $1$ to $zero$.
63+
- If the current character is `'1'`, we add $1$ to $one$, and update the answer to $ans = max(ans, 2 \times min(one, zero))$.
64+
65+
After the traversal is complete, we can get the length of the longest balanced substring.
66+
67+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of string $s$.
68+
5069
<!-- tabs:start -->
5170

5271
### **Python3**

0 commit comments

Comments
 (0)