Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashSet;
- import java.util.LinkedList;
- public class GuitarChords {
- public int stretch(String[] strings, String[] chord) {
- int[] str = new int[strings.length];
- int[] chd = new int[chord.length];
- for (int i = 0; i < str.length; i++) {
- str[i] = noteToInt(strings[i]);
- }
- for (int i = 0; i < chd.length; i++) {
- chd[i] = noteToInt(chord[i]);
- }
- return bestest(str, chd, new LinkedList<Integer>(), Integer.MAX_VALUE);
- }
- private int bestest(int[] str, int[] chd, LinkedList<Integer> comb, int best) {
- if (comb.size() == str.length) {
- return Math.min(eval(str, chd, comb), best);
- }
- for (int i = 0; i < 12; i++) {
- comb.add(str[comb.size()] + i);
- best = Math.min(best, bestest(str, chd, comb, best));
- comb.removeLast();
- }
- return best;
- }
- private int eval(int[] str, int[] chd, LinkedList<Integer> comb) {
- HashSet<Integer> left = new HashSet<Integer>();
- for (int i : chd) {
- left.add(i);
- }
- for (int i : comb) {
- if (left.contains(i)) {
- left.remove(i);
- } else {
- return Integer.MAX_VALUE;
- }
- }
- int min = Integer.MAX_VALUE;
- int max = Integer.MIN_VALUE;
- boolean fffuuu = true;
- for (int i = 0; i < str.length; i++) {
- int diff = comb.get(i) - str[i];
- if (diff != 0) {
- fffuuu = false;
- min = Math.min(min, diff);
- max = Math.max(max, diff);
- }
- }
- if (fffuuu) {
- return 0;
- }
- return max - min + 1;
- }
- public int noteToInt(String note) {
- String[] foo = "A A# B C C# D D# E F F# G G#".split(" ");
- for (int i = 0; i < foo.length; i++) {
- if (foo[i].equals(note)) return i;
- }
- return -1;
- }
- public static void main(String[] args) {
- System.out.println(new GuitarChords().stretch(
- "A C F".split(" "),
- "C# F# A#".split(" ")
- ));
- System.out.println(new GuitarChords().stretch(
- "E A D G B E".split(" "),
- "E G# B".split(" ")
- ));
- System.out.println(new GuitarChords().stretch(
- "D#".split(" "),
- "D#".split(" ")
- ));
- System.out.println(new GuitarChords().stretch(
- "E F".split(" "),
- "F# D#".split(" ")
- ));
- System.out.println(new GuitarChords().stretch(
- "C C C".split(" "),
- "C E G".split(" ")
- ));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment