You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README_EN.md
+33-25
Original file line number
Diff line number
Diff line change
@@ -65,7 +65,21 @@ You can skip the first and third rest to arrive in ((7/2 + <u>0</u>) + (3/2 + 0)
65
65
66
66
## Solutions
67
67
68
-
### Solution 1
68
+
### Solution 1: Dynamic Programming
69
+
70
+
We define $f[i][j]$ as the shortest time considering the first $i$ roads and exactly skipping $j$ rest times. Initially, $f[0][0]=0$, and the rest $f[i][j]=\infty$.
71
+
72
+
Since we can choose to skip or not skip the rest time of the $i$-th road, we can list the state transition equation:
73
+
74
+
$$
75
+
f[i][j]=\min\left\{\begin{aligned} \lceil f[i-1][j]+\frac{d_i}{s}\rceil & \text{Do not skip the rest time of the $i$-th road} \\ f[i-1][j-1]+\frac{d_i}{s} & \text{Skip the rest time of the $i$-th road} \end{aligned}\right.
76
+
$$
77
+
78
+
Where $\lceil x\rceil$ represents rounding $x$ up. It should be noted that since we need to ensure that exactly $j$ rest times are skipped, we must have $j\le i$; moreover, if $j=0$, no rest time can be skipped.
79
+
80
+
Due to the possible precision error brought by floating-point operations and rounding up, we introduce a constant $eps = 10^{-8}$ to represent a very small positive real number. We subtract $eps$ before rounding the floating-point number, and finally, when comparing $f[n][j]$ and $hoursBefore$, we need to add $eps$.
81
+
82
+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$, where $n$ is the number of roads.
0 commit comments