Record two similar upgraded versions (it seems to be a Huawei question)

One, the largest field sum -----> the largest sub-matrix sum

1.1 leetcode53. Maximum suborder sum

The largest continuous sub-segment sum. f[i] means that it must end with the i-th number, which means that this number will definitely be added, so it depends on whether to add the part before the number. Add more than zero, discard less than zero. Classic dynamic programming, here I use a single variable to achieve it. Time complexity O(n).

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        if (nums.size()==0) return 0;
        int former=0;
        int ans=-0x3f3f3f3f;
        for (int i=0;i<nums.size();++i)
        {
            if (former>0) former+=nums[i];//这边也就是说必须以nums[i]为结尾的最大价值。
            else former=nums[i];
            if (former>ans) ans=former;
        }
        return ans;
    }
};

1.2 Lanqiao Cup-Maximum Submatrix Sum

Given a matrix, find the maximum sub-matrix sum, the algorithm is to compress multiple rows into one row, enumerate from row i to row j (i<=j), compress these rows into one row, and then O(m) find the largest sub-matrix Segment sum, the total time complexity is O(n*n*m)

Guess you like

Origin blog.csdn.net/hbhhhxs/article/details/108118225