# 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;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://netjimmy.gitbook.io/code-interview-note/recursion/general.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
