LeetCode weekly contest 154
Last weekend I was in Belgium, and the contest time was from 4:30 a.m. to 6:00 a.m. The timing was unsuitable, so I did not participate. I found that only the biweekly contest time, Saturday from 4:30 p.m. to 6:00 p.m., is somewhat suitable. My goal of ranking 2000 this year is probably going to be postponed. Even in the best case, the number of contests I can participate in is only one-third of what it would be in China.
1189. Maximum Number of Balloons
Just count the frequency of each letter. Note that l and o each need to appear twice to form one balloon.
Time complexity: O(N),
space complexity: O(1).
1 | class Solution { |
1190. Reverse Substrings Between Each Pair of Parentheses
Because the maximum string length is 2000, a brute-force method is enough. Use a stack for parenthesis matching.
Time complexity: O(N ^ 2),
space complexity: O(N).
1 | class Solution { |
After reading the discuss section, I found a solution with time complexity O(N).
The first pass finds all matching parentheses, and the second pass constructs the result string. Each parenthesis acts like a portal: it changes the direction of movement while jumping to the matching parenthesis.
1 | string reverseParentheses(string s) { |
1191. K-Concatenation Maximum Sum
This is the maximum subarray sum problem. The special point is that the array can be repeated k times.
By observing carefully, we can see that when the sum of the entire array is less than or equal to 0, according to a greedy idea, the final answer must be the answer formed by repeating the array twice: either a subarray within the array itself, or the maximum prefix sum plus the maximum suffix sum.
When the sum of the entire array is greater than 0, the answer is the sum of k - 2 arrays plus the maximum prefix sum plus the maximum suffix sum.
Time complexity: O(N),
space complexity: O(1).
1 | class Solution { |