Skip to content

Commit 4b2a88f

Browse files
authored
Create CountNumberOfWaysToCoverADistance.java
1 parent 20b6553 commit 4b2a88f

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
https://www.geeksforgeeks.org/count-number-of-ways-to-cover-a-distance/
3+
https://www.geeksforgeeks.org/count-number-of-ways-to-cover-a-distance-set-2/
4+
5+
Given a distance 'dist', count total number of ways to cover the distance with 1, 2 and 3 steps.
6+
7+
Examples :
8+
9+
Input: n = 3
10+
Output: 4
11+
Below are the four ways
12+
1 step + 1 step + 1 step
13+
1 step + 2 step
14+
2 step + 1 step
15+
3 step
16+
17+
Input: n = 4
18+
Output: 7
19+
20+
In the given problem we have 3 choices to cover EACH STEP of a distance X
21+
1. Take a step of length = 1, remaining dist = X-1 ==> recur on (X-1)
22+
2. Take a step of length = 2, remaining dist = X-2 ==> recur on (X-2)
23+
3. Take a step of length = 3, remaining dist = X-3 ==> recur on (X-3)
24+
*/
25+
26+
public int countPaths(int dist){
27+
28+
//if (dist == 0) then the steps taken are valid return 1 to increment the number of valid paths
29+
if(dist == 0) return 1;
30+
31+
/*
32+
if (dist < 0) then the steps taken are invalid and don't sum to the original distance. This path will not contribute to the
33+
total number of valid paths
34+
*/
35+
else if(dist<0) return 0;
36+
37+
//explore the 3 choices to find all possible paths
38+
else return (countPaths(dist-1) + countPaths(dist-2) + countPaths(dist-3));
39+
}
40+
41+
/*
42+
Time Complexity - O(3^distance)
43+
For each value of distance we explore all the 3 choices to find all possible paths
44+
1. distance-1
45+
2. distance-2
46+
3. distance-3
47+
*/
48+
49+
/*
50+
DYNAMIC PROGRAMMING - BOTTOM UP APPROACH
51+
The main problem can be divided into subproblems through recursion.
52+
These subproblems are overlapping ==> we can apply dynamic programming
53+
*/
54+
55+
public int countPaths(int dist){
56+
//paths[i] indicates the number of possible ways to reach the distance 'i', given we are allowed to take steps
57+
//of length 1, 2 and 3.
58+
int paths[] = new int[dist+1];
59+
60+
//Base Cases: becuase each subproblem depends on three smaller subproblems
61+
paths[0] = 1;
62+
paths[1] = 1;
63+
paths[2] = 2;
64+
65+
for(int d=3 ; d<=dist ; d++){
66+
//solving subproblem paths[d] with smaller subproblems
67+
paths[d] = paths[d-1] + paths[d-2] + paths[d-3];
68+
}
69+
70+
//final ans - total number of paths possible
71+
return paths[dist];
72+
}
73+
74+
/*
75+
Time Complexity & Space Complexity - O(distance)
76+
*/
77+
78+
79+
/*
80+
DYNAMIC PROGRAMMING - SPACE OPTIMIZED
81+
to calculate the number of steps to cover the distance i, only the last three states are required (i – 1, i – 2, i – 3).
82+
So, keep track of last 3 states only.
83+
*/
84+
85+
public int countPaths(int dist){
86+
if(dist==0 || dist==1) return 1;
87+
else if( dist==2 ) return 2;
88+
89+
int subProb0 = 1;
90+
int subProb1 = 1;
91+
int subProb2 = 2;
92+
int numOfPaths = 0;
93+
94+
for(int d=3;d<=dist;d++){
95+
numOfPaths = subProb0 + subProb1 + subProb3;
96+
subProb0 = subProb1;
97+
subProb1 = subProb2;
98+
subProb3 = numOfPaths;
99+
}
100+
101+
return numOfPaths;
102+
103+
}
104+
105+
/*
106+
Time Complexity - O(distance)
107+
Space Complexity - O(1)
108+
*/

0 commit comments

Comments
 (0)