LeetCode Weekly Contest 185
| Rank | Name | Score | Finish Time | Q1 (4) | Q2 (4) | Q3 (6) | Q4 (7) |
|---|---|---|---|---|---|---|---|
| 703 / 9206 | YoungForest | 12 | 0:36:35 | 0:10:24 | 0:22:03 | 0:31:35 1 | null |
1417. Reformat The String
Count the number of digits and letters separately. If the difference in counts is no greater than 1, the string can be reformatted.
Choose the category with more characters first.
Time complexity: O(N),
space complexity: O(N). A two-pointer method can be used to save the space for storing the digit and letter strings.
1 | class Solution { |
1418. Display Table of Food Orders in a Restaurant
This tests the use of data structures. Traverse orders and convert it into a dish -> table HashMap, while using a set to store table numbers.
Time complexity: O(orders.length * orders[i].length),
space complexity: O(tables.length * dishes.length).
1 | class Solution { |
1419. Minimum Number of Frogs Croaking
A finite state machine. Record the number of frogs at each different position, and simulate the entire croaking process.
Time complexity: O(croakOfFrogs.length),
space complexity: O(croak.length).
1 | class Solution { |
1420. Build Array Where You Can Find The Maximum Exactly K Comparisons
Recently I have solved many DP problems, but when I encounter a new problem, I still cannot always solve it. The main difficulties are:
- Determining the definition of
dp. Usually copying the target directly works, but not always. - Determining boundary conditions, meaning the recursive exit conditions.
- The state transition equation, which is tightly connected to the definition of
dp.
In this problem, dp[i][currentMaxValue][cost] represents the number of arrays of length i whose maximum value is currentMaxValue and whose increasing subsequence length is cost.
There are two types of state transitions:
The first is when the maximum value of the length i-1 array is already currentMaxValue. In that case, we can add [1, currentMaxValue] at the i-th position, and cost stays unchanged.
The second is when the maximum value of the length i-1 array is less than currentMaxValue. In this case, the i-th position must be currentMaxValue, and the previous cost is cost-1. Note that the minimum previous maximum value is cost-1, with the increasing sequence being 1, ..., cost-1.
Time complexity: O(n * m * k * (m - k)),
space complexity: O(n * m * k).
1 | class Solution: |
A DP Problem From Meituan’s Written Test
I participated in Meituan’s summer internship written test on Thursday. The last problem was also DP, but I did not solve it at the time. In fact, it was just that kind of problem.
Given two strings
SandT, find the number of substrings ofSthat are equal to subsequences ofT. Note that even if substrings are the same, different positions count as different ways.
The maximum length ofSandTis 5000.
Time complexity: (len(S) * (len(T) ^ 2)),
space complexity: (len(S) * len(T)).
1 | import functools |