Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2025
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. int longestMonotonicSubarray(vector<int>& nums) {
  2. int ans = 1;
  3.  
  4. for(int i = 0; i < nums.size();)
  5. {
  6. bool flag = true;
  7. int j;
  8.  
  9. for(j = i + 1; j < nums.size(); j++)
  10. {
  11. if(nums[j] <= nums[j - 1])
  12. {
  13. ans = max(ans, (j - i));
  14. flag = false;
  15. break;
  16. }
  17. }
  18.  
  19. if(flag) ans = max(ans, (j - i));
  20. i = j;
  21. }
  22.  
  23. for(int i = 0; i < nums.size();)
  24. {
  25. bool flag = true;
  26. int j;
  27.  
  28. for(j = i + 1; j < nums.size(); j++)
  29. {
  30. if(nums[j] >= nums[j - 1])
  31. {
  32. ans = max(ans, (j - i));
  33. flag = false;
  34. break;
  35. }
  36. }
  37.  
  38. if(flag) ans = max(ans, (j - i));
  39. i = j;
  40. }
  41.  
  42. return ans;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement