leetcode

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

2593.cpp (723B)


      1 class Solution {
      2   public:
      3     long long findScore(const vector<int> &nums) const {
      4         static uint8_t marked[500002];
      5         static int indices[500002];
      6 
      7         const int n = size(nums);
      8         long long res = 0;
      9 
     10         memset(marked, 0x00, sizeof(marked));
     11         iota(begin(indices), begin(indices) + n, 0);
     12         sort(begin(indices), begin(indices) + n,
     13              [&nums](int a, int b) { return nums[a] != nums[b] ? nums[a] < nums[b] : a < b; });
     14 
     15         for (int i = 0; i < n; i++) {
     16             const int idx = indices[i];
     17             if (marked[idx + 1]) continue;
     18             marked[idx] = marked[idx + 1] = marked[idx + 2] = 1;
     19             res += nums[idx];
     20         }
     21 
     22         return res;
     23     }
     24 };