# General

## Hanoi Tower

```java
private static final int NUM_PEGS = 3;
public static void computeTowerHanoi(int numRings){
  List<Deque<Integer>> pegs = new ArrayList<>();
  for(int i = 0; i < NUM_PEGS; i++){
    pegs.add(new LinkedList<Integer>)
  }
  for(int i = numRings; i > 0 ; i--){
    pegs.get(0).push(i);
  }

  helper(pegs, numRings, 0, 1, 2)
}

public static void helper(List<Deque<Integer>> pegs, int numRings, int pegFrom, int pegTo, int pegUse){
  if (numRings > 0){
    helper(pegs, numRings - 1, pegFrom, pegUse, pegTo);
    pegs.get(pegTo).push(pegs.get(pegFrom).pop());
    helper(pegs, numRings - 1, pegUse, pegTo, pegFrom);
  }
}
```

## n-Queens

```java
public static List<List<Integer>> nQueens(int n){
  List<List<Integer>> result = new ArrayList<>();
  helper(n, 0, new ArrayList<Integer>(), result);
  return result;
}

private static void helper(int n, int row, List<Integer> col_list, List<List<Integer>> result){
  if (row == n){
    result.add(col_list);
  }else{
    for (int i = 0; i < n; i++){
      col_list.add(i);
      if (isValid(col_list)){
        helper(n, row + 1, col_list, result);
      }
      col_list.remove(row);
    }
  }
}
private static boolean helper(List<Integer> col_list){
  int newID = col_list.size()-1;
  for (int i = 0; i < newID; i++){
    int diff = Math.abs(col_list.get(i) - col_list.get(newID));
    if (diff == 0 || diff == newID - i){
     return false;
    }
  }
  return true;
}
```
