fork download
  1. #include <vector>
  2. #include <algorithm>
  3.  
  4. int variableSlidingWindow(const std::vector<int>& arr) {
  5. int left = 0;
  6. int bestAns = 0;
  7. int currentState = 0; // Could be an int, unordered_map, etc.
  8.  
  9. for (int right = 0; right < arr.size(); ++right) {
  10. // 1. ACQUIRE: Add arr[right] to the current state
  11. currentState += arr[right];
  12.  
  13. // 2. RELEASE (Shrink): While the window is INVALID, move left pointer
  14. // Note: You will replace `conditionIsInvalid` with actual logic
  15. while (/* conditionIsInvalid(currentState) */ false) {
  16. // Remove arr[left] from current state
  17. currentState -= arr[left];
  18. left++; // Shrink the window
  19. }
  20.  
  21. // 3. UPDATE: The window is now valid. Update the best answer.
  22. // Example for finding the max length:
  23. bestAns = std::max(bestAns, right - left + 1);
  24. }
  25.  
  26. return bestAns;
  27. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty