Skip to content
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

774关于priority_pueue的优化 #53

Closed
xymabinogi opened this issue Aug 17, 2022 · 2 comments
Closed

774关于priority_pueue的优化 #53

xymabinogi opened this issue Aug 17, 2022 · 2 comments

Comments

@xymabinogi
Copy link
Contributor

LC-774 priority_queue
顺着您的思路做了一些改进(不会TLE了):先找到整个数据minmax的upper bound,然后在第一次遇到某个pair时,不要减去1,直接减到低于这个upper bound为止。

Example (借用您的代码)

class Solution {
public:
    double minmaxGasDist(vector<int>& stations, int K) 
    {
        priority_queue<pair<double,int>>pq;
        for (int i=1; i<stations.size(); i++)
            pq.push({stations[i]-stations[i-1],1});
        
        double ub = (stations.back() - stations[0]) / (double) (K + 1);
            
        while (K > 0)
        {
            auto [space, insertNum] = pq.top();
            pq.pop();
            int add = 1;
            if (insertNum == 1)
                add = max(1, int(ceil(space/ub) - 1));
            K -= add;
            pq.push({space*insertNum/(insertNum+add),insertNum+add});            
        }
        
        return pq.top().first;
    }
};
@wisdompeak
Copy link
Owner

非常感谢!这个思路非常棒,已经在Github更新了,关于这题的视频也重做了一期。

@xymabinogi
Copy link
Contributor Author

感谢群主!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants