Skip to content

Commit 410c3a4

Browse files
committed
[main] Added DP solution for longest increasing path in matrix and permutation solution as well
1 parent af80f46 commit 410c3a4

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from __future__ import annotations
2+
from typing import List, Optional
3+
4+
5+
class Solution:
6+
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
7+
base_len = len(matrix)
8+
col_len = len(matrix[0])
9+
range_base = range(base_len)
10+
range_col = range(col_len)
11+
longest_paths = [[1] * col_len for x in range_base]
12+
for i in range(len(matrix)):
13+
for j in range(len(matrix[0])):
14+
self.find_longest_path(matrix, longest_paths, i, j)
15+
max_len = 1
16+
for each_row in longest_paths:
17+
max_len = max(max_len, max(each_row))
18+
return max_len
19+
20+
def find_longest_path(
21+
self,
22+
matrix: List[List[int]],
23+
dfs_table: List[List[int]],
24+
row: int,
25+
col: int,
26+
current_length: int = 1,
27+
) -> None:
28+
if row != -1 and col != -1 and row < len(matrix) and col < len(matrix[0]):
29+
current_element = matrix[row][col]
30+
left_element = matrix[row][col - 1] if col > 0 else 1
31+
right_element = matrix[row][col + 1] if col < len(matrix[0]) - 1 else 1
32+
bottom_element = matrix[row + 1][col] if row < len(matrix) - 1 else 1
33+
top_element = matrix[row - 1][col] if row > 0 else 1
34+
35+
if dfs_table[row][col] != 1:
36+
return
37+
38+
if top_element > current_element:
39+
self.find_longest_path(
40+
matrix, dfs_table, row - 1, col, current_length + 1
41+
) # top
42+
if bottom_element > current_element:
43+
self.find_longest_path(
44+
matrix, dfs_table, row + 1, col, current_length + 1
45+
)
46+
if right_element > current_element:
47+
self.find_longest_path(
48+
matrix, dfs_table, row, col + 1, current_length + 1
49+
)
50+
if left_element > current_element:
51+
self.find_longest_path(
52+
matrix, dfs_table, row, col - 1, current_length + 1
53+
)
54+
55+
dfs_left = (
56+
dfs_table[row][col - 1]
57+
if col > 0 and matrix[row][col - 1] > matrix[row][col]
58+
else 0
59+
)
60+
dfs_right = (
61+
dfs_table[row][col + 1]
62+
if col < len(matrix[0]) - 1 and matrix[row][col + 1] > matrix[row][col]
63+
else 0
64+
)
65+
dfs_bottom = (
66+
dfs_table[row + 1][col]
67+
if row < len(matrix) - 1 and matrix[row + 1][col] > matrix[row][col]
68+
else 0
69+
)
70+
dfs_top = (
71+
dfs_table[row - 1][col]
72+
if row > 0 and matrix[row - 1][col] > matrix[row][col]
73+
else 0
74+
)
75+
dfs_table[row][col] = (
76+
max(dfs_left, max(dfs_right, max(dfs_bottom, dfs_top))) + 1
77+
)
78+
79+
80+
if __name__ == "__main__":
81+
sol = Solution()
82+
a = [[9, 9, 4], [6, 6, 8], [2, 1, 1]]
83+
b = [[3, 4, 5], [3, 2, 6], [2, 2, 1]]
84+
c = [[1]]
85+
print(sol.longestIncreasingPath(a))
86+
print(sol.longestIncreasingPath(b))
87+
print(sol.longestIncreasingPath(c))

pythonProblems/permute/permute.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def permute(self, nums: List[int]) -> List[List[int]]:
6+
permutations: List[int] = []
7+
self.dfs(nums, len(nums), permutations)
8+
return permutations
9+
10+
def dfs(
11+
self,
12+
nums: List[int],
13+
permute_length: int,
14+
permutations: List[List[int]],
15+
current_permutation: List[int] = [],
16+
) -> None:
17+
for index, each_number in enumerate(nums):
18+
# Choice
19+
new_permutation = current_permutation[:] + [each_number]
20+
self.dfs(
21+
nums[:index] + nums[index + 1 :],
22+
permute_length,
23+
permutations,
24+
new_permutation,
25+
)
26+
if (
27+
len(current_permutation) == permute_length
28+
and current_permutation not in permutations
29+
):
30+
permutations.append(current_permutation)
31+
32+
33+
if __name__ == "__main__":
34+
sol = Solution()
35+
nums = [1, 2, 3]
36+
print(sol.permute(nums))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def solveSudoku(self, board: List[List[str]]) -> None:
3+
return None

0 commit comments

Comments
 (0)