Given a boolean 2D array, where each row is sorted. Find the row with the maximum number of 1s.
Examples:
Input:
matrix =
[[0, 1, 1, 1],
[0, 0, 1, 1],
[1, 1, 1, 1],
[0, 0, 0, 0]]
Output: 2
We have existing solution for this problem please refer Find the row with maximum number of 1’s. We can solve this problem in python quickly using map() function. Approach is very simple, find sum of all 1’s in each row and then print index of maximum sum in a list because row having maximum 1 will also have maximum sum.
Implementation:
Python3
def maxOnes(input):
result = list(map(sum,input))
print (result.index(max(result)))
if __name__ == "__main__":
input = [[0, 1, 1, 1],[0, 0, 1, 1],[1, 1, 1, 1],[0, 0, 0, 0]]
maxOnes(input)
|
Complexity : O(M*N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
28 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...