leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2497.cpp (758B)


      1 class Solution {
      2   public:
      3     int maxStarSum(vector<int> &vals, vector<vector<int>> &edges, int k) {
      4         vector<vector<int>> adj(vals.size());
      5 
      6         for (auto &e : edges) {
      7             adj[e[0]].push_back(e[1]);
      8             adj[e[1]].push_back(e[0]);
      9         }
     10 
     11         int res = INT_MIN;
     12         for (int i = 0; i < vals.size(); i++) {
     13             priority_queue<int, vector<int>, greater<int>> pq;
     14             for (int c : adj[i]) {
     15                 if (vals[c] <= 0) continue;
     16                 pq.push(vals[c]);
     17                 if (pq.size() > k) pq.pop();
     18             }
     19             int sum = vals[i];
     20             while (!pq.empty())
     21                 sum += pq.top(), pq.pop();
     22             res = max(res, sum);
     23         }
     24         return res;
     25     }
     26 };