Skip to content

Commit 4d8ac27

Browse files
committed
Add tasks 15 materials
1 parent 1f8c174 commit 4d8ac27

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

Seminars/sem_15/README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,9 @@
5757
- Хамилтоновият цикъл е път, който минава всеки един **връх** на даден на даден граф само по един път, като свършва във върха, от който е тръгнал.
5858
- Проблемът за намирането на минимален Хамилтонов цикъл е известен като *The Traveling Salesman Problem* (*NP hard problem*).
5959

60-
Пример:
61-
6260
![Hamiltonian path and cycle example](media/hamiltonian_path_and_cycle.png)
6361

64-
- Хамилтонов път - *ABCED*
65-
- Хамилтонов цикъл - *ABDECA*
62+
Пример за Хамилтонов път - *ABCED*, за Хамилтонов цикъл - *ABDECA*.
6663

6764
## P, NP, NP complete, NP hard проблеми
6865

@@ -101,7 +98,8 @@
10198

10299
## Задачи за упражнение
103100

101+
- [Permutations](https://leetcode.com/problems/permutations)
102+
- [All Paths From Source to Target](https://leetcode.com/problems/all-paths-from-source-to-target)
104103
- [Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary)
105104

106-
107105
Решения на задачите: [тук](/Tasks/tasks_15)

Tasks/tasks_15/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Условия:
2+
3+
- [Permutations](https://leetcode.com/problems/permutations)
4+
- [All Paths From Source to Target](https://leetcode.com/problems/all-paths-from-source-to-target)
5+
- [Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def dfs(self, current_node, end_node, path, visited, all_paths, graph):
3+
for neigh in graph[current_node]:
4+
if neigh in visited:
5+
continue
6+
7+
if neigh == end_node:
8+
all_paths.append(path + [end_node])
9+
continue
10+
11+
path.append(neigh)
12+
visited.add(neigh)
13+
14+
self.dfs(neigh, end_node, path, visited, all_paths, graph)
15+
path.pop()
16+
visited.remove(neigh)
17+
18+
return all_paths
19+
20+
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
21+
N = len(graph)
22+
23+
start_node = 0
24+
end_node = N - 1
25+
26+
all_paths = []
27+
path = [start_node]
28+
visited = set([start_node])
29+
30+
return self.dfs(start_node, end_node, path, visited, all_paths, graph)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def dfs(self, path, all_paths, nums, used):
3+
if len(path) == len(nums):
4+
all_paths.append(path.copy())
5+
return all_paths
6+
7+
for num in nums:
8+
if num in used:
9+
continue
10+
path.append(num)
11+
used.add(num)
12+
13+
self.dfs(path, all_paths, nums, used)
14+
15+
path.pop()
16+
used.remove(num)
17+
18+
return all_paths
19+
20+
def permute(self, nums: List[int]) -> List[List[int]]:
21+
all_paths = []
22+
path = []
23+
used = set()
24+
25+
return self.dfs(path, all_paths, nums, used)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from itertools import permutations
2+
3+
class Solution:
4+
def permute(self, nums: List[int]) -> List[List[int]]:
5+
return permutations(nums)

0 commit comments

Comments
 (0)