mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Add euler project problem 15 additional solution (#12774)
* Add euler project problem 15 additional solution by explicitly counting the paths. * Update sol2.py * updating DIRECTORY.md * updating DIRECTORY.md * Trigger CI * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru> Co-authored-by: MaximSmolskiy <MaximSmolskiy@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -469,6 +469,11 @@
|
||||
|
||||
## Geometry
|
||||
* [Geometry](geometry/geometry.py)
|
||||
* [Graham Scan](geometry/graham_scan.py)
|
||||
* [Jarvis March](geometry/jarvis_march.py)
|
||||
* Tests
|
||||
* [Test Graham Scan](geometry/tests/test_graham_scan.py)
|
||||
* [Test Jarvis March](geometry/tests/test_jarvis_march.py)
|
||||
|
||||
## Graphics
|
||||
* [Bezier Curve](graphics/bezier_curve.py)
|
||||
@@ -981,6 +986,7 @@
|
||||
* [Sol2](project_euler/problem_014/sol2.py)
|
||||
* Problem 015
|
||||
* [Sol1](project_euler/problem_015/sol1.py)
|
||||
* [Sol2](project_euler/problem_015/sol2.py)
|
||||
* Problem 016
|
||||
* [Sol1](project_euler/problem_016/sol1.py)
|
||||
* [Sol2](project_euler/problem_016/sol2.py)
|
||||
|
||||
32
project_euler/problem_015/sol2.py
Normal file
32
project_euler/problem_015/sol2.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""
|
||||
Problem 15: https://projecteuler.net/problem=15
|
||||
|
||||
Starting in the top left corner of a 2x2 grid, and only being able to move to
|
||||
the right and down, there are exactly 6 routes to the bottom right corner.
|
||||
How many such routes are there through a 20x20 grid?
|
||||
"""
|
||||
|
||||
|
||||
def solution(n: int = 20) -> int:
|
||||
"""
|
||||
Solve by explicitly counting the paths with dynamic programming.
|
||||
|
||||
>>> solution(6)
|
||||
924
|
||||
>>> solution(2)
|
||||
6
|
||||
>>> solution(1)
|
||||
2
|
||||
"""
|
||||
|
||||
counts = [[1 for _ in range(n + 1)] for _ in range(n + 1)]
|
||||
|
||||
for i in range(1, n + 1):
|
||||
for j in range(1, n + 1):
|
||||
counts[i][j] = counts[i - 1][j] + counts[i][j - 1]
|
||||
|
||||
return counts[n][n]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(solution())
|
||||
Reference in New Issue
Block a user