knakul853

Untitled

Jul 11th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. /**
  2. knakul853
  3. **/
  4. class Solution {
  5. public:
  6.     int maxSubArray(vector<int>& nums) {
  7.         int n = nums.size();
  8.         if(n==0)return 0;
  9.      
  10.         int best =  INT_MIN;
  11.         int cur = 0;
  12.        
  13.         for(const int& num :  nums )
  14.         {
  15.             cur = max( cur, 0 );
  16.             cur = max(num, cur +num ); // subarray might start from here or continue with previous.
  17.             best = max(best, cur);
  18.         }
  19.        
  20.         return best;
  21.     }
  22. };
Add Comment
Please, Sign In to add comment