Weekly Contest 154
1189. Maximum Number of Balloons
Given a string
text
, you want to use the characters oftext
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
2 >Input: text = "nlaebolko"
>Output: 1
1
2 >Input: text = "loonbalxballpoon"
>Output: 2
1
2 >Input: text = "leetcode"
>Output: 0
- 解析:查看这个里面有多少个”ballon”单词,不能使用以及用过的字母。我用的map来做:
1 | class Solution { |
别人的方法:思路一样,找最小出现字符的数目就行。
1 | class Solution: |
1190. Reverse Substrings Between Each Pair of Parentheses
You are 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 brackets.
1
2 >Input: s = "a(bcdefghijkl(mno)p)q"
>Output: "apmnolkjihgfedcbq"
解析:我用的python自带的find、rfind函数来做的,就是找到左右括号,然后切片反正:
1
2
3
4
5
6
7
8
9class Solution:
def reverseParentheses(self, s: str) -> str:
while 1:
l = s.rfind('(')
r = s[l:].find(')') + l
if l == -1 or r == -1:
break
s = s[:l] + s[l + 1 : r][::-1] + s[r + 1:]
return s别人的做法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31class Solution {
public:
string reverseParentheses(string s) {
int n = s.size();
if (s.find('(') == string::npos)
return s;
int position = s.find('(');
int level = 0; //记录对称的括号是否平衡
for (int i = position; i < n; i++)
if (s[i] == '(')
level++;
else if (s[i] == ')') {
level--;
if (level == 0) {
string answer = s.substr(0, position);//不需要反转的左括号前的子串
string sub = s.substr(position + 1, i - (position + 1));//最外层括号的子串
sub = reverseParentheses(sub); //对内部的括号层做反转的结果
reverse(sub.begin(), sub.end()); //最后对最外层做反转
answer += sub; //加上前后的子串
answer += reverseParentheses(s.substr(i + 1));
return answer;
}
}
assert(false);
}
};
1191. K-Concatenation Maximum Sum
Given an integer array
arr
and an integerk
, modify the array by repeating itk
times.For example, if
arr = [1, 2]
andk = 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 is0
.As the answer can be very large, return the answer modulo
10^9 + 7
.
- 解析:先知道这怎么去求解一个数组的最大子数组和:https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/
若整个数组的和都小于等于0,那么k大于1时,只考虑数组的前缀和数组的后缀大小和,与数组的最大子数组和的大小,谁更大。
1 | class Solution { |
1192. Critical Connections in a Network
There are
n
servers numbered from0
ton-1
connected by undirected server-to-serverconnections
forming a network whereconnections[i] = [a, b]
represents a connection between serversa
andb
. 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.
Constraints:
1 <= n <= 10^5
n-1 <= connections.length <= 10^5
connections[i][0] != connections[i][1]
- There are no repeated connections.
1 | Input: n = 4, connections = [[0,1],[1,2],[2,0],[1,3]] |
先要知道的图的存储方式:
数组实现图的邻接表方法:https://wiki.jikexueyuan.com/project/easy-learn-algorithm/clever-adjacency-list.html
输入一个图:
1
2
3
4
5
64 5 //节点数目 、 边数目
1 4 9 //下面是输入的边,代表:起点、终点、权重
4 3 8
1 2 5
2 4 6
1 3 7
设数组first,长度为节点的数目,first 数组来存储每个顶点其中一条边的编号。
比如1号顶点有一条边是 “1 4 9”(该条边的编号是 1),那么就将 first[1]的值设为 1。如果某个顶点 i 没有以该顶点为起始点的边,则将 first[ i ] 的值设为-1。
设数组next,长度为边的数目,next[i]存储的是“编号为i的边”的“前一条边”的编号。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29int main()
{
int n = 4 ,m = 5;//点数和边数
//u、v代表一对有向边
int u[6] = {0, 1, 4, 1, 2, 1}; //起点
int v[6] = {0, 4, 3, 2, 4, 3}; //终点
int i;
int first[n + 1],next[m + 1]; //0位置处不用
for(int i=1; i<=n; i++)
first[i]=-1;
for(i=1; i<=m; i++)
{
//当前输入第i条边;对应节点(起点)号码为u[i]
next[i] = first[u[i]];
first[u[i]] = i;
}
//遍历1号顶点所有边
int k = first[1]; //1号顶点其中的一条边的编号(其实也是最后读入的边)
while(k != -1)
{
cout << u[k] << "--->" << v[k] << endl;
k = next[k];
}
}
output:
1--->3
1--->2
1--->4
无向图的类似存储方式:
1 | //因为是无向图,所以边的数目是 MAXM * 2. |
- 类似于Tarjan 算法:Tarjan 思想:对于一个连通图,递归可以访问到所有顶点和边。
1 | ------------ |
而对于割边,例如2-4
,递归的时候,2-4
递归的所有顶点都大于2
。
而对于非割边,比如5-6
,递归的时候,6
可以找到更小的4
。
总结一下就是,这个边递归找的最小编号都比自己大,那这个边就是割边,否则不是割边。
所以我们需要做的就是递归寻找每个顶点能够到达的最小编号,然后比大小即可。
1 | void Tarjan(int v, int pre = -1) |
完整代码:
1 | const int MAXN = 1e5 + 10; |
python:
1 | import collections |