Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
1.1 kB
19
Indexable
Never
#include <algorithm>
#include <numeric>

long optimizeTiktokWatchTime(int m, vector<int> initialWatch, vector<int> repeatWatch) {
    int n = initialWatch.size();
    vector<pair<int, int>> reels(n);
    
    // Create a vector of pairs where each pair contains (repeatWatch[i], initial total time for the i-th reel)
    for (int i = 0; i < n; ++i) {
        reels[i] = {repeatWatch[i], initialWatch[i] + repeatWatch[i]};
    }
    
    // Sort by repeatWatch[i] since we want to prioritize reels with the smallest rewatch times.
    sort(reels.begin(), reels.end());
    
    // Calculate the initial watch time
    long totalTime = 0;
    for (int i = 0; i < n; ++i) {
        totalTime += reels[i].second; // initial total time = initialWatch[i] + repeatWatch[i]
    }
    
    // We've already watched all n reels once, so we need m - n additional rewatchings
    int remainingRewatches = m - n;
    
    // Minimize time by rewatching reels with the smallest repeatWatch time
    for (int i = 0; i < remainingRewatches; ++i) {
        totalTime += reels[i % n].first; // rewatch time = repeatWatch[i]
    }
    
    return totalTime;
}
Leave a Comment