An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
February 22, 2022 04:43 pm GMT
Original Link: https://dev.to/thivyaamohan/maximum-subarray-leetcode-solution-60f
Maximum Subarray - Leetcode Solution
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
A subarray is a contiguous part of an array.
We need to use Kadane's Algorithm here
//Kadane's Algorithmclass Solution {public: int maxSubArray(vector<int>& nums) { int ans =INT_MIN,sum =0; int n=nums.size(); for(int i=0;i<n;++i){ sum+=nums[i]; ans = max(ans,sum); if(sum<0) sum=0; } return ans; } };
Original Link: https://dev.to/thivyaamohan/maximum-subarray-leetcode-solution-60f
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To