Problem solution · C++

Sum of Total Strength of Wizards

Sum of Total Strength of Wizards: a C++ solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sliding window or two pointers
Source
walkccc LeetCode Solutions
Length
55 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For Sum of Total Strength of Wizards, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 55 lines of C++ from the credited upstream file 2281.cpp.
  • The implementation visibly relies on sequence storage.
  • 7 loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeSum of Total Strength of Wizards · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  int totalStrength(vector<int>& strength) {    constexpr int kMod = 1'000'000'007;    const int n = strength.size();    vector<long> prefix(n);    vector<long> prefixOfPrefix(n + 1);    // left[i] := the next index on the left (if any) s.t.    // nums[left[i]] <= nums[i]    vector<int> left(n, -1);    // right[i] := the next index on the right (if any) s.t.    // nums[right[i]] < nums[i]    vector<int> right(n, n);    stack<int> stack;     for (int i = 0; i < n; ++i)      prefix[i] = i == 0 ? strength[0] : (strength[i] + prefix[i - 1]) % kMod;     for (int i = 0; i < n; ++i)      prefixOfPrefix[i + 1] = (prefixOfPrefix[i] + prefix[i]) % kMod;     for (int i = n - 1; i >= 0; --i) {      while (!stack.empty() && strength[stack.top()] >= strength[i])        left[stack.top()] = i, stack.pop();      stack.push(i);    }     stack = std::stack<int>();     for (int i = 0; i < n; ++i) {      while (!stack.empty() && strength[stack.top()] > strength[i])        right[stack.top()] = i, stack.pop();      stack.push(i);    }     long ans = 0;     // For each strength[i] as minimum, calculate sum.    for (int i = 0; i < n; ++i) {      const int l = left[i];      const int r = right[i];      const long leftSum = prefixOfPrefix[i] - prefixOfPrefix[max(0, l)];      const long rightSum = prefixOfPrefix[r] - prefixOfPrefix[i];      const int leftLen = i - l;      const int rightLen = r - i;      ans += strength[i] *             (rightSum * leftLen % kMod - leftSum * rightLen % kMod + kMod) %             kMod;      ans %= kMod;    }     return ans;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗