{"id":9999,"date":"2023-04-28T18:28:50","date_gmt":"2023-04-29T01:28:50","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9999"},"modified":"2023-04-28T18:29:51","modified_gmt":"2023-04-29T01:29:51","slug":"leetcode-2643-row-with-maximum-ones","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2643-row-with-maximum-ones\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2643. Row With Maximum Ones"},"content":{"rendered":"\n<p>Given a&nbsp;<code>m x n<\/code>&nbsp;binary matrix&nbsp;<code>mat<\/code>, find the&nbsp;<strong>0-indexed<\/strong>&nbsp;position of the row that contains the&nbsp;<strong>maximum<\/strong>&nbsp;count of&nbsp;<strong>ones,<\/strong>&nbsp;and the number of ones in that row.<\/p>\n\n\n\n<p>In case there are multiple rows that have the maximum count of ones, the row with the&nbsp;<strong>smallest row number<\/strong>&nbsp;should be selected.<\/p>\n\n\n\n<p>Return<em>&nbsp;an array containing the index of the row, and the number of ones in it.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[0,1],[1,0]]\n<strong>Output:<\/strong> [0,1]\n<strong>Explanation:<\/strong> Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1<code>)<\/code>. So, the answer is [0,1]. \n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[0,0,0],[0,1,1]]\n<strong>Output:<\/strong> [1,2]\n<strong>Explanation:<\/strong> The row indexed 1 has the maximum count of ones <code>(2)<\/code>. So we return its index, <code>1<\/code>, and the count. So, the answer is [1,2].\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[0,0],[1,1],[0,0]]\n<strong>Output:<\/strong> [1,2]\n<strong>Explanation:<\/strong> The row indexed 1 has the maximum count of ones (2). So the answer is [1,2].\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == mat.length<\/code>&nbsp;<\/li><li><code>n == mat[i].length<\/code>&nbsp;<\/li><li><code>1 &lt;= m, n &lt;= 100<\/code>&nbsp;<\/li><li><code>mat[i][j]<\/code>&nbsp;is either&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Counting<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(m*n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> rowAndMaximumOnes(vector<vector<int>>& mat) {\n    vector<int> ans(2);\n    for (int i = 0; i < mat.size(); ++i) {\n      int ones = accumulate(begin(mat[i]), end(mat[i]), 0);\n      if (ones > ans[1]) {\n        ans[0] = i;\n        ans[1] = ones;\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;m x n&nbsp;binary matrix&nbsp;mat, find the&nbsp;0-indexed&nbsp;position of the row that contains the&nbsp;maximum&nbsp;count of&nbsp;ones,&nbsp;and the number of ones in that row. In case there are&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,216],"class_list":["post-9999","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-matrix","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9999"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9999\/revisions"}],"predecessor-version":[{"id":10001,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9999\/revisions\/10001"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}