Anyone could help with my divide and conquer solution?


  • 0
    J

    I wrote the program with the divide and conquer method.The OJ said that my program had a compile error called required from here.I don't know what kind of error it is.Is anyone willing to help me?

    class Solution {
    public:
        int maxSubArray(vector<int>& nums) {
            int nz=nums.size();
            if(nz==0) return 0;
            return findmax(0,nz-1,nums);
        }
        int findmax(int l,int r,vector<int>& nums){
            //if(l>r) return 0;
            if(l==r) return max(0,nums[l]);
            int m=(l+r)/2;
            int lmax,lsum;
            lmax=lsum=0;
            for(int i=m;i>=l;i--){
                lsum=lsum+nums[i];
                lmax=max(lsum,lmax);
            }
            int rmax,rsum;
            rmax=rsum=0;
            for(int i=m+1;i<=r;i++){
                rsum=rsum+nums[i];
                rmax=max(rsum,rmax);
            }
            return max(lmax+rmax,findmax(l,m,nums),findmax(m+1,r,nums));
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.