-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElevatorSteps.java
More file actions
44 lines (37 loc) · 1.34 KB
/
ElevatorSteps.java
File metadata and controls
44 lines (37 loc) · 1.34 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
package org.complex;
import java.util.ArrayList;
import java.util.List;
public class ElevatorSteps {
public static void main(String[] args) {
int n = 7; // Example step number
List<Integer> steps = new ArrayList<>();
findPaths(0, n, steps);
}
public static void findPaths(int currentStep, int targetStep, List<Integer> steps) {
if (currentStep == targetStep) {
System.out.println(steps);
return;
}
if (currentStep > targetStep) {
return;
}
int lastStepSize = steps.isEmpty() ? 0 : steps.get(steps.size() - 1);
// Try starting with 1 or 2 steps initially
if (lastStepSize == 0) {
for (int i = 1; i <= 2; i++) {
steps.add(i);
findPaths(currentStep + i, targetStep, steps);
// System.out.println("Executing If condition" + steps);
steps.remove(steps.size() - 1);
}
} else {
// Continue with the current step pattern
for (int i = lastStepSize; i <= lastStepSize + 2; i++) {
steps.add(i);
findPaths(currentStep + i, targetStep, steps);
// System.out.println("Executing Else condition" + steps);
steps.remove(steps.size() - 1);
}
}
}
}