LeetCode Weekly Contest 234
| Rank | Name | Score | Finish Time | Q1 (3) | Q2 (5) | Q3 (5) | Q4 (6) |
|---|---|---|---|---|---|---|---|
| 672 / 12421 | YoungForest | 19 | 1:19:08 | 0:12:04 2 | 0:23:51 | 0:29:26 | 0:54:08 3 |
It was check-in time again. I had been doing this brutal check-in for five consecutive weeks. And honestly, I did not feel the problems were that easy this time. I got 5 WAs and my mental state more or less exploded, but the ranking was still not ideal. It feels like LeetCode is getting more and more competitive.
1805. Number of Different Integers in a String
This problem is actually much nicer in Python and can be implemented much faster. Python’s advantages with strings and big integers are hard to beat.
I still insisted on doing it in C++, running into all kinds of unfamiliar and inconvenient string operations. I got 2 WAs.
1 | class Solution { |
Time complexity: O(N),
space complexity: O(N).
1806. Minimum Number of Operations to Reinitialize a Permutation
I thought about this problem backwards.
The inverse operation moves odd positions to the second half and even positions to the first half.
Also, by observing the transformation process, we only need to track one number. If it returns to its original position, all the remaining numbers will also return to their original positions.
1 | class Solution { |
Time complexity: O(N),
space complexity: O(1).
1807. Evaluate the Bracket Pairs of a String
To be honest, as the third problem, this did not have the corresponding difficulty. It could totally have been a warm-up problem.
Just implement it directly.
Key ideas: string processing + dictionary/Map.
1 | class Solution { |
Time complexity: O(s.size() + knowledge.size() * (knowledge[i][0].size() + knowledge[i][1].size())),
space complexity: O(s.size() + knowledge.size() * (knowledge[i][0].size() + knowledge[i][1].size())).
1808. Maximize Number of Nice Divisors
The problem can be transformed into: with a fixed sum, maximize the product.
The fixed sum is primeFactors. Then the number of nice divisors is the product, which is exactly the objective to maximize in the problem.
If you look at nice divisors, they are essentially the product of the counts of each prime factor.
Searching fix sum max multiplication on Google, the third result was a similar GeeksforGeeks problem, Breaking an Integer to get Maximum Product. LeetCode also has the original problem, 343.
I tried to copy its code, but there are two differences:
- GeeksforGeeks requires a break and does not allow using the number as a whole. This problem allows not breaking it. That leads to special cases: when
primeFactors = 2or3, returnn. - The GeeksforGeeks code uses a
forloop to multiply by 3. The time complexity is O(n / 3), which will TLE here. I wrote my ownpowto multiply by 3 faster, with time complexity O(log n). This is basically LeetCode 50 Pow(x, n).
1 | class Solution { |
Time complexity: O(log n),
space complexity: O(1).