Maximum Number of Balloons
Given a string text, you want to use the characters of text to form as many instances of the word “balloon” as possible.
You can use each character in text at most once. Return the maximum number of instances that can be formed.
1 | Example 1: |
Constraints:
- 1 <= text.length <= 10^4
- text consists of lower case English letters only.
1 | class Solution { |
K-Concatenation Maximum Sum
Given an integer array arr and an integer k, modify the array by repeating it k times.
For example, if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2].
Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be 0 and its sum in that case is 0.
As the answer can be very large, return the answer modulo 10^9 + 7.
1 | Example 1: |
Constraints:
- 1 <= arr.length <= 10^5
- 1 <= k <= 10^5
- -10^4 <= arr[i] <= 10^4
1 | 1. all positive : [1,2,3], k = 3, array_sum = 6 |
1 | class Solution { |
Reverse Substrings Between Each Pair of Parentheses
Given a string s that consists of lower case English letters and brackets.
Reverse the strings in each pair of matching parentheses, starting from the innermost one.
Your result should not contain any bracket.
1 | Example 1: |
Constraints:
- 0 <= s.length <= 2000
- s only contains lower case English characters and parentheses.
- It’s guaranteed that all parentheses are balanced.
给一个有括号的字符串,一个匹配的括号代表括号里的内容需要反转。
1 | class Solution { |
Critical Connections in a Network
There are n servers numbered from 0 to n-1 connected by undirected server-to-server connections forming a network where connections[i] = [a, b] represents a connection between servers a and b. Any server can reach any other server directly or indirectly through the network.
A critical connection is a connection that, if removed, will make some server unable to reach some other server.
Return all critical connections in the network in any order.
1 | Example 1: |
Constraints:
- 1 <= n <= 10^5
- n-1 <= connections.length <= 10^5
- connections[i][0] != connections[i][1]
- There are no repeated connections.
给一个无向连通图,求所有割边(桥)。
割边定义:删除这个边,连通图就不再联通,即被划分为两个独立部分。
Tarjan 方法
对于一个连通图,递归可以访问到所有顶点和边。
1 | ------------ |
而对于割边,例如2-4,递归的时候,2-4递归的所有顶点都大于2。
而对于非割边,比如5-6,递归的时候,6可以找到更小的4。
总结一下就是,这个边递归找的最小编号都比自己大,那这个边就是割边,否则不是割边。
所以我们需要做的就是递归寻找每个顶点能够到达的最小编号,然后比大小即可。
1 | class Solution { |