|
| 1 | +package ru.mirea.practice.s23k0120.task1; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +public class Combinatorics { |
| 7 | + private static List<Integer> undirectIntegers(List<DirectedInteger> directedIntegers) { |
| 8 | + List<Integer> integers = new ArrayList<>(); |
| 9 | + for (DirectedInteger directedInteger : directedIntegers) { |
| 10 | + integers.add(directedInteger.getValue()); |
| 11 | + } |
| 12 | + return integers; |
| 13 | + } |
| 14 | + |
| 15 | + private int maxInd(List<DirectedInteger> directedIntegers) { |
| 16 | + int maxInd = 0; |
| 17 | + for (int i = 0; i < directedIntegers.size(); i++) { |
| 18 | + if (directedIntegers.get(i).getValue() > directedIntegers.get(maxInd).getValue()) { |
| 19 | + maxInd = i; |
| 20 | + } |
| 21 | + } |
| 22 | + return maxInd; |
| 23 | + } |
| 24 | + |
| 25 | + private int maxMobileInd(List<DirectedInteger> directedIntegers) { |
| 26 | + int maxInd = -1; |
| 27 | + int size = directedIntegers.size(); |
| 28 | + int maxUndirInd = maxInd(directedIntegers); |
| 29 | + if (maxUndirInd != 0 && maxUndirInd != size - 1 |
| 30 | + || maxUndirInd == 0 && directedIntegers.get(maxUndirInd).getDirection() == Direction.RIGHT |
| 31 | + || maxUndirInd == size - 1 && directedIntegers.get(maxUndirInd).getDirection() == Direction.LEFT) { |
| 32 | + return maxUndirInd; |
| 33 | + } |
| 34 | + for (int i = 0; i < size; i++) { |
| 35 | + DirectedInteger dirInt = directedIntegers.get(i); |
| 36 | + if (i != 0 && i != size - 1) { |
| 37 | + int indDirIntFacing = dirInt.getDirection() == Direction.LEFT ? i - 1 : i + 1; |
| 38 | + DirectedInteger dirIntFacing = directedIntegers.get(indDirIntFacing); |
| 39 | + if (dirInt.getValue() > dirIntFacing.getValue()) { |
| 40 | + if (maxInd == -1) { |
| 41 | + maxInd = i; |
| 42 | + } else { |
| 43 | + DirectedInteger maxDirInt = directedIntegers.get(maxInd); |
| 44 | + if (dirInt.getValue() > maxDirInt.getValue()) { |
| 45 | + maxInd = i; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } else if (i == size - 1 && dirInt.getDirection() == Direction.LEFT && directedIntegers.get(i - 1).getValue() < dirInt.getValue()) { |
| 50 | + maxInd = i; |
| 51 | + } else if (i == 0 && dirInt.getDirection() == Direction.RIGHT && directedIntegers.get(1).getValue() < dirInt.getValue()) { |
| 52 | + maxInd = i; |
| 53 | + } |
| 54 | + } |
| 55 | + return maxInd; |
| 56 | + } |
| 57 | + |
| 58 | + public List<List<Integer>> permutations(int n) { |
| 59 | + List<DirectedInteger> directedIntegers = new ArrayList<>(); |
| 60 | + for (int i = 0; i < n; i++) { |
| 61 | + directedIntegers.add(new DirectedInteger(i + 1, Direction.LEFT)); |
| 62 | + } |
| 63 | + List<List<Integer>> permutations = new ArrayList<>(); |
| 64 | + permutations.add(undirectIntegers(directedIntegers)); |
| 65 | + while (true) { |
| 66 | + int ind = maxMobileInd(directedIntegers); |
| 67 | + if (ind == -1) { |
| 68 | + break; |
| 69 | + } |
| 70 | + DirectedInteger maxDirInt = directedIntegers.get(ind); |
| 71 | + int tempInd = maxDirInt.getDirection() == Direction.LEFT ? ind - 1 : ind + 1; |
| 72 | + DirectedInteger tempDirInt = directedIntegers.get(tempInd); |
| 73 | + directedIntegers.set(ind, tempDirInt); |
| 74 | + directedIntegers.set(tempInd, maxDirInt); |
| 75 | + for (DirectedInteger directedInteger : directedIntegers) { |
| 76 | + if (directedInteger.getValue() > maxDirInt.getValue()) { |
| 77 | + directedInteger.switchDirection(); |
| 78 | + } |
| 79 | + } |
| 80 | + permutations.add(undirectIntegers(directedIntegers)); |
| 81 | + } |
| 82 | + return permutations; |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + enum Direction { LEFT, RIGHT } |
| 87 | + |
| 88 | + private class DirectedInteger { |
| 89 | + private final Integer value; |
| 90 | + private Direction direction; |
| 91 | + |
| 92 | + public DirectedInteger(Integer value, Direction direction) { |
| 93 | + this.value = value; |
| 94 | + this.direction = direction; |
| 95 | + } |
| 96 | + |
| 97 | + public Integer getValue() { |
| 98 | + return value; |
| 99 | + } |
| 100 | + |
| 101 | + public Direction getDirection() { |
| 102 | + return direction; |
| 103 | + } |
| 104 | + |
| 105 | + public void setDirection(Direction direction) { |
| 106 | + this.direction = direction; |
| 107 | + } |
| 108 | + |
| 109 | + public void switchDirection() { |
| 110 | + this.direction = this.direction == Direction.LEFT ? Direction.RIGHT : Direction.LEFT; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String toString() { |
| 115 | + return direction + " " + value; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments