problem:https://leetcode.com/problems/video-stitching/
背包类型问题。可将长度视为背包容量,物体视为长条状的物体。
class Solution { public: int videoStitching(vector<vector<int>>& clips, int T) { sort(clips.begin(), clips.end(), cmp); vector<int> dp(T + 1, -1); dp[0] = 0; for (int i = 0; i < clips.size(); i++) { int begin = clips[i][0]; if (begin > T) break; int end = min(clips[i][1], T); if (dp[begin] == -1) continue; for (int t = begin + 1; t <= end; t++) { if (dp[t] == -1) dp[t] = dp[begin] + 1; else dp[t] = min(dp[t], dp[begin] + 1); } } return dp[T]; } };