-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathnumber_of_ways.py
More file actions
63 lines (50 loc) · 1.81 KB
/
number_of_ways.py
File metadata and controls
63 lines (50 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from tqdm import tqdm
def numberOfWays(startPos: int, endPos: int, k: int) -> int:
"""
Solving Leetcode Problem.
https://leetcode.com/problems/number-of-ways-to-reach-a-position-after-exactly-k-steps/
Given two positive integers startPos and endPos
Initially, you are standing at position startPos on an infinite
number line. With one step, you can move either one position to the left,
or one position to the right.
Given a positive integer k, return the number of different ways to
reach the position endPos starting from startPos, such that you
perform exactly k steps.
"""
# start with path of length 1
paths = [startPos]
# loop k times
for i in tqdm(range(k)):
for path in paths:
new_path = path.copy()
last_position = new_path[-1]
# exist fast if not going to make to end
if endPos - last_position > (k - i - 1):
continue
# path that goes to the left
new_path_left = new_path + [last_position - 1]
# path that goes to the right
new_path_right = new_path + [last_position - 1]
# add paths to the left and right
paths.append(new_path_left)
paths.append(new_path_right)
num_ways = 0
for path in paths:
if path[-1] == endPos:
new_ways += 1
return num_ways
def test_number_of_ways():
"""
Example 1:
Input: startPos = 1, endPos = 2, k = 3
Output: 3
Explanation: We can reach position 2 from 1 in exactly 3 steps
in three ways:
- 1 -> 2 -> 3 -> 2.
- 1 -> 2 -> 1 -> 2.
- 1 -> 0 -> 1 -> 2.
It can be proven that no other way is possible, so we return 3.
"""
print(numberOfWays(1, 2, 3))
if __name__ == "__main__":
test_number_of_ways()