├── .gitignore
├── .idea
├── .gitignore
├── misc.xml
└── modules.xml
├── DirectionalAlgorothm.iml
└── src
└── Main.java
/.gitignore:
--------------------------------------------------------------------------------
1 | ### IntelliJ IDEA ###
2 | out/
3 | !**/src/main/**/out/
4 | !**/src/test/**/out/
5 |
6 | ### Eclipse ###
7 | .apt_generated
8 | .classpath
9 | .factorypath
10 | .project
11 | .settings
12 | .springBeans
13 | .sts4-cache
14 | bin/
15 | !**/src/main/**/bin/
16 | !**/src/test/**/bin/
17 |
18 | ### NetBeans ###
19 | /nbproject/private/
20 | /nbbuild/
21 | /dist/
22 | /nbdist/
23 | /.nb-gradle/
24 |
25 | ### VS Code ###
26 | .vscode/
27 |
28 | ### Mac OS ###
29 | .DS_Store
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/DirectionalAlgorothm.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/Main.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class Main {
4 | public static String[] dirReduc(String[] directions) {
5 | HashMap opposites = new HashMap<>();
6 | opposites.put("NORTH", "SOUTH");
7 | opposites.put("SOUTH", "NORTH");
8 | opposites.put("EAST", "WEST");
9 | opposites.put("WEST", "EAST");
10 |
11 | Stack stack = new Stack<>();
12 | for (String direction : directions) {
13 | if (!stack.isEmpty() && stack.peek().equals(opposites.get(direction))) {
14 | stack.pop();
15 | } else {
16 | stack.push(direction);
17 | }
18 | }
19 |
20 | return stack.toArray(new String[0]);
21 | }
22 |
23 | public static void main(String[] args) {
24 | String[] directions1 = {"NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"};
25 | String[] optimized1 = dirReduc(directions1);
26 | System.out.println(Arrays.toString(optimized1)); // Output: [WEST]
27 |
28 | String[] directions2 = {"NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH"};
29 | String[] optimized2 = dirReduc(directions2);
30 | System.out.println(Arrays.toString(optimized2)); // Output: []
31 |
32 | String[] directions3 = {"NORTH", "WEST", "SOUTH", "EAST"};
33 | String[] optimized3 = dirReduc(directions3);
34 | System.out.println(Arrays.toString(optimized3)); // Output: [NORTH, WEST, SOUTH, EAST]
35 | }
36 | }
37 |
--------------------------------------------------------------------------------