leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2593.cpp (723B)
0 class Solution { 1 public: 2 long long findScore(const vector<int> &nums) const { 3 static uint8_t marked[500002]; 4 static int indices[500002]; 5 6 const int n = size(nums); 7 long long res = 0; 8 9 memset(marked, 0x00, sizeof(marked)); 10 iota(begin(indices), begin(indices) + n, 0); 11 sort(begin(indices), begin(indices) + n, 12 [&nums](int a, int b) { return nums[a] != nums[b] ? nums[a] < nums[b] : a < b; }); 13 14 for (int i = 0; i < n; i++) { 15 const int idx = indices[i]; 16 if (marked[idx + 1]) continue; 17 marked[idx] = marked[idx + 1] = marked[idx + 2] = 1; 18 res += nums[idx]; 19 } 20 21 return res; 22 } 23 };