leetcode

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

1943.cpp (778B)


      1 class Solution {
      2   public:
      3     vector<vector<long long>> splitPainting(const vector<vector<int>> &segments) const {
      4         static long long prefix[100002];
      5         static bool seen[100002];
      6 
      7         memset(seen, 0x00, sizeof(seen));
      8         memset(prefix, 0x00, sizeof(prefix));
      9         for (const auto seg : segments) {
     10             prefix[seg[0]] += seg[2];
     11             prefix[seg[1]] -= seg[2];
     12 
     13             seen[seg[0]] = seen[seg[1]] = 1;
     14         }
     15 
     16         vector<vector<long long>> res;
     17         long long crnt = 0, prev = 0;
     18         for (int i = 1; i < 100002; i++) {
     19             if (crnt && seen[i]) {
     20                 res.push_back({prev, i, crnt});
     21             }
     22             if (seen[i]) prev = i;
     23             crnt += prefix[i];
     24         }
     25 
     26         return res;
     27     }
     28 };