Skip to content

Commit 8ae93fb

Browse files
committed
Added code for Matrix problems in DSA-450 in Python , closes isssue -99
1 parent 77d8539 commit 8ae93fb

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

DSA 450 GFG/row_with_max_ones.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#https://practice.geeksforgeeks.org/problems/row-with-max-1s0023/1
2+
3+
4+
# 1 . Initialize the count of ones in a row as max_ones_row = -1
5+
# 2 . Initialize the max count of 1's as max_ones = 0
6+
# 3 . Iterate through each row and find the ones count
7+
# 4 . Compare the max_ones with max_ones_row in each row and store the updated count in max_ones_row
8+
9+
10+
11+
12+
class Solution:
13+
14+
def rowWithMax1s(self,arr, n, m):
15+
# code here
16+
j = m - 1
17+
max_ones = 0
18+
max_ones_row = -1
19+
for i in range(0 , n ):
20+
ones = self.countones(arr[i])
21+
if( ones > max_ones ):
22+
max_ones = ones
23+
max_ones_row = i
24+
25+
26+
return max_ones_row
27+
28+
def countones(self , a ):
29+
count = a.count(1)
30+
return count
31+
32+
#{
33+
# Driver Code Starts
34+
35+
36+
if __name__ == '__main__':
37+
tc = int(input())
38+
while tc > 0:
39+
n, m = list(map(int, input().strip().split()))
40+
41+
inputLine = list(map(int, input().strip().split()))
42+
arr = [[0 for j in range(m)] for i in range(n)]
43+
44+
for i in range(n):
45+
for j in range(m):
46+
arr[i][j] = inputLine[i * m + j]
47+
ob = Solution()
48+
ans = ob.rowWithMax1s(arr, n, m)
49+
print(ans)
50+
tc -= 1
51+
52+
# } Driver Code Ends
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Question : https://leetcode.com/problems/search-a-2d-matrix/
2+
3+
# Search an element in array
4+
#Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
5+
6+
# Integers in each row are sorted from left to right.
7+
# The first integer of each row is greater than the last integer of the previous row.
8+
9+
# Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
10+
# Output: true
11+
12+
#1. Apply Binary Search on every row
13+
# Iterate through every row and consider each row as an array and apply binary Search
14+
15+
class Solution:
16+
def searchMatrix(self, matrix, target):
17+
18+
output = False
19+
if len(matrix) == 1 and len(matrix[0]) == 1:
20+
if target == matrix[0][0]:return True
21+
else:return False
22+
23+
j = len(matrix[0]) - 1
24+
for i in range(len(matrix)):
25+
if target == matrix[i][j]:
26+
return True
27+
elif target < matrix[i][j]:
28+
output = self.search(matrix[i], target)
29+
break
30+
return output
31+
32+
def search(self, arr, target):
33+
beg_index = 0
34+
end_index = len(arr) - 1
35+
while beg_index <= end_index :
36+
mid = (beg_index + end_index ) // 2
37+
if arr[mid] == target:
38+
return True
39+
elif target < arr[mid]:
40+
end_index = mid - 1
41+
else:
42+
beg_index = mid + 1
43+
return False

0 commit comments

Comments
 (0)