2643 一最多的行

·   ·   ·   ·

  ·   ·


题目链接

2643. 一最多的行

分析

枚举每一行,统计 $1$ 的个数,当前行的 $1$ 数量大于之前的最大数量时,更新答案

代码实现

class Solution {
public:
    vector<int> rowAndMaximumOnes(vector<vector<int>>& mat) {
        int ans = 0, maxx = 0;

        for(int i = 0; i < mat.size(); ++i) {
            int t = accumulate(mat[i].begin(), mat[i].end(), 0);
            if(maxx < t) {
                maxx = t;
                ans = i;
            }
        }
  
        return {ans, maxx};
    }
};

复杂度分析

  • 时间复杂度:$O(nm)$,其中 $n,m$ 分别为数组的长宽
  • 空间复杂度:$O(1)$