LeetCode Weekly Contest 243
| Rank | Name | Score | Finish Time | Q1 (3) | Q2 (5) | Q3 (5) | Q4 (6) |
|---|---|---|---|---|---|---|---|
| 95 / 12835 | YoungForest | 18 | 1:19:18 | 0:02:50 | 0:11:21 | 0:36:27 🐞1 | 1:04:18 🐞2 |
LingShen’s big data:
1880,Check if Word Equals Summation of Two Words,check-if-word-equals-summation-of-two-words,1187.1641565458
1881,Maximum Value after Insertion,maximum-value-after-insertion,1381.2168789318
1882,Process Tasks Using Servers,process-tasks-using-servers,1979.1112273597
1883,Minimum Skips to Arrive at Meeting On Time,minimum-skips-to-arrive-at-meeting-on-time,2587.8725248485
1880. Check if Word Equals Summation of Two Words
A warm-up problem. Convert strings to numbers according to the problem statement, then judge.
1 | class Solution: |
Time complexity: O(N),
space complexity: O(N).
1881. Maximum Value after Insertion
Greedy. For a positive number, insert before the first digit smaller than x; for a negative number, insert before the first digit greater than x.
1 | class Solution: |
Time complexity: O(N),
space complexity: O(N).
1882. Process Tasks Using Servers
Direct brute-force simulation is enough. Use priority queues to maintain idle servers, waiting tasks, and release times.
During simulation, note that time cannot move one unit at a time. It should only move to moments when events happen: tasks begin waiting or servers are released.
Also note that the time value may exceed int; using long long is safer.
1 | class Solution { |
Time complexity: O((m + n) * log n),
space complexity: O(m + n).
1883. Minimum Skips to Arrive at Meeting On Time
Dynamic Programming.
dp(i, k) represents the earliest ending time from dist0 to i inclusive, with k rests.
The state transition is:
dp(i, k) = min(
dp(i-1, k) + .. no rest at the last stop
dp(i-1, k-1) + .. rest at the last stop
),
Then use binary search to find the minimum k such that dp(n-1, k) <= hoursBefore.
Actually, binary search is not necessary; scanning from small to large would also work, because the time-complexity bottleneck is not there, but in computing dp.
Recently I have often encountered Dynamic Programming problems. Previously, when finding the state transition, my time complexity was always too high because I tried to enumerate where the last rest position occurred. The correct approach should be to enumerate only whether the last stop rests. This reduces time complexity by one factor of n.
This problem has a floating-point precision pitfall, and I got WA twice because of it.
There are two solutions:
- Add an
err; I used 10^-9 here, which is enough in most cases. - Convert to integers. This may require
long long. In this problem, represent alltime * speedas distance, which avoids precision loss.
1 | class Solution: |
Time complexity: O(n ^ 2),
space complexity: O(m ^ 2).