Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

[pull] master from wisdompeak:master #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const int INF = INT_MAX / 2;
int dp[51][11][101];

class Solution {
public:
int minTravelTime(int l, int n, int K, vector<int>& pos, vector<int>& time) {

fill(&dp[0][0][0], &dp[0][0][0]+51*11*101, INT_MAX/2);

dp[0][0][time[0]] = 0;

for (int i=0; i<n; i++)
for (int m=0; m<=K; m++)
for (int v=0; v<=100; v++)
{
int t = 0;
for (int j=i+1; j<n && m+j-i-1<=K; j++)
{
t += time[j];
dp[j][m+j-i-1][t] = min(dp[j][m+j-i-1][t], dp[i][m][v] + v*(pos[j]-pos[i]));
}
}

int ans = INT_MAX/2;
for (int v = 0; v <= 100; v++) {
ans = min(ans, dp[n - 1][K][v]);
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### 3538.Merge-Operations-for-Minimum-Travel-Time

我们比较容易想到令dp[i][k]表示前进至第i个标记时、且做了k次合并的最短时间。此时为了计算时间,我们需要知道上一处合并点的位置j,以及从j到i的效率(即单位长度所用的时间)。考虑到约束条件`1 <= sum(time) <= 100​​​​​​`,这时一个可以遍历的数字,所以我们就可以将其作为第三个维度。即我们定理dp[i][k][v]表示前进至第i个标记时、恰做了k次合并、且从i开始的效率是v的情况下,所用的最短时间。

写出上述表达式之后,我们发现想要计算dp[i][k][v],如果只遍历上一个停留点j的位置是不够的,因为从j往后到i的效率也是未知的,无法计算从j到i所花的时间。似乎状态转移陷入了僵局。

其实基于dp[i][k][v],我们发现对未来的状态进行推断更容易一些。我们同样遍历下一个停留点的位置j,那么从i往后到j所需要的时间是基于v且已知的`t = v*(dist[j]-dist[i])`。此外,从j点往后的效率就是`v2=time[i:j]`的区间和,也是已知的。于是,我们就可以用`dp[i][k][v] + t`来尝试更新`dp[j][k+j-i-1][v2]`的值。

这样的时间复杂度就是`50*18*100*40 = 3e6`数量级,可以通过。
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,7 @@
* ``Infer future from current``
[2044.Count-Number-of-Maximum-Bitwise-OR-Subsets](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2044.Count-Number-of-Maximum-Bitwise-OR-Subsets) (M)
[2742.Painting-the-Walls](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2742.Painting-the-Walls) (H)
[3538.Merge-Operations-for-Minimum-Travel-Time](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/3538.Merge-Operations-for-Minimum-Travel-Time) (H)
* ``maximum subarray``
[053.Maximum-Subarray](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/053.Maximum-Subarray) (E+)
[152.Maximum-Product-Subarray](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/152.Maximum-Product-Subarray) (M+)
Expand Down