We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- The Maximum Subarray
- Discussions
The Maximum Subarray
The Maximum Subarray
Sort by
recency
|
18 Discussions
|
Please Login in order to post a comment
Pythonic 2 lines:
Python
C#
JS
EASY C++ SOLUTION
include
using namespace std;
string ltrim(const string &); string rtrim(const string &); vector split(const string &);
/* * Complete the 'maxSubarray' function below. * * The function is expected to return an INTEGER_ARRAY. * The function accepts INTEGER_ARRAY arr as parameter. */
vector maxSubarray(vector arr) { vector result(2); // Create a vector of array with size 2 //maxSubarray Sum int maxSum = INT32_MIN;//assgining a minimum value to the maxSum int currSum = 0; for(int i=0;i<=arr.size()-1;i++){ currSum += arr[i]; if (currSum > maxSum){ maxSum = currSum; } if(currSum < 0){ currSum = 0; } } //-------------- //maxSubsequence Sum int sum = 0; for (int i = 0; i < arr.size(); ++i) { int num = arr[i]; if (num > 0) { sum += num; } } if (sum == 0) { sum = *std::max_element(arr.begin(), arr.end()); }
}
int main() { ofstream fout(getenv("OUTPUT_PATH"));
}
string ltrim(const string &str) { string s(str);
}
string rtrim(const string &str) { string s(str);
}
vector split(const string &str) { vector tokens;
}