Skip to content

Commit 14f8cf4

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 03adefc commit 14f8cf4

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

searches/Peak_Element_on_2D.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
def find_peak_util(matrix, left, right, row_count):
22
mid_col = (left + right) // 2
3-
3+
44
max_row_index = 0
55
for i in range(row_count):
66
if matrix[i][mid_col] > matrix[max_row_index][mid_col]:
77
max_row_index = i
8-
98

10-
if (mid_col == 0 or matrix[max_row_index][mid_col] >= matrix[max_row_index][mid_col - 1]) and \
11-
(mid_col == len(matrix[0]) - 1 or matrix[max_row_index][mid_col] >= matrix[max_row_index][mid_col + 1]):
9+
if (
10+
mid_col == 0
11+
or matrix[max_row_index][mid_col] >= matrix[max_row_index][mid_col - 1]
12+
) and (
13+
mid_col == len(matrix[0]) - 1
14+
or matrix[max_row_index][mid_col] >= matrix[max_row_index][mid_col + 1]
15+
):
1216
return matrix[max_row_index][mid_col]
13-
1417

15-
if mid_col > 0 and matrix[max_row_index][mid_col - 1] > matrix[max_row_index][mid_col]:
18+
if (
19+
mid_col > 0
20+
and matrix[max_row_index][mid_col - 1] > matrix[max_row_index][mid_col]
21+
):
1622
return find_peak_util(matrix, left, mid_col - 1, row_count)
1723

1824
return find_peak_util(matrix, mid_col + 1, right, row_count)
1925

26+
2027
def find_peak(matrix):
2128
if not matrix or not matrix[0]:
2229
return None
2330
return find_peak_util(matrix, 0, len(matrix[0]) - 1, len(matrix))
2431

25-
matrix = [
26-
[10, 8, 10, 10],
27-
[14, 13, 12, 11],
28-
[15, 9, 11, 21],
29-
[16, 17, 19, 20]
30-
]
32+
33+
matrix = [[10, 8, 10, 10], [14, 13, 12, 11], [15, 9, 11, 21], [16, 17, 19, 20]]
3134

3235
peak = find_peak(matrix)
3336
print(f"Peak element is: {peak}")

0 commit comments

Comments
 (0)