translation: Add Python and Java code for EN version (#1345)

* Add the intial translation of code of all the languages

* test

* revert

* Remove

* Add Python and Java code for EN version
This commit is contained in:
Yudong Jin 2024-05-06 05:21:51 +08:00 committed by GitHub
parent b5e198db7d
commit 1c0f350ad6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
174 changed files with 12349 additions and 0 deletions

1
en/codes/java/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
build

View file

@ -0,0 +1,105 @@
/**
* File: array.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_array_and_linkedlist;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class array {
/* Random access to elements */
static int randomAccess(int[] nums) {
// Randomly select a number in the interval [0, nums.length)
int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);
// Retrieve and return a random element
int randomNum = nums[randomIndex];
return randomNum;
}
/* Extend array length */
static int[] extend(int[] nums, int enlarge) {
// Initialize an extended length array
int[] res = new int[nums.length + enlarge];
// Copy all elements from the original array to the new array
for (int i = 0; i < nums.length; i++) {
res[i] = nums[i];
}
// Return the new array after expansion
return res;
}
/* Insert element num at `index` */
static void insert(int[] nums, int num, int index) {
// Move all elements after `index` one position backward
for (int i = nums.length - 1; i > index; i--) {
nums[i] = nums[i - 1];
}
// Assign num to the element at index
nums[index] = num;
}
/* Remove the element at `index` */
static void remove(int[] nums, int index) {
// Move all elements after `index` one position forward
for (int i = index; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
}
/* Traverse array */
static void traverse(int[] nums) {
int count = 0;
// Traverse array by index
for (int i = 0; i < nums.length; i++) {
count += nums[i];
}
// Traverse array elements
for (int num : nums) {
count += num;
}
}
/* Search for a specified element in the array */
static int find(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target)
return i;
}
return -1;
}
/* Driver Code */
public static void main(String[] args) {
/* Initialize an array */
int[] arr = new int[5];
System.out.println("Array arr = " + Arrays.toString(arr));
int[] nums = { 1, 3, 2, 5, 4 };
System.out.println("Array nums = " + Arrays.toString(nums));
/* Random access */
int randomNum = randomAccess(nums);
System.out.println("Get a random element from nums = " + randomNum);
/* Length extension */
nums = extend(nums, 3);
System.out.println("Extend the array length to 8, resulting in nums = " + Arrays.toString(nums));
/* Insert element */
insert(nums, 6, 3);
System.out.println("Insert the number 6 at index 3, resulting in nums = " + Arrays.toString(nums));
/* Remove element */
remove(nums, 2);
System.out.println("Remove the element at index 2, resulting in nums = " + Arrays.toString(nums));
/* Traverse array */
traverse(nums);
/* Search for elements */
int index = find(nums, 3);
System.out.println("Find element 3 in nums, index = " + index);
}
}

View file

@ -0,0 +1,86 @@
/**
* File: linked_list.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_array_and_linkedlist;
import utils.*;
public class linked_list {
/* Insert node P after node n0 in the linked list */
static void insert(ListNode n0, ListNode P) {
ListNode n1 = n0.next;
P.next = n1;
n0.next = P;
}
/* Remove the first node after node n0 in the linked list */
static void remove(ListNode n0) {
if (n0.next == null)
return;
// n0 -> P -> n1
ListNode P = n0.next;
ListNode n1 = P.next;
n0.next = n1;
}
/* Access the node at `index` in the linked list */
static ListNode access(ListNode head, int index) {
for (int i = 0; i < index; i++) {
if (head == null)
return null;
head = head.next;
}
return head;
}
/* Search for the first node with value target in the linked list */
static int find(ListNode head, int target) {
int index = 0;
while (head != null) {
if (head.val == target)
return index;
head = head.next;
index++;
}
return -1;
}
/* Driver Code */
public static void main(String[] args) {
/* Initialize linked list */
// Initialize each node
ListNode n0 = new ListNode(1);
ListNode n1 = new ListNode(3);
ListNode n2 = new ListNode(2);
ListNode n3 = new ListNode(5);
ListNode n4 = new ListNode(4);
// Build references between nodes
n0.next = n1;
n1.next = n2;
n2.next = n3;
n3.next = n4;
System.out.println("The initialized linked list is");
PrintUtil.printLinkedList(n0);
/* Insert node */
insert(n0, new ListNode(0));
System.out.println("Linked list after inserting the node is");
PrintUtil.printLinkedList(n0);
/* Remove node */
remove(n0);
System.out.println("Linked list after removing the node is");
PrintUtil.printLinkedList(n0);
/* Access node */
ListNode node = access(n0, 3);
System.out.println("The value of the node at index 3 in the linked list = " + node.val);
/* Search node */
int index = find(n0, 2);
System.out.println("The index of the node with value 2 in the linked list = " + index);
}
}

View file

@ -0,0 +1,66 @@
/**
* File: list.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_array_and_linkedlist;
import java.util.*;
public class list {
public static void main(String[] args) {
/* Initialize list */
// The array's element type is Integer[], a wrapper class for int
Integer[] numbers = new Integer[] { 1, 3, 2, 5, 4 };
List<Integer> nums = new ArrayList<>(Arrays.asList(numbers));
System.out.println("List nums = " + nums);
/* Access element */
int num = nums.get(1);
System.out.println("Access the element at index 1, obtained num = " + num);
/* Update element */
nums.set(1, 0);
System.out.println("Update the element at index 1 to 0, resulting in nums = " + nums);
/* Clear list */
nums.clear();
System.out.println("After clearing the list, nums = " + nums);
/* Add element at the end */
nums.add(1);
nums.add(3);
nums.add(2);
nums.add(5);
nums.add(4);
System.out.println("After adding elements, nums = " + nums);
/* Insert element in the middle */
nums.add(3, 6);
System.out.println("Insert the number 6 at index 3, resulting in nums = " + nums);
/* Remove element */
nums.remove(3);
System.out.println("Remove the element at index 3, resulting in nums = " + nums);
/* Traverse the list by index */
int count = 0;
for (int i = 0; i < nums.size(); i++) {
count += nums.get(i);
}
/* Traverse the list elements */
for (int x : nums) {
count += x;
}
/* Concatenate two lists */
List<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));
nums.addAll(nums1);
System.out.println("Concatenate list nums1 to nums, resulting in nums = " + nums);
/* Sort list */
Collections.sort(nums);
System.out.println("After sorting the list, nums = " + nums);
}
}

View file

@ -0,0 +1,147 @@
/**
* File: my_list.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_array_and_linkedlist;
import java.util.*;
/* List class */
class MyList {
private int[] arr; // Array (stores list elements)
private int capacity = 10; // List capacity
private int size = 0; // List length (current number of elements)
private int extendRatio = 2; // Multiple for each list expansion
/* Constructor */
public MyList() {
arr = new int[capacity];
}
/* Get list length (current number of elements) */
public int size() {
return size;
}
/* Get list capacity */
public int capacity() {
return capacity;
}
/* Access element */
public int get(int index) {
// If the index is out of bounds, throw an exception, as below
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index out of bounds");
return arr[index];
}
/* Update element */
public void set(int index, int num) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index out of bounds");
arr[index] = num;
}
/* Add element at the end */
public void add(int num) {
// When the number of elements exceeds capacity, trigger the expansion mechanism
if (size == capacity())
extendCapacity();
arr[size] = num;
// Update the number of elements
size++;
}
/* Insert element in the middle */
public void insert(int index, int num) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index out of bounds");
// When the number of elements exceeds capacity, trigger the expansion mechanism
if (size == capacity())
extendCapacity();
// Move all elements after `index` one position backward
for (int j = size - 1; j >= index; j--) {
arr[j + 1] = arr[j];
}
arr[index] = num;
// Update the number of elements
size++;
}
/* Remove element */
public int remove(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index out of bounds");
int num = arr[index];
// Move all elements after `index` one position forward
for (int j = index; j < size - 1; j++) {
arr[j] = arr[j + 1];
}
// Update the number of elements
size--;
// Return the removed element
return num;
}
/* Extend list */
public void extendCapacity() {
// Create a new array with a length multiple of the original array by extendRatio, and copy the original array to the new array
arr = Arrays.copyOf(arr, capacity() * extendRatio);
// Update list capacity
capacity = arr.length;
}
/* Convert the list to an array */
public int[] toArray() {
int size = size();
// Only convert elements within valid length range
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = get(i);
}
return arr;
}
}
public class my_list {
/* Driver Code */
public static void main(String[] args) {
/* Initialize list */
MyList nums = new MyList();
/* Add element at the end */
nums.add(1);
nums.add(3);
nums.add(2);
nums.add(5);
nums.add(4);
System.out.println("List nums = " + Arrays.toString(nums.toArray()) +
", capacity = " + nums.capacity() + ", length = " + nums.size());
/* Insert element in the middle */
nums.insert(3, 6);
System.out.println("Insert the number 6 at index 3, resulting in nums = " + Arrays.toString(nums.toArray()));
/* Remove element */
nums.remove(3);
System.out.println("Remove the element at index 3, resulting in nums = " + Arrays.toString(nums.toArray()));
/* Access element */
int num = nums.get(1);
System.out.println("Access the element at index 1, obtained num = " + num);
/* Update element */
nums.set(1, 0);
System.out.println("Update the element at index 1 to 0, resulting in nums = " + Arrays.toString(nums.toArray()));
/* Test expansion mechanism */
for (int i = 0; i < 10; i++) {
// At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism at this time
nums.add(i);
}
System.out.println("After extending, list nums = " + Arrays.toString(nums.toArray()) +
", capacity = " + nums.capacity() + ", length = " + nums.size());
}
}

View file

@ -0,0 +1,77 @@
/**
* File: n_queens.java
* Created Time: 2023-05-04
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import java.util.*;
public class n_queens {
/* Backtracking algorithm: n queens */
public static void backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,
boolean[] cols, boolean[] diags1, boolean[] diags2) {
// When all rows are placed, record the solution
if (row == n) {
List<List<String>> copyState = new ArrayList<>();
for (List<String> sRow : state) {
copyState.add(new ArrayList<>(sRow));
}
res.add(copyState);
return;
}
// Traverse all columns
for (int col = 0; col < n; col++) {
// Calculate the main and minor diagonals corresponding to the cell
int diag1 = row - col + n - 1;
int diag2 = row + col;
// Pruning: do not allow queens on the column, main diagonal, or minor diagonal of the cell
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// Attempt: place the queen in the cell
state.get(row).set(col, "Q");
cols[col] = diags1[diag1] = diags2[diag2] = true;
// Place the next row
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// Retract: restore the cell to an empty spot
state.get(row).set(col, "#");
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
/* Solve n queens */
public static List<List<List<String>>> nQueens(int n) {
// Initialize an n*n size chessboard, where 'Q' represents the queen and '#' represents an empty spot
List<List<String>> state = new ArrayList<>();
for (int i = 0; i < n; i++) {
List<String> row = new ArrayList<>();
for (int j = 0; j < n; j++) {
row.add("#");
}
state.add(row);
}
boolean[] cols = new boolean[n]; // Record columns with queens
boolean[] diags1 = new boolean[2 * n - 1]; // Record main diagonals with queens
boolean[] diags2 = new boolean[2 * n - 1]; // Record minor diagonals with queens
List<List<List<String>>> res = new ArrayList<>();
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
public static void main(String[] args) {
int n = 4;
List<List<List<String>>> res = nQueens(n);
System.out.println("Input the dimensions of the chessboard as " + n);
System.out.println("Total number of queen placement solutions = " + res.size());
for (List<List<String>> state : res) {
System.out.println("--------------------");
for (List<String> row : state) {
System.out.println(row);
}
}
}
}

View file

@ -0,0 +1,51 @@
/**
* File: permutations_i.java
* Created Time: 2023-04-24
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import java.util.*;
public class permutations_i {
/* Backtracking algorithm: Permutation I */
public static void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
// When the state length equals the number of elements, record the solution
if (state.size() == choices.length) {
res.add(new ArrayList<Integer>(state));
return;
}
// Traverse all choices
for (int i = 0; i < choices.length; i++) {
int choice = choices[i];
// Pruning: do not allow repeated selection of elements
if (!selected[i]) {
// Attempt: make a choice, update the state
selected[i] = true;
state.add(choice);
// Proceed to the next round of selection
backtrack(state, choices, selected, res);
// Retract: undo the choice, restore to the previous state
selected[i] = false;
state.remove(state.size() - 1);
}
}
}
/* Permutation I */
static List<List<Integer>> permutationsI(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
return res;
}
public static void main(String[] args) {
int[] nums = { 1, 2, 3 };
List<List<Integer>> res = permutationsI(nums);
System.out.println("Input array nums = " + Arrays.toString(nums));
System.out.println("All permutations res = " + res);
}
}

View file

@ -0,0 +1,53 @@
/**
* File: permutations_ii.java
* Created Time: 2023-04-24
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import java.util.*;
public class permutations_ii {
/* Backtracking algorithm: Permutation II */
static void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
// When the state length equals the number of elements, record the solution
if (state.size() == choices.length) {
res.add(new ArrayList<Integer>(state));
return;
}
// Traverse all choices
Set<Integer> duplicated = new HashSet<Integer>();
for (int i = 0; i < choices.length; i++) {
int choice = choices[i];
// Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements
if (!selected[i] && !duplicated.contains(choice)) {
// Attempt: make a choice, update the state
duplicated.add(choice); // Record selected element values
selected[i] = true;
state.add(choice);
// Proceed to the next round of selection
backtrack(state, choices, selected, res);
// Retract: undo the choice, restore to the previous state
selected[i] = false;
state.remove(state.size() - 1);
}
}
}
/* Permutation II */
static List<List<Integer>> permutationsII(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
return res;
}
public static void main(String[] args) {
int[] nums = { 1, 2, 2 };
List<List<Integer>> res = permutationsII(nums);
System.out.println("Input array nums = " + Arrays.toString(nums));
System.out.println("All permutations res = " + res);
}
}

View file

@ -0,0 +1,44 @@
/**
* File: preorder_traversal_i_compact.java
* Created Time: 2023-04-16
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import utils.*;
import java.util.*;
public class preorder_traversal_i_compact {
static List<TreeNode> res;
/* Pre-order traversal: Example one */
static void preOrder(TreeNode root) {
if (root == null) {
return;
}
if (root.val == 7) {
// Record solution
res.add(root);
}
preOrder(root.left);
preOrder(root.right);
}
public static void main(String[] args) {
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
System.out.println("\nInitialize binary tree");
PrintUtil.printTree(root);
// Pre-order traversal
res = new ArrayList<>();
preOrder(root);
System.out.println("\nOutput all nodes with value 7");
List<Integer> vals = new ArrayList<>();
for (TreeNode node : res) {
vals.add(node.val);
}
System.out.println(vals);
}
}

View file

@ -0,0 +1,52 @@
/**
* File: preorder_traversal_ii_compact.java
* Created Time: 2023-04-16
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import utils.*;
import java.util.*;
public class preorder_traversal_ii_compact {
static List<TreeNode> path;
static List<List<TreeNode>> res;
/* Pre-order traversal: Example two */
static void preOrder(TreeNode root) {
if (root == null) {
return;
}
// Attempt
path.add(root);
if (root.val == 7) {
// Record solution
res.add(new ArrayList<>(path));
}
preOrder(root.left);
preOrder(root.right);
// Retract
path.remove(path.size() - 1);
}
public static void main(String[] args) {
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
System.out.println("\nInitialize binary tree");
PrintUtil.printTree(root);
// Pre-order traversal
path = new ArrayList<>();
res = new ArrayList<>();
preOrder(root);
System.out.println("\nOutput all root-to-node 7 paths");
for (List<TreeNode> path : res) {
List<Integer> vals = new ArrayList<>();
for (TreeNode node : path) {
vals.add(node.val);
}
System.out.println(vals);
}
}
}

View file

@ -0,0 +1,53 @@
/**
* File: preorder_traversal_iii_compact.java
* Created Time: 2023-04-16
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import utils.*;
import java.util.*;
public class preorder_traversal_iii_compact {
static List<TreeNode> path;
static List<List<TreeNode>> res;
/* Pre-order traversal: Example three */
static void preOrder(TreeNode root) {
// Pruning
if (root == null || root.val == 3) {
return;
}
// Attempt
path.add(root);
if (root.val == 7) {
// Record solution
res.add(new ArrayList<>(path));
}
preOrder(root.left);
preOrder(root.right);
// Retract
path.remove(path.size() - 1);
}
public static void main(String[] args) {
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
System.out.println("\nInitialize binary tree");
PrintUtil.printTree(root);
// Pre-order traversal
path = new ArrayList<>();
res = new ArrayList<>();
preOrder(root);
System.out.println("\nOutput all root-to-node 7 paths, not including nodes with value 3");
for (List<TreeNode> path : res) {
List<Integer> vals = new ArrayList<>();
for (TreeNode node : path) {
vals.add(node.val);
}
System.out.println(vals);
}
}
}

View file

@ -0,0 +1,77 @@
/**
* File: preorder_traversal_iii_template.java
* Created Time: 2023-04-16
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import utils.*;
import java.util.*;
public class preorder_traversal_iii_template {
/* Determine if the current state is a solution */
static boolean isSolution(List<TreeNode> state) {
return !state.isEmpty() && state.get(state.size() - 1).val == 7;
}
/* Record solution */
static void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
res.add(new ArrayList<>(state));
}
/* Determine if the choice is legal under the current state */
static boolean isValid(List<TreeNode> state, TreeNode choice) {
return choice != null && choice.val != 3;
}
/* Update state */
static void makeChoice(List<TreeNode> state, TreeNode choice) {
state.add(choice);
}
/* Restore state */
static void undoChoice(List<TreeNode> state, TreeNode choice) {
state.remove(state.size() - 1);
}
/* Backtracking algorithm: Example three */
static void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
// Check if it's a solution
if (isSolution(state)) {
// Record solution
recordSolution(state, res);
}
// Traverse all choices
for (TreeNode choice : choices) {
// Pruning: check if the choice is legal
if (isValid(state, choice)) {
// Attempt: make a choice, update the state
makeChoice(state, choice);
// Proceed to the next round of selection
backtrack(state, Arrays.asList(choice.left, choice.right), res);
// Retract: undo the choice, restore to the previous state
undoChoice(state, choice);
}
}
}
public static void main(String[] args) {
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
System.out.println("\nInitialize binary tree");
PrintUtil.printTree(root);
// Backtracking algorithm
List<List<TreeNode>> res = new ArrayList<>();
backtrack(new ArrayList<>(), Arrays.asList(root), res);
System.out.println("\nOutput all root-to-node 7 paths, requiring paths not to include nodes with value 3");
for (List<TreeNode> path : res) {
List<Integer> vals = new ArrayList<>();
for (TreeNode node : path) {
vals.add(node.val);
}
System.out.println(vals);
}
}
}

View file

@ -0,0 +1,55 @@
/**
* File: subset_sum_i.java
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import java.util.*;
public class subset_sum_i {
/* Backtracking algorithm: Subset Sum I */
static void backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {
// When the subset sum equals target, record the solution
if (target == 0) {
res.add(new ArrayList<>(state));
return;
}
// Traverse all choices
// Pruning two: start traversing from start to avoid generating duplicate subsets
for (int i = start; i < choices.length; i++) {
// Pruning one: if the subset sum exceeds target, end the loop immediately
// This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target
if (target - choices[i] < 0) {
break;
}
// Attempt: make a choice, update target, start
state.add(choices[i]);
// Proceed to the next round of selection
backtrack(state, target - choices[i], choices, i, res);
// Retract: undo the choice, restore to the previous state
state.remove(state.size() - 1);
}
}
/* Solve Subset Sum I */
static List<List<Integer>> subsetSumI(int[] nums, int target) {
List<Integer> state = new ArrayList<>(); // State (subset)
Arrays.sort(nums); // Sort nums
int start = 0; // Start point for traversal
List<List<Integer>> res = new ArrayList<>(); // Result list (subset list)
backtrack(state, target, nums, start, res);
return res;
}
public static void main(String[] args) {
int[] nums = { 3, 4, 5 };
int target = 9;
List<List<Integer>> res = subsetSumI(nums, target);
System.out.println("Input array nums = " + Arrays.toString(nums) + ", target = " + target);
System.out.println("All subsets summing to " + target + " res = " + res);
}
}

View file

@ -0,0 +1,53 @@
/**
* File: subset_sum_i_naive.java
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import java.util.*;
public class subset_sum_i_naive {
/* Backtracking algorithm: Subset Sum I */
static void backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {
// When the subset sum equals target, record the solution
if (total == target) {
res.add(new ArrayList<>(state));
return;
}
// Traverse all choices
for (int i = 0; i < choices.length; i++) {
// Pruning: if the subset sum exceeds target, skip that choice
if (total + choices[i] > target) {
continue;
}
// Attempt: make a choice, update elements and total
state.add(choices[i]);
// Proceed to the next round of selection
backtrack(state, target, total + choices[i], choices, res);
// Retract: undo the choice, restore to the previous state
state.remove(state.size() - 1);
}
}
/* Solve Subset Sum I (including duplicate subsets) */
static List<List<Integer>> subsetSumINaive(int[] nums, int target) {
List<Integer> state = new ArrayList<>(); // State (subset)
int total = 0; // Subset sum
List<List<Integer>> res = new ArrayList<>(); // Result list (subset list)
backtrack(state, target, total, nums, res);
return res;
}
public static void main(String[] args) {
int[] nums = { 3, 4, 5 };
int target = 9;
List<List<Integer>> res = subsetSumINaive(nums, target);
System.out.println("Input array nums = " + Arrays.toString(nums) + ", target = " + target);
System.out.println("All subsets summing to " + target + " res = " + res);
System.out.println("Please note that the result of this method includes duplicate sets");
}
}

View file

@ -0,0 +1,60 @@
/**
* File: subset_sum_ii.java
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import java.util.*;
public class subset_sum_ii {
/* Backtracking algorithm: Subset Sum II */
static void backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {
// When the subset sum equals target, record the solution
if (target == 0) {
res.add(new ArrayList<>(state));
return;
}
// Traverse all choices
// Pruning two: start traversing from start to avoid generating duplicate subsets
// Pruning three: start traversing from start to avoid repeatedly selecting the same element
for (int i = start; i < choices.length; i++) {
// Pruning one: if the subset sum exceeds target, end the loop immediately
// This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target
if (target - choices[i] < 0) {
break;
}
// Pruning four: if the element equals the left element, it indicates that the search branch is repeated, skip it
if (i > start && choices[i] == choices[i - 1]) {
continue;
}
// Attempt: make a choice, update target, start
state.add(choices[i]);
// Proceed to the next round of selection
backtrack(state, target - choices[i], choices, i + 1, res);
// Retract: undo the choice, restore to the previous state
state.remove(state.size() - 1);
}
}
/* Solve Subset Sum II */
static List<List<Integer>> subsetSumII(int[] nums, int target) {
List<Integer> state = new ArrayList<>(); // State (subset)
Arrays.sort(nums); // Sort nums
int start = 0; // Start point for traversal
List<List<Integer>> res = new ArrayList<>(); // Result list (subset list)
backtrack(state, target, nums, start, res);
return res;
}
public static void main(String[] args) {
int[] nums = { 4, 4, 5 };
int target = 9;
List<List<Integer>> res = subsetSumII(nums, target);
System.out.println("Input array nums = " + Arrays.toString(nums) + ", target = " + target);
System.out.println("All subsets summing to " + target + " res = " + res);
}
}

View file

@ -0,0 +1,76 @@
/**
* File: iteration.java
* Created Time: 2023-08-24
* Author: krahets (krahets@163.com)
*/
package chapter_computational_complexity;
public class iteration {
/* for loop */
static int forLoop(int n) {
int res = 0;
// Loop sum 1, 2, ..., n-1, n
for (int i = 1; i <= n; i++) {
res += i;
}
return res;
}
/* while loop */
static int whileLoop(int n) {
int res = 0;
int i = 1; // Initialize condition variable
// Loop sum 1, 2, ..., n-1, n
while (i <= n) {
res += i;
i++; // Update condition variable
}
return res;
}
/* while loop (two updates) */
static int whileLoopII(int n) {
int res = 0;
int i = 1; // Initialize condition variable
// Loop sum 1, 4, 10, ...
while (i <= n) {
res += i;
// Update condition variable
i++;
i *= 2;
}
return res;
}
/* Double for loop */
static String nestedForLoop(int n) {
StringBuilder res = new StringBuilder();
// Loop i = 1, 2, ..., n-1, n
for (int i = 1; i <= n; i++) {
// Loop j = 1, 2, ..., n-1, n
for (int j = 1; j <= n; j++) {
res.append("(" + i + ", " + j + "), ");
}
}
return res.toString();
}
/* Driver Code */
public static void main(String[] args) {
int n = 5;
int res;
res = forLoop(n);
System.out.println("\nSum result of the for loop res = " + res);
res = whileLoop(n);
System.out.println("\nSum result of the while loop res = " + res);
res = whileLoopII(n);
System.out.println("\nSum result of the while loop (with two updates) res = " + res);
String resStr = nestedForLoop(n);
System.out.println("\nResult of the double for loop traversal = " + resStr);
}
}

View file

@ -0,0 +1,79 @@
/**
* File: recursion.java
* Created Time: 2023-08-24
* Author: krahets (krahets@163.com)
*/
package chapter_computational_complexity;
import java.util.Stack;
public class recursion {
/* Recursion */
static int recur(int n) {
// Termination condition
if (n == 1)
return 1;
// Recursive: recursive call
int res = recur(n - 1);
// Return: return result
return n + res;
}
/* Simulate recursion with iteration */
static int forLoopRecur(int n) {
// Use an explicit stack to simulate the system call stack
Stack<Integer> stack = new Stack<>();
int res = 0;
// Recursive: recursive call
for (int i = n; i > 0; i--) {
// Simulate "recursive" by "pushing onto the stack"
stack.push(i);
}
// Return: return result
while (!stack.isEmpty()) {
// Simulate "return" by "popping from the stack"
res += stack.pop();
}
// res = 1+2+3+...+n
return res;
}
/* Tail recursion */
static int tailRecur(int n, int res) {
// Termination condition
if (n == 0)
return res;
// Tail recursive call
return tailRecur(n - 1, res + n);
}
/* Fibonacci sequence: Recursion */
static int fib(int n) {
// Termination condition f(1) = 0, f(2) = 1
if (n == 1 || n == 2)
return n - 1;
// Recursive call f(n) = f(n-1) + f(n-2)
int res = fib(n - 1) + fib(n - 2);
// Return result f(n)
return res;
}
/* Driver Code */
public static void main(String[] args) {
int n = 5;
int res;
res = recur(n);
System.out.println("\nSum result of the recursive function res = " + res);
res = forLoopRecur(n);
System.out.println("\nSum result using iteration to simulate recursion res = " + res);
res = tailRecur(n, 0);
System.out.println("\nSum result of the tail-recursive function res = " + res);
res = fib(n);
System.out.println("\nThe " + n + "th number in the Fibonacci sequence is " + res);
}
}

View file

@ -0,0 +1,110 @@
/**
* File: space_complexity.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_computational_complexity;
import utils.*;
import java.util.*;
public class space_complexity {
/* Function */
static int function() {
// Perform some operations
return 0;
}
/* Constant complexity */
static void constant(int n) {
// Constants, variables, objects occupy O(1) space
final int a = 0;
int b = 0;
int[] nums = new int[10000];
ListNode node = new ListNode(0);
// Variables in a loop occupy O(1) space
for (int i = 0; i < n; i++) {
int c = 0;
}
// Functions in a loop occupy O(1) space
for (int i = 0; i < n; i++) {
function();
}
}
/* Linear complexity */
static void linear(int n) {
// Array of length n occupies O(n) space
int[] nums = new int[n];
// A list of length n occupies O(n) space
List<ListNode> nodes = new ArrayList<>();
for (int i = 0; i < n; i++) {
nodes.add(new ListNode(i));
}
// A hash table of length n occupies O(n) space
Map<Integer, String> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(i, String.valueOf(i));
}
}
/* Linear complexity (recursive implementation) */
static void linearRecur(int n) {
System.out.println("Recursion n = " + n);
if (n == 1)
return;
linearRecur(n - 1);
}
/* Quadratic complexity */
static void quadratic(int n) {
// Matrix occupies O(n^2) space
int[][] numMatrix = new int[n][n];
// A two-dimensional list occupies O(n^2) space
List<List<Integer>> numList = new ArrayList<>();
for (int i = 0; i < n; i++) {
List<Integer> tmp = new ArrayList<>();
for (int j = 0; j < n; j++) {
tmp.add(0);
}
numList.add(tmp);
}
}
/* Quadratic complexity (recursive implementation) */
static int quadraticRecur(int n) {
if (n <= 0)
return 0;
// Array nums length = n, n-1, ..., 2, 1
int[] nums = new int[n];
System.out.println("Recursion n = " + n + " in the length of nums = " + nums.length);
return quadraticRecur(n - 1);
}
/* Exponential complexity (building a full binary tree) */
static TreeNode buildTree(int n) {
if (n == 0)
return null;
TreeNode root = new TreeNode(0);
root.left = buildTree(n - 1);
root.right = buildTree(n - 1);
return root;
}
/* Driver Code */
public static void main(String[] args) {
int n = 5;
// Constant complexity
constant(n);
// Linear complexity
linear(n);
linearRecur(n);
// Quadratic complexity
quadratic(n);
quadraticRecur(n);
// Exponential complexity
TreeNode root = buildTree(n);
PrintUtil.printTree(root);
}
}

View file

@ -0,0 +1,167 @@
/**
* File: time_complexity.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_computational_complexity;
public class time_complexity {
/* Constant complexity */
static int constant(int n) {
int count = 0;
int size = 100000;
for (int i = 0; i < size; i++)
count++;
return count;
}
/* Linear complexity */
static int linear(int n) {
int count = 0;
for (int i = 0; i < n; i++)
count++;
return count;
}
/* Linear complexity (traversing an array) */
static int arrayTraversal(int[] nums) {
int count = 0;
// Loop count is proportional to the length of the array
for (int num : nums) {
count++;
}
return count;
}
/* Quadratic complexity */
static int quadratic(int n) {
int count = 0;
// Loop count is squared in relation to the data size n
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
count++;
}
}
return count;
}
/* Quadratic complexity (bubble sort) */
static int bubbleSort(int[] nums) {
int count = 0; // Counter
// Outer loop: unsorted range is [0, i]
for (int i = nums.length - 1; i > 0; i--) {
// Inner loop: swap the largest element in the unsorted range [0, i] to the right end of the range
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// Swap nums[j] and nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
count += 3; // Element swap includes 3 individual operations
}
}
}
return count;
}
/* Exponential complexity (loop implementation) */
static int exponential(int n) {
int count = 0, base = 1;
// Cells split into two every round, forming the sequence 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++) {
for (int j = 0; j < base; j++) {
count++;
}
base *= 2;
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count;
}
/* Exponential complexity (recursive implementation) */
static int expRecur(int n) {
if (n == 1)
return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
/* Logarithmic complexity (loop implementation) */
static int logarithmic(int n) {
int count = 0;
while (n > 1) {
n = n / 2;
count++;
}
return count;
}
/* Logarithmic complexity (recursive implementation) */
static int logRecur(int n) {
if (n <= 1)
return 0;
return logRecur(n / 2) + 1;
}
/* Linear logarithmic complexity */
static int linearLogRecur(int n) {
if (n <= 1)
return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
for (int i = 0; i < n; i++) {
count++;
}
return count;
}
/* Factorial complexity (recursive implementation) */
static int factorialRecur(int n) {
if (n == 0)
return 1;
int count = 0;
// From 1 split into n
for (int i = 0; i < n; i++) {
count += factorialRecur(n - 1);
}
return count;
}
/* Driver Code */
public static void main(String[] args) {
// Can modify n to experience the trend of operation count changes under various complexities
int n = 8;
System.out.println("Input data size n = " + n);
int count = constant(n);
System.out.println("Number of constant complexity operations = " + count);
count = linear(n);
System.out.println("Number of linear complexity operations = " + count);
count = arrayTraversal(new int[n]);
System.out.println("Number of linear complexity operations (traversing the array) = " + count);
count = quadratic(n);
System.out.println("Number of quadratic order operations = " + count);
int[] nums = new int[n];
for (int i = 0; i < n; i++)
nums[i] = n - i; // [n,n-1,...,2,1]
count = bubbleSort(nums);
System.out.println("Number of quadratic order operations (bubble sort) = " + count);
count = exponential(n);
System.out.println("Number of exponential complexity operations (implemented by loop) = " + count);
count = expRecur(n);
System.out.println("Number of exponential complexity operations (implemented by recursion) = " + count);
count = logarithmic(n);
System.out.println("Number of logarithmic complexity operations (implemented by loop) = " + count);
count = logRecur(n);
System.out.println("Number of logarithmic complexity operations (implemented by recursion) = " + count);
count = linearLogRecur(n);
System.out.println("Number of linear logarithmic complexity operations (implemented by recursion) = " + count);
count = factorialRecur(n);
System.out.println("Number of factorial complexity operations (implemented by recursion) = " + count);
}
}

View file

@ -0,0 +1,50 @@
/**
* File: worst_best_time_complexity.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_computational_complexity;
import java.util.*;
public class worst_best_time_complexity {
/* Generate an array with elements {1, 2, ..., n} in a randomly shuffled order */
static int[] randomNumbers(int n) {
Integer[] nums = new Integer[n];
// Generate array nums = { 1, 2, 3, ..., n }
for (int i = 0; i < n; i++) {
nums[i] = i + 1;
}
// Randomly shuffle array elements
Collections.shuffle(Arrays.asList(nums));
// Integer[] -> int[]
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nums[i];
}
return res;
}
/* Find the index of number 1 in array nums */
static int findOne(int[] nums) {
for (int i = 0; i < nums.length; i++) {
// When element 1 is at the start of the array, achieve best time complexity O(1)
// When element 1 is at the end of the array, achieve worst time complexity O(n)
if (nums[i] == 1)
return i;
}
return -1;
}
/* Driver Code */
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
int n = 100;
int[] nums = randomNumbers(n);
int index = findOne(nums);
System.out.println("\nThe array [ 1, 2, ..., n ] after being shuffled = " + Arrays.toString(nums));
System.out.println("The index of number 1 is " + index);
}
}
}

View file

@ -0,0 +1,45 @@
/**
* File: binary_search_recur.java
* Created Time: 2023-07-17
* Author: krahets (krahets@163.com)
*/
package chapter_divide_and_conquer;
public class binary_search_recur {
/* Binary search: problem f(i, j) */
static int dfs(int[] nums, int target, int i, int j) {
// If the interval is empty, indicating no target element, return -1
if (i > j) {
return -1;
}
// Calculate midpoint index m
int m = (i + j) / 2;
if (nums[m] < target) {
// Recursive subproblem f(m+1, j)
return dfs(nums, target, m + 1, j);
} else if (nums[m] > target) {
// Recursive subproblem f(i, m-1)
return dfs(nums, target, i, m - 1);
} else {
// Found the target element, thus return its index
return m;
}
}
/* Binary search */
static int binarySearch(int[] nums, int target) {
int n = nums.length;
// Solve problem f(0, n-1)
return dfs(nums, target, 0, n - 1);
}
public static void main(String[] args) {
int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
// Binary search (double closed interval)
int index = binarySearch(nums, target);
System.out.println("Index of target element 6 =" + index);
}
}

View file

@ -0,0 +1,51 @@
/**
* File: build_tree.java
* Created Time: 2023-07-17
* Author: krahets (krahets@163.com)
*/
package chapter_divide_and_conquer;
import utils.*;
import java.util.*;
public class build_tree {
/* Build binary tree: Divide and conquer */
static TreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {
// Terminate when subtree interval is empty
if (r - l < 0)
return null;
// Initialize root node
TreeNode root = new TreeNode(preorder[i]);
// Query m to divide left and right subtrees
int m = inorderMap.get(preorder[i]);
// Subproblem: build left subtree
root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// Subproblem: build right subtree
root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// Return root node
return root;
}
/* Build binary tree */
static TreeNode buildTree(int[] preorder, int[] inorder) {
// Initialize hash table, storing in-order elements to indices mapping
Map<Integer, Integer> inorderMap = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
inorderMap.put(inorder[i], i);
}
TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);
return root;
}
public static void main(String[] args) {
int[] preorder = { 3, 9, 2, 1, 7 };
int[] inorder = { 9, 3, 1, 2, 7 };
System.out.println("Pre-order traversal = " + Arrays.toString(preorder));
System.out.println("In-order traversal = " + Arrays.toString(inorder));
TreeNode root = buildTree(preorder, inorder);
System.out.println("The built binary tree is:");
PrintUtil.printTree(root);
}
}

View file

@ -0,0 +1,59 @@
/**
* File: hanota.java
* Created Time: 2023-07-17
* Author: krahets (krahets@163.com)
*/
package chapter_divide_and_conquer;
import java.util.*;
public class hanota {
/* Move a disc */
static void move(List<Integer> src, List<Integer> tar) {
// Take out a disc from the top of src
Integer pan = src.remove(src.size() - 1);
// Place the disc on top of tar
tar.add(pan);
}
/* Solve the Tower of Hanoi problem f(i) */
static void dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {
// If only one disc remains on src, move it to tar
if (i == 1) {
move(src, tar);
return;
}
// Subproblem f(i-1): move the top i-1 discs from src with the help of tar to buf
dfs(i - 1, src, tar, buf);
// Subproblem f(1): move the remaining one disc from src to tar
move(src, tar);
// Subproblem f(i-1): move the top i-1 discs from buf with the help of src to tar
dfs(i - 1, buf, src, tar);
}
/* Solve the Tower of Hanoi problem */
static void solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {
int n = A.size();
// Move the top n discs from A with the help of B to C
dfs(n, A, B, C);
}
public static void main(String[] args) {
// The tail of the list is the top of the pillar
List<Integer> A = new ArrayList<>(Arrays.asList(5, 4, 3, 2, 1));
List<Integer> B = new ArrayList<>();
List<Integer> C = new ArrayList<>();
System.out.println("Initial state:");
System.out.println("A = " + A);
System.out.println("B = " + B);
System.out.println("C = " + C);
solveHanota(A, B, C);
System.out.println("After the discs are moved:");
System.out.println("A = " + A);
System.out.println("B = " + B);
System.out.println("C = " + C);
}
}

View file

@ -0,0 +1,44 @@
/**
* File: climbing_stairs_backtrack.java
* Created Time: 2023-06-30
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
import java.util.*;
public class climbing_stairs_backtrack {
/* Backtracking */
public static void backtrack(List<Integer> choices, int state, int n, List<Integer> res) {
// When climbing to the nth step, add 1 to the number of solutions
if (state == n)
res.set(0, res.get(0) + 1);
// Traverse all choices
for (Integer choice : choices) {
// Pruning: do not allow climbing beyond the nth step
if (state + choice > n)
continue;
// Attempt: make a choice, update the state
backtrack(choices, state + choice, n, res);
// Retract
}
}
/* Climbing stairs: Backtracking */
public static int climbingStairsBacktrack(int n) {
List<Integer> choices = Arrays.asList(1, 2); // Can choose to climb up 1 step or 2 steps
int state = 0; // Start climbing from the 0th step
List<Integer> res = new ArrayList<>();
res.add(0); // Use res[0] to record the number of solutions
backtrack(choices, state, n, res);
return res.get(0);
}
public static void main(String[] args) {
int n = 9;
int res = climbingStairsBacktrack(n);
System.out.println(String.format("There are %d solutions to climb %d stairs", n, res));
}
}

View file

@ -0,0 +1,36 @@
/**
* File: climbing_stairs_constraint_dp.java
* Created Time: 2023-07-01
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
public class climbing_stairs_constraint_dp {
/* Constrained climbing stairs: Dynamic programming */
static int climbingStairsConstraintDP(int n) {
if (n == 1 || n == 2) {
return 1;
}
// Initialize dp table, used to store subproblem solutions
int[][] dp = new int[n + 1][3];
// Initial state: preset the smallest subproblem solution
dp[1][1] = 1;
dp[1][2] = 0;
dp[2][1] = 0;
dp[2][2] = 1;
// State transition: gradually solve larger subproblems from smaller ones
for (int i = 3; i <= n; i++) {
dp[i][1] = dp[i - 1][2];
dp[i][2] = dp[i - 2][1] + dp[i - 2][2];
}
return dp[n][1] + dp[n][2];
}
public static void main(String[] args) {
int n = 9;
int res = climbingStairsConstraintDP(n);
System.out.println(String.format("There are %d solutions to climb %d stairs", n, res));
}
}

View file

@ -0,0 +1,31 @@
/**
* File: climbing_stairs_dfs.java
* Created Time: 2023-06-30
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
public class climbing_stairs_dfs {
/* Search */
public static int dfs(int i) {
// Known dp[1] and dp[2], return them
if (i == 1 || i == 2)
return i;
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1) + dfs(i - 2);
return count;
}
/* Climbing stairs: Search */
public static int climbingStairsDFS(int n) {
return dfs(n);
}
public static void main(String[] args) {
int n = 9;
int res = climbingStairsDFS(n);
System.out.println(String.format("There are %d solutions to climb %d stairs", n, res));
}
}

View file

@ -0,0 +1,41 @@
/**
* File: climbing_stairs_dfs_mem.java
* Created Time: 2023-06-30
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
import java.util.Arrays;
public class climbing_stairs_dfs_mem {
/* Memoized search */
public static int dfs(int i, int[] mem) {
// Known dp[1] and dp[2], return them
if (i == 1 || i == 2)
return i;
// If there is a record for dp[i], return it
if (mem[i] != -1)
return mem[i];
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1, mem) + dfs(i - 2, mem);
// Record dp[i]
mem[i] = count;
return count;
}
/* Climbing stairs: Memoized search */
public static int climbingStairsDFSMem(int n) {
// mem[i] records the total number of solutions for climbing to the ith step, -1 means no record
int[] mem = new int[n + 1];
Arrays.fill(mem, -1);
return dfs(n, mem);
}
public static void main(String[] args) {
int n = 9;
int res = climbingStairsDFSMem(n);
System.out.println(String.format("There are %d solutions to climb %d stairs", n, res));
}
}

View file

@ -0,0 +1,48 @@
/**
* File: climbing_stairs_dp.java
* Created Time: 2023-06-30
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
public class climbing_stairs_dp {
/* Climbing stairs: Dynamic programming */
public static int climbingStairsDP(int n) {
if (n == 1 || n == 2)
return n;
// Initialize dp table, used to store subproblem solutions
int[] dp = new int[n + 1];
// Initial state: preset the smallest subproblem solution
dp[1] = 1;
dp[2] = 2;
// State transition: gradually solve larger subproblems from smaller ones
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
/* Climbing stairs: Space-optimized dynamic programming */
public static int climbingStairsDPComp(int n) {
if (n == 1 || n == 2)
return n;
int a = 1, b = 2;
for (int i = 3; i <= n; i++) {
int tmp = b;
b = a + b;
a = tmp;
}
return b;
}
public static void main(String[] args) {
int n = 9;
int res = climbingStairsDP(n);
System.out.println(String.format("There are %d solutions to climb %d stairs", n, res));
res = climbingStairsDPComp(n);
System.out.println(String.format("There are %d solutions to climb %d stairs", n, res));
}
}

View file

@ -0,0 +1,72 @@
/**
* File: coin_change.java
* Created Time: 2023-07-11
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
import java.util.Arrays;
public class coin_change {
/* Coin change: Dynamic programming */
static int coinChangeDP(int[] coins, int amt) {
int n = coins.length;
int MAX = amt + 1;
// Initialize dp table
int[][] dp = new int[n + 1][amt + 1];
// State transition: first row and first column
for (int a = 1; a <= amt; a++) {
dp[0][a] = MAX;
}
// State transition: the rest of the rows and columns
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {
// If exceeding the target amount, do not choose coin i
dp[i][a] = dp[i - 1][a];
} else {
// The smaller value between not choosing and choosing coin i
dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);
}
}
}
return dp[n][amt] != MAX ? dp[n][amt] : -1;
}
/* Coin change: Space-optimized dynamic programming */
static int coinChangeDPComp(int[] coins, int amt) {
int n = coins.length;
int MAX = amt + 1;
// Initialize dp table
int[] dp = new int[amt + 1];
Arrays.fill(dp, MAX);
dp[0] = 0;
// State transition
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {
// If exceeding the target amount, do not choose coin i
dp[a] = dp[a];
} else {
// The smaller value between not choosing and choosing coin i
dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);
}
}
}
return dp[amt] != MAX ? dp[amt] : -1;
}
public static void main(String[] args) {
int[] coins = { 1, 2, 5 };
int amt = 4;
// Dynamic programming
int res = coinChangeDP(coins, amt);
System.out.println("The minimum number of coins required to make up the target amount is " + res);
// Space-optimized dynamic programming
res = coinChangeDPComp(coins, amt);
System.out.println("The minimum number of coins required to make up the target amount is " + res);
}
}

View file

@ -0,0 +1,67 @@
/**
* File: coin_change_ii.java
* Created Time: 2023-07-11
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
public class coin_change_ii {
/* Coin change II: Dynamic programming */
static int coinChangeIIDP(int[] coins, int amt) {
int n = coins.length;
// Initialize dp table
int[][] dp = new int[n + 1][amt + 1];
// Initialize first column
for (int i = 0; i <= n; i++) {
dp[i][0] = 1;
}
// State transition
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {
// If exceeding the target amount, do not choose coin i
dp[i][a] = dp[i - 1][a];
} else {
// The sum of the two options of not choosing and choosing coin i
dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];
}
}
}
return dp[n][amt];
}
/* Coin change II: Space-optimized dynamic programming */
static int coinChangeIIDPComp(int[] coins, int amt) {
int n = coins.length;
// Initialize dp table
int[] dp = new int[amt + 1];
dp[0] = 1;
// State transition
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {
// If exceeding the target amount, do not choose coin i
dp[a] = dp[a];
} else {
// The sum of the two options of not choosing and choosing coin i
dp[a] = dp[a] + dp[a - coins[i - 1]];
}
}
}
return dp[amt];
}
public static void main(String[] args) {
int[] coins = { 1, 2, 5 };
int amt = 5;
// Dynamic programming
int res = coinChangeIIDP(coins, amt);
System.out.println("The number of coin combinations to make up the target amount is " + res);
// Space-optimized dynamic programming
res = coinChangeIIDPComp(coins, amt);
System.out.println("The number of coin combinations to make up the target amount is " + res);
}
}

View file

@ -0,0 +1,139 @@
/**
* File: edit_distance.java
* Created Time: 2023-07-13
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
import java.util.Arrays;
public class edit_distance {
/* Edit distance: Brute force search */
static int editDistanceDFS(String s, String t, int i, int j) {
// If both s and t are empty, return 0
if (i == 0 && j == 0)
return 0;
// If s is empty, return the length of t
if (i == 0)
return j;
// If t is empty, return the length of s
if (j == 0)
return i;
// If the two characters are equal, skip these two characters
if (s.charAt(i - 1) == t.charAt(j - 1))
return editDistanceDFS(s, t, i - 1, j - 1);
// The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
int insert = editDistanceDFS(s, t, i, j - 1);
int delete = editDistanceDFS(s, t, i - 1, j);
int replace = editDistanceDFS(s, t, i - 1, j - 1);
// Return the minimum number of edits
return Math.min(Math.min(insert, delete), replace) + 1;
}
/* Edit distance: Memoized search */
static int editDistanceDFSMem(String s, String t, int[][] mem, int i, int j) {
// If both s and t are empty, return 0
if (i == 0 && j == 0)
return 0;
// If s is empty, return the length of t
if (i == 0)
return j;
// If t is empty, return the length of s
if (j == 0)
return i;
// If there is a record, return it
if (mem[i][j] != -1)
return mem[i][j];
// If the two characters are equal, skip these two characters
if (s.charAt(i - 1) == t.charAt(j - 1))
return editDistanceDFSMem(s, t, mem, i - 1, j - 1);
// The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
int insert = editDistanceDFSMem(s, t, mem, i, j - 1);
int delete = editDistanceDFSMem(s, t, mem, i - 1, j);
int replace = editDistanceDFSMem(s, t, mem, i - 1, j - 1);
// Record and return the minimum number of edits
mem[i][j] = Math.min(Math.min(insert, delete), replace) + 1;
return mem[i][j];
}
/* Edit distance: Dynamic programming */
static int editDistanceDP(String s, String t) {
int n = s.length(), m = t.length();
int[][] dp = new int[n + 1][m + 1];
// State transition: first row and first column
for (int i = 1; i <= n; i++) {
dp[i][0] = i;
}
for (int j = 1; j <= m; j++) {
dp[0][j] = j;
}
// State transition: the rest of the rows and columns
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s.charAt(i - 1) == t.charAt(j - 1)) {
// If the two characters are equal, skip these two characters
dp[i][j] = dp[i - 1][j - 1];
} else {
// The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;
}
}
}
return dp[n][m];
}
/* Edit distance: Space-optimized dynamic programming */
static int editDistanceDPComp(String s, String t) {
int n = s.length(), m = t.length();
int[] dp = new int[m + 1];
// State transition: first row
for (int j = 1; j <= m; j++) {
dp[j] = j;
}
// State transition: the rest of the rows
for (int i = 1; i <= n; i++) {
// State transition: first column
int leftup = dp[0]; // Temporarily store dp[i-1, j-1]
dp[0] = i;
// State transition: the rest of the columns
for (int j = 1; j <= m; j++) {
int temp = dp[j];
if (s.charAt(i - 1) == t.charAt(j - 1)) {
// If the two characters are equal, skip these two characters
dp[j] = leftup;
} else {
// The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;
}
leftup = temp; // Update for the next round of dp[i-1, j-1]
}
}
return dp[m];
}
public static void main(String[] args) {
String s = "bag";
String t = "pack";
int n = s.length(), m = t.length();
// Brute force search
int res = editDistanceDFS(s, t, n, m);
System.out.println("Changing " + s + " to " + t + " requires a minimum of " + res + " edits");
// Memoized search
int[][] mem = new int[n + 1][m + 1];
for (int[] row : mem)
Arrays.fill(row, -1);
res = editDistanceDFSMem(s, t, mem, n, m);
System.out.println("Changing " + s + " to " + t + " requires a minimum of " + res + " edits");
// Dynamic programming
res = editDistanceDP(s, t);
System.out.println("Changing " + s + " to " + t + " requires a minimum of " + res + " edits");
// Space-optimized dynamic programming
res = editDistanceDPComp(s, t);
System.out.println("Changing " + s + " to " + t + " requires a minimum of " + res + " edits");
}
}

View file

@ -0,0 +1,116 @@
/**
* File: knapsack.java
* Created Time: 2023-07-10
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
import java.util.Arrays;
public class knapsack {
/* 0-1 Knapsack: Brute force search */
static int knapsackDFS(int[] wgt, int[] val, int i, int c) {
// If all items have been chosen or the knapsack has no remaining capacity, return value 0
if (i == 0 || c == 0) {
return 0;
}
// If exceeding the knapsack capacity, can only choose not to put it in the knapsack
if (wgt[i - 1] > c) {
return knapsackDFS(wgt, val, i - 1, c);
}
// Calculate the maximum value of not putting in and putting in item i
int no = knapsackDFS(wgt, val, i - 1, c);
int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];
// Return the greater value of the two options
return Math.max(no, yes);
}
/* 0-1 Knapsack: Memoized search */
static int knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {
// If all items have been chosen or the knapsack has no remaining capacity, return value 0
if (i == 0 || c == 0) {
return 0;
}
// If there is a record, return it
if (mem[i][c] != -1) {
return mem[i][c];
}
// If exceeding the knapsack capacity, can only choose not to put it in the knapsack
if (wgt[i - 1] > c) {
return knapsackDFSMem(wgt, val, mem, i - 1, c);
}
// Calculate the maximum value of not putting in and putting in item i
int no = knapsackDFSMem(wgt, val, mem, i - 1, c);
int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];
// Record and return the greater value of the two options
mem[i][c] = Math.max(no, yes);
return mem[i][c];
}
/* 0-1 Knapsack: Dynamic programming */
static int knapsackDP(int[] wgt, int[] val, int cap) {
int n = wgt.length;
// Initialize dp table
int[][] dp = new int[n + 1][cap + 1];
// State transition
for (int i = 1; i <= n; i++) {
for (int c = 1; c <= cap; c++) {
if (wgt[i - 1] > c) {
// If exceeding the knapsack capacity, do not choose item i
dp[i][c] = dp[i - 1][c];
} else {
// The greater value between not choosing and choosing item i
dp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);
}
}
}
return dp[n][cap];
}
/* 0-1 Knapsack: Space-optimized dynamic programming */
static int knapsackDPComp(int[] wgt, int[] val, int cap) {
int n = wgt.length;
// Initialize dp table
int[] dp = new int[cap + 1];
// State transition
for (int i = 1; i <= n; i++) {
// Traverse in reverse order
for (int c = cap; c >= 1; c--) {
if (wgt[i - 1] <= c) {
// The greater value between not choosing and choosing item i
dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
}
}
}
return dp[cap];
}
public static void main(String[] args) {
int[] wgt = { 10, 20, 30, 40, 50 };
int[] val = { 50, 120, 150, 210, 240 };
int cap = 50;
int n = wgt.length;
// Brute force search
int res = knapsackDFS(wgt, val, n, cap);
System.out.println("The maximum value within the bag capacity is " + res);
// Memoized search
int[][] mem = new int[n + 1][cap + 1];
for (int[] row : mem) {
Arrays.fill(row, -1);
}
res = knapsackDFSMem(wgt, val, mem, n, cap);
System.out.println("The maximum value within the bag capacity is " + res);
// Dynamic programming
res = knapsackDP(wgt, val, cap);
System.out.println("The maximum value within the bag capacity is " + res);
// Space-optimized dynamic programming
res = knapsackDPComp(wgt, val, cap);
System.out.println("The maximum value within the bag capacity is " + res);
}
}

View file

@ -0,0 +1,53 @@
/**
* File: min_cost_climbing_stairs_dp.java
* Created Time: 2023-06-30
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
import java.util.Arrays;
public class min_cost_climbing_stairs_dp {
/* Climbing stairs with minimum cost: Dynamic programming */
public static int minCostClimbingStairsDP(int[] cost) {
int n = cost.length - 1;
if (n == 1 || n == 2)
return cost[n];
// Initialize dp table, used to store subproblem solutions
int[] dp = new int[n + 1];
// Initial state: preset the smallest subproblem solution
dp[1] = cost[1];
dp[2] = cost[2];
// State transition: gradually solve larger subproblems from smaller ones
for (int i = 3; i <= n; i++) {
dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];
}
return dp[n];
}
/* Climbing stairs with minimum cost: Space-optimized dynamic programming */
public static int minCostClimbingStairsDPComp(int[] cost) {
int n = cost.length - 1;
if (n == 1 || n == 2)
return cost[n];
int a = cost[1], b = cost[2];
for (int i = 3; i <= n; i++) {
int tmp = b;
b = Math.min(a, tmp) + cost[i];
a = tmp;
}
return b;
}
public static void main(String[] args) {
int[] cost = { 0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1 };
System.out.println(String.format("Input the cost list for stairs as %s", Arrays.toString(cost)));
int res = minCostClimbingStairsDP(cost);
System.out.println(String.format("Minimum cost to climb the stairs %d", res));
res = minCostClimbingStairsDPComp(cost);
System.out.println(String.format("Minimum cost to climb the stairs %d", res));
}
}

View file

@ -0,0 +1,125 @@
/**
* File: min_path_sum.java
* Created Time: 2023-07-10
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
import java.util.Arrays;
public class min_path_sum {
/* Minimum path sum: Brute force search */
static int minPathSumDFS(int[][] grid, int i, int j) {
// If it's the top-left cell, terminate the search
if (i == 0 && j == 0) {
return grid[0][0];
}
// If the row or column index is out of bounds, return a + cost
if (i < 0 || j < 0) {
return Integer.MAX_VALUE;
}
// Calculate the minimum path cost from the top-left to (i-1, j) and (i, j-1)
int up = minPathSumDFS(grid, i - 1, j);
int left = minPathSumDFS(grid, i, j - 1);
// Return the minimum path cost from the top-left to (i, j)
return Math.min(left, up) + grid[i][j];
}
/* Minimum path sum: Memoized search */
static int minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {
// If it's the top-left cell, terminate the search
if (i == 0 && j == 0) {
return grid[0][0];
}
// If the row or column index is out of bounds, return a + cost
if (i < 0 || j < 0) {
return Integer.MAX_VALUE;
}
// If there is a record, return it
if (mem[i][j] != -1) {
return mem[i][j];
}
// The minimum path cost from the left and top cells
int up = minPathSumDFSMem(grid, mem, i - 1, j);
int left = minPathSumDFSMem(grid, mem, i, j - 1);
// Record and return the minimum path cost from the top-left to (i, j)
mem[i][j] = Math.min(left, up) + grid[i][j];
return mem[i][j];
}
/* Minimum path sum: Dynamic programming */
static int minPathSumDP(int[][] grid) {
int n = grid.length, m = grid[0].length;
// Initialize dp table
int[][] dp = new int[n][m];
dp[0][0] = grid[0][0];
// State transition: first row
for (int j = 1; j < m; j++) {
dp[0][j] = dp[0][j - 1] + grid[0][j];
}
// State transition: first column
for (int i = 1; i < n; i++) {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
// State transition: the rest of the rows and columns
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];
}
}
return dp[n - 1][m - 1];
}
/* Minimum path sum: Space-optimized dynamic programming */
static int minPathSumDPComp(int[][] grid) {
int n = grid.length, m = grid[0].length;
// Initialize dp table
int[] dp = new int[m];
// State transition: first row
dp[0] = grid[0][0];
for (int j = 1; j < m; j++) {
dp[j] = dp[j - 1] + grid[0][j];
}
// State transition: the rest of the rows
for (int i = 1; i < n; i++) {
// State transition: first column
dp[0] = dp[0] + grid[i][0];
// State transition: the rest of the columns
for (int j = 1; j < m; j++) {
dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];
}
}
return dp[m - 1];
}
public static void main(String[] args) {
int[][] grid = {
{ 1, 3, 1, 5 },
{ 2, 2, 4, 2 },
{ 5, 3, 2, 1 },
{ 4, 3, 5, 2 }
};
int n = grid.length, m = grid[0].length;
// Brute force search
int res = minPathSumDFS(grid, n - 1, m - 1);
System.out.println("The minimum path sum from the top left corner to the bottom right corner is " + res);
// Memoized search
int[][] mem = new int[n][m];
for (int[] row : mem) {
Arrays.fill(row, -1);
}
res = minPathSumDFSMem(grid, mem, n - 1, m - 1);
System.out.println("The minimum path sum from the top left corner to the bottom right corner is " + res);
// Dynamic programming
res = minPathSumDP(grid);
System.out.println("The minimum path sum from the top left corner to the bottom right corner is " + res);
// Space-optimized dynamic programming
res = minPathSumDPComp(grid);
System.out.println("The minimum path sum from the top left corner to the bottom right corner is " + res);
}
}

View file

@ -0,0 +1,63 @@
/**
* File: unbounded_knapsack.java
* Created Time: 2023-07-11
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
public class unbounded_knapsack {
/* Complete knapsack: Dynamic programming */
static int unboundedKnapsackDP(int[] wgt, int[] val, int cap) {
int n = wgt.length;
// Initialize dp table
int[][] dp = new int[n + 1][cap + 1];
// State transition
for (int i = 1; i <= n; i++) {
for (int c = 1; c <= cap; c++) {
if (wgt[i - 1] > c) {
// If exceeding the knapsack capacity, do not choose item i
dp[i][c] = dp[i - 1][c];
} else {
// The greater value between not choosing and choosing item i
dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);
}
}
}
return dp[n][cap];
}
/* Complete knapsack: Space-optimized dynamic programming */
static int unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
int n = wgt.length;
// Initialize dp table
int[] dp = new int[cap + 1];
// State transition
for (int i = 1; i <= n; i++) {
for (int c = 1; c <= cap; c++) {
if (wgt[i - 1] > c) {
// If exceeding the knapsack capacity, do not choose item i
dp[c] = dp[c];
} else {
// The greater value between not choosing and choosing item i
dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
}
}
}
return dp[cap];
}
public static void main(String[] args) {
int[] wgt = { 1, 2, 3 };
int[] val = { 5, 11, 15 };
int cap = 4;
// Dynamic programming
int res = unboundedKnapsackDP(wgt, val, cap);
System.out.println("The maximum value within the bag capacity is " + res);
// Space-optimized dynamic programming
res = unboundedKnapsackDPComp(wgt, val, cap);
System.out.println("The maximum value within the bag capacity is " + res);
}
}

View file

@ -0,0 +1,117 @@
/**
* File: graph_adjacency_list.java
* Created Time: 2023-01-26
* Author: krahets (krahets@163.com)
*/
package chapter_graph;
import java.util.*;
import utils.*;
/* Undirected graph class based on adjacency list */
class GraphAdjList {
// Adjacency list, key: vertex, value: all adjacent vertices of that vertex
Map<Vertex, List<Vertex>> adjList;
/* Constructor */
public GraphAdjList(Vertex[][] edges) {
this.adjList = new HashMap<>();
// Add all vertices and edges
for (Vertex[] edge : edges) {
addVertex(edge[0]);
addVertex(edge[1]);
addEdge(edge[0], edge[1]);
}
}
/* Get the number of vertices */
public int size() {
return adjList.size();
}
/* Add edge */
public void addEdge(Vertex vet1, Vertex vet2) {
if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)
throw new IllegalArgumentException();
// Add edge vet1 - vet2
adjList.get(vet1).add(vet2);
adjList.get(vet2).add(vet1);
}
/* Remove edge */
public void removeEdge(Vertex vet1, Vertex vet2) {
if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)
throw new IllegalArgumentException();
// Remove edge vet1 - vet2
adjList.get(vet1).remove(vet2);
adjList.get(vet2).remove(vet1);
}
/* Add vertex */
public void addVertex(Vertex vet) {
if (adjList.containsKey(vet))
return;
// Add a new linked list to the adjacency list
adjList.put(vet, new ArrayList<>());
}
/* Remove vertex */
public void removeVertex(Vertex vet) {
if (!adjList.containsKey(vet))
throw new IllegalArgumentException();
// Remove the vertex vet's corresponding linked list from the adjacency list
adjList.remove(vet);
// Traverse other vertices' linked lists, removing all edges containing vet
for (List<Vertex> list : adjList.values()) {
list.remove(vet);
}
}
/* Print the adjacency list */
public void print() {
System.out.println("Adjacency list =");
for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {
List<Integer> tmp = new ArrayList<>();
for (Vertex vertex : pair.getValue())
tmp.add(vertex.val);
System.out.println(pair.getKey().val + ": " + tmp + ",");
}
}
}
public class graph_adjacency_list {
public static void main(String[] args) {
/* Initialize undirected graph */
Vertex[] v = Vertex.valsToVets(new int[] { 1, 3, 2, 5, 4 });
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] },
{ v[2], v[3] }, { v[2], v[4] }, { v[3], v[4] } };
GraphAdjList graph = new GraphAdjList(edges);
System.out.println("\nAfter initialization, the graph is");
graph.print();
/* Add edge */
// Vertices 1, 2 i.e., v[0], v[2]
graph.addEdge(v[0], v[2]);
System.out.println("\nAfter adding edge 1-2, the graph is");
graph.print();
/* Remove edge */
// Vertices 1, 3 i.e., v[0], v[1]
graph.removeEdge(v[0], v[1]);
System.out.println("\nAfter removing edge 1-3, the graph is");
graph.print();
/* Add vertex */
Vertex v5 = new Vertex(6);
graph.addVertex(v5);
System.out.println("\nAfter adding vertex 6, the graph is");
graph.print();
/* Remove vertex */
// Vertex 3 i.e., v[1]
graph.removeVertex(v[1]);
System.out.println("\nAfter removing vertex 3, the graph is");
graph.print();
}
}

View file

@ -0,0 +1,131 @@
/**
* File: graph_adjacency_matrix.java
* Created Time: 2023-01-26
* Author: krahets (krahets@163.com)
*/
package chapter_graph;
import utils.*;
import java.util.*;
/* Undirected graph class based on adjacency matrix */
class GraphAdjMat {
List<Integer> vertices; // Vertex list, elements represent "vertex value", index represents "vertex index"
List<List<Integer>> adjMat; // Adjacency matrix, row and column indices correspond to "vertex index"
/* Constructor */
public GraphAdjMat(int[] vertices, int[][] edges) {
this.vertices = new ArrayList<>();
this.adjMat = new ArrayList<>();
// Add vertex
for (int val : vertices) {
addVertex(val);
}
// Add edge
// Please note, edges elements represent vertex indices, corresponding to vertices elements indices
for (int[] e : edges) {
addEdge(e[0], e[1]);
}
}
/* Get the number of vertices */
public int size() {
return vertices.size();
}
/* Add vertex */
public void addVertex(int val) {
int n = size();
// Add new vertex value to the vertex list
vertices.add(val);
// Add a row to the adjacency matrix
List<Integer> newRow = new ArrayList<>(n);
for (int j = 0; j < n; j++) {
newRow.add(0);
}
adjMat.add(newRow);
// Add a column to the adjacency matrix
for (List<Integer> row : adjMat) {
row.add(0);
}
}
/* Remove vertex */
public void removeVertex(int index) {
if (index >= size())
throw new IndexOutOfBoundsException();
// Remove vertex at `index` from the vertex list
vertices.remove(index);
// Remove the row at `index` from the adjacency matrix
adjMat.remove(index);
// Remove the column at `index` from the adjacency matrix
for (List<Integer> row : adjMat) {
row.remove(index);
}
}
/* Add edge */
// Parameters i, j correspond to vertices element indices
public void addEdge(int i, int j) {
// Handle index out of bounds and equality
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
throw new IndexOutOfBoundsException();
// In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., satisfies (i, j) == (j, i)
adjMat.get(i).set(j, 1);
adjMat.get(j).set(i, 1);
}
/* Remove edge */
// Parameters i, j correspond to vertices element indices
public void removeEdge(int i, int j) {
// Handle index out of bounds and equality
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
throw new IndexOutOfBoundsException();
adjMat.get(i).set(j, 0);
adjMat.get(j).set(i, 0);
}
/* Print adjacency matrix */
public void print() {
System.out.print("Vertex list = ");
System.out.println(vertices);
System.out.println("Adjacency matrix =");
PrintUtil.printMatrix(adjMat);
}
}
public class graph_adjacency_matrix {
public static void main(String[] args) {
/* Initialize undirected graph */
// Please note, edges elements represent vertex indices, corresponding to vertices elements indices
int[] vertices = { 1, 3, 2, 5, 4 };
int[][] edges = { { 0, 1 }, { 0, 3 }, { 1, 2 }, { 2, 3 }, { 2, 4 }, { 3, 4 } };
GraphAdjMat graph = new GraphAdjMat(vertices, edges);
System.out.println("\nAfter initialization, the graph is");
graph.print();
/* Add edge */
// Indices of vertices 1, 2 are 0, 2 respectively
graph.addEdge(0, 2);
System.out.println("\nAfter adding edge 1-2, the graph is");
graph.print();
/* Remove edge */
// Indices of vertices 1, 3 are 0, 1 respectively
graph.removeEdge(0, 1);
System.out.println("\nAfter removing edge 1-3, the graph is");
graph.print();
/* Add vertex */
graph.addVertex(6);
System.out.println("\nAfter adding vertex 6, the graph is");
graph.print();
/* Remove vertex */
// Index of vertex 3 is 1
graph.removeVertex(1);
System.out.println("\nAfter removing vertex 3, the graph is");
graph.print();
}
}

View file

@ -0,0 +1,55 @@
/**
* File: graph_bfs.java
* Created Time: 2023-02-12
* Author: krahets (krahets@163.com)
*/
package chapter_graph;
import java.util.*;
import utils.*;
public class graph_bfs {
/* Breadth-first traversal */
// Use adjacency list to represent the graph, to obtain all adjacent vertices of a specified vertex
static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
// Vertex traversal sequence
List<Vertex> res = new ArrayList<>();
// Hash set, used to record visited vertices
Set<Vertex> visited = new HashSet<>();
visited.add(startVet);
// Queue used to implement BFS
Queue<Vertex> que = new LinkedList<>();
que.offer(startVet);
// Starting from vertex vet, loop until all vertices are visited
while (!que.isEmpty()) {
Vertex vet = que.poll(); // Dequeue the vertex at the head of the queue
res.add(vet); // Record visited vertex
// Traverse all adjacent vertices of that vertex
for (Vertex adjVet : graph.adjList.get(vet)) {
if (visited.contains(adjVet))
continue; // Skip already visited vertices
que.offer(adjVet); // Only enqueue unvisited vertices
visited.add(adjVet); // Mark the vertex as visited
}
}
// Return the vertex traversal sequence
return res;
}
public static void main(String[] args) {
/* Initialize undirected graph */
Vertex[] v = Vertex.valsToVets(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] }, { v[1], v[4] },
{ v[2], v[5] }, { v[3], v[4] }, { v[3], v[6] }, { v[4], v[5] },
{ v[4], v[7] }, { v[5], v[8] }, { v[6], v[7] }, { v[7], v[8] } };
GraphAdjList graph = new GraphAdjList(edges);
System.out.println("\nAfter initialization, the graph is");
graph.print();
/* Breadth-first traversal */
List<Vertex> res = graphBFS(graph, v[0]);
System.out.println("\nBreadth-first traversal (BFS) vertex sequence is");
System.out.println(Vertex.vetsToVals(res));
}
}

View file

@ -0,0 +1,51 @@
/**
* File: graph_dfs.java
* Created Time: 2023-02-12
* Author: krahets (krahets@163.com)
*/
package chapter_graph;
import java.util.*;
import utils.*;
public class graph_dfs {
/* Depth-first traversal helper function */
static void dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {
res.add(vet); // Record visited vertex
visited.add(vet); // Mark the vertex as visited
// Traverse all adjacent vertices of that vertex
for (Vertex adjVet : graph.adjList.get(vet)) {
if (visited.contains(adjVet))
continue; // Skip already visited vertices
// Recursively visit adjacent vertices
dfs(graph, visited, res, adjVet);
}
}
/* Depth-first traversal */
// Use adjacency list to represent the graph, to obtain all adjacent vertices of a specified vertex
static List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
// Vertex traversal sequence
List<Vertex> res = new ArrayList<>();
// Hash set, used to record visited vertices
Set<Vertex> visited = new HashSet<>();
dfs(graph, visited, res, startVet);
return res;
}
public static void main(String[] args) {
/* Initialize undirected graph */
Vertex[] v = Vertex.valsToVets(new int[] { 0, 1, 2, 3, 4, 5, 6 });
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] },
{ v[2], v[5] }, { v[4], v[5] }, { v[5], v[6] } };
GraphAdjList graph = new GraphAdjList(edges);
System.out.println("\nAfter initialization, the graph is");
graph.print();
/* Depth-first traversal */
List<Vertex> res = graphDFS(graph, v[0]);
System.out.println("\nDepth-first traversal (DFS) vertex sequence is");
System.out.println(Vertex.vetsToVals(res));
}
}

View file

@ -0,0 +1,55 @@
/**
* File: coin_change_greedy.java
* Created Time: 2023-07-20
* Author: krahets (krahets@163.com)
*/
package chapter_greedy;
import java.util.Arrays;
public class coin_change_greedy {
/* Coin change: Greedy */
static int coinChangeGreedy(int[] coins, int amt) {
// Assume coins list is ordered
int i = coins.length - 1;
int count = 0;
// Loop for greedy selection until no remaining amount
while (amt > 0) {
// Find the smallest coin close to and less than the remaining amount
while (i > 0 && coins[i] > amt) {
i--;
}
// Choose coins[i]
amt -= coins[i];
count++;
}
// If no feasible solution is found, return -1
return amt == 0 ? count : -1;
}
public static void main(String[] args) {
// Greedy: can ensure finding a global optimal solution
int[] coins = { 1, 5, 10, 20, 50, 100 };
int amt = 186;
int res = coinChangeGreedy(coins, amt);
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
System.out.println("The minimum number of coins required to make up " + amt + " is " + res);
// Greedy: cannot ensure finding a global optimal solution
coins = new int[] { 1, 20, 50 };
amt = 60;
res = coinChangeGreedy(coins, amt);
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
System.out.println("The minimum number of coins required to make up " + amt + " is " + res);
System.out.println("In reality, the minimum number needed is 3, i.e., 20 + 20 + 20");
// Greedy: cannot ensure finding a global optimal solution
coins = new int[] { 1, 49, 50 };
amt = 98;
res = coinChangeGreedy(coins, amt);
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
System.out.println("The minimum number of coins required to make up " + amt + " is " + res);
System.out.println("In reality, the minimum number needed is 2, i.e., 49 + 49");
}
}

View file

@ -0,0 +1,59 @@
/**
* File: fractional_knapsack.java
* Created Time: 2023-07-20
* Author: krahets (krahets@163.com)
*/
package chapter_greedy;
import java.util.Arrays;
import java.util.Comparator;
/* Item */
class Item {
int w; // Item weight
int v; // Item value
public Item(int w, int v) {
this.w = w;
this.v = v;
}
}
public class fractional_knapsack {
/* Fractional knapsack: Greedy */
static double fractionalKnapsack(int[] wgt, int[] val, int cap) {
// Create an item list, containing two properties: weight, value
Item[] items = new Item[wgt.length];
for (int i = 0; i < wgt.length; i++) {
items[i] = new Item(wgt[i], val[i]);
}
// Sort by unit value item.v / item.w from high to low
Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));
// Loop for greedy selection
double res = 0;
for (Item item : items) {
if (item.w <= cap) {
// If the remaining capacity is sufficient, put the entire item into the knapsack
res += item.v;
cap -= item.w;
} else {
// If the remaining capacity is insufficient, put part of the item into the knapsack
res += (double) item.v / item.w * cap;
// No remaining capacity left, thus break the loop
break;
}
}
return res;
}
public static void main(String[] args) {
int[] wgt = { 10, 20, 30, 40, 50 };
int[] val = { 50, 120, 150, 210, 240 };
int cap = 50;
// Greedy algorithm
double res = fractionalKnapsack(wgt, val, cap);
System.out.println("The maximum value within the bag capacity is " + res);
}
}

View file

@ -0,0 +1,38 @@
/**
* File: max_capacity.java
* Created Time: 2023-07-21
* Author: krahets (krahets@163.com)
*/
package chapter_greedy;
public class max_capacity {
/* Maximum capacity: Greedy */
static int maxCapacity(int[] ht) {
// Initialize i, j, making them split the array at both ends
int i = 0, j = ht.length - 1;
// Initial maximum capacity is 0
int res = 0;
// Loop for greedy selection until the two boards meet
while (i < j) {
// Update maximum capacity
int cap = Math.min(ht[i], ht[j]) * (j - i);
res = Math.max(res, cap);
// Move the shorter board inward
if (ht[i] < ht[j]) {
i++;
} else {
j--;
}
}
return res;
}
public static void main(String[] args) {
int[] ht = { 3, 8, 5, 2, 7, 7, 3, 4 };
// Greedy algorithm
int res = maxCapacity(ht);
System.out.println("The maximum capacity is " + res);
}
}

View file

@ -0,0 +1,40 @@
/**
* File: max_product_cutting.java
* Created Time: 2023-07-21
* Author: krahets (krahets@163.com)
*/
package chapter_greedy;
import java.lang.Math;
public class max_product_cutting {
/* Maximum product of cutting: Greedy */
public static int maxProductCutting(int n) {
// When n <= 3, must cut out a 1
if (n <= 3) {
return 1 * (n - 1);
}
// Greedy cut out 3s, a is the number of 3s, b is the remainder
int a = n / 3;
int b = n % 3;
if (b == 1) {
// When the remainder is 1, convert a pair of 1 * 3 into 2 * 2
return (int) Math.pow(3, a - 1) * 2 * 2;
}
if (b == 2) {
// When the remainder is 2, do nothing
return (int) Math.pow(3, a) * 2;
}
// When the remainder is 0, do nothing
return (int) Math.pow(3, a);
}
public static void main(String[] args) {
int n = 58;
// Greedy algorithm
int res = maxProductCutting(n);
System.out.println("The maximum product of division is " + res);
}
}

View file

@ -0,0 +1,141 @@
/**
* File: array_hash_map.java
* Created Time: 2022-12-04
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
import java.util.*;
/* Key-value pair */
class Pair {
public int key;
public String val;
public Pair(int key, String val) {
this.key = key;
this.val = val;
}
}
/* Hash table based on array implementation */
class ArrayHashMap {
private List<Pair> buckets;
public ArrayHashMap() {
// Initialize an array, containing 100 buckets
buckets = new ArrayList<>();
for (int i = 0; i < 100; i++) {
buckets.add(null);
}
}
/* Hash function */
private int hashFunc(int key) {
int index = key % 100;
return index;
}
/* Query operation */
public String get(int key) {
int index = hashFunc(key);
Pair pair = buckets.get(index);
if (pair == null)
return null;
return pair.val;
}
/* Add operation */
public void put(int key, String val) {
Pair pair = new Pair(key, val);
int index = hashFunc(key);
buckets.set(index, pair);
}
/* Remove operation */
public void remove(int key) {
int index = hashFunc(key);
// Set to null, indicating removal
buckets.set(index, null);
}
/* Get all key-value pairs */
public List<Pair> pairSet() {
List<Pair> pairSet = new ArrayList<>();
for (Pair pair : buckets) {
if (pair != null)
pairSet.add(pair);
}
return pairSet;
}
/* Get all keys */
public List<Integer> keySet() {
List<Integer> keySet = new ArrayList<>();
for (Pair pair : buckets) {
if (pair != null)
keySet.add(pair.key);
}
return keySet;
}
/* Get all values */
public List<String> valueSet() {
List<String> valueSet = new ArrayList<>();
for (Pair pair : buckets) {
if (pair != null)
valueSet.add(pair.val);
}
return valueSet;
}
/* Print hash table */
public void print() {
for (Pair kv : pairSet()) {
System.out.println(kv.key + " -> " + kv.val);
}
}
}
public class array_hash_map {
public static void main(String[] args) {
/* Initialize hash table */
ArrayHashMap map = new ArrayHashMap();
/* Add operation */
// Add key-value pair (key, value) to the hash table
map.put(12836, "Ha");
map.put(15937, "Luo");
map.put(16750, "Suan");
map.put(13276, "Fa");
map.put(10583, "Ya");
System.out.println("\nAfter adding, the hash table is\nKey -> Value");
map.print();
/* Query operation */
// Enter key to the hash table, get value
String name = map.get(15937);
System.out.println("\nEnter student ID 15937, found name " + name);
/* Remove operation */
// Remove key-value pair (key, value) from the hash table
map.remove(10583);
System.out.println("\nAfter removing 10583, the hash table is\nKey -> Value");
map.print();
/* Traverse hash table */
System.out.println("\nTraverse key-value pairs Key->Value");
for (Pair kv : map.pairSet()) {
System.out.println(kv.key + " -> " + kv.val);
}
System.out.println("\nIndividually traverse keys Key");
for (int key : map.keySet()) {
System.out.println(key);
}
System.out.println("\nIndividually traverse values Value");
for (String val : map.valueSet()) {
System.out.println(val);
}
}
}

View file

@ -0,0 +1,38 @@
/**
* File: built_in_hash.java
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
import utils.*;
import java.util.*;
public class built_in_hash {
public static void main(String[] args) {
int num = 3;
int hashNum = Integer.hashCode(num);
System.out.println("The hash value of integer " + num + " is " + hashNum);
boolean bol = true;
int hashBol = Boolean.hashCode(bol);
System.out.println("The hash value of boolean " + bol + " is " + hashBol);
double dec = 3.14159;
int hashDec = Double.hashCode(dec);
System.out.println("The hash value of decimal " + dec + " is " + hashDec);
String str = "Hello algorithm";
int hashStr = str.hashCode();
System.out.println("The hash value of string " + str + " is " + hashStr);
Object[] arr = { 12836, "Ha" };
int hashTup = Arrays.hashCode(arr);
System.out.println("The hash value of array " + Arrays.toString(arr) + " is " + hashTup);
ListNode obj = new ListNode(0);
int hashObj = obj.hashCode();
System.out.println("The hash value of node object " + obj + " is " + hashObj);
}
}

View file

@ -0,0 +1,52 @@
/**
* File: hash_map.java
* Created Time: 2022-12-04
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
import java.util.*;
import utils.*;
public class hash_map {
public static void main(String[] args) {
/* Initialize hash table */
Map<Integer, String> map = new HashMap<>();
/* Add operation */
// Add key-value pair (key, value) to the hash table
map.put(12836, "Ha");
map.put(15937, "Luo");
map.put(16750, "Suan");
map.put(13276, "Fa");
map.put(10583, "Ya");
System.out.println("\nAfter adding, the hash table is\nKey -> Value");
PrintUtil.printHashMap(map);
/* Query operation */
// Enter key to the hash table, get value
String name = map.get(15937);
System.out.println("\nEnter student ID 15937, found name " + name);
/* Remove operation */
// Remove key-value pair (key, value) from the hash table
map.remove(10583);
System.out.println("\nAfter removing 10583, the hash table is\nKey -> Value");
PrintUtil.printHashMap(map);
/* Traverse hash table */
System.out.println("\nTraverse key-value pairs Key->Value");
for (Map.Entry<Integer, String> kv : map.entrySet()) {
System.out.println(kv.getKey() + " -> " + kv.getValue());
}
System.out.println("\nIndividually traverse keys Key");
for (int key : map.keySet()) {
System.out.println(key);
}
System.out.println("\nIndividually traverse values Value");
for (String val : map.values()) {
System.out.println(val);
}
}
}

View file

@ -0,0 +1,148 @@
/**
* File: hash_map_chaining.java
* Created Time: 2023-06-13
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
import java.util.ArrayList;
import java.util.List;
/* Chained address hash table */
class HashMapChaining {
int size; // Number of key-value pairs
int capacity; // Hash table capacity
double loadThres; // Load factor threshold for triggering expansion
int extendRatio; // Expansion multiplier
List<List<Pair>> buckets; // Bucket array
/* Constructor */
public HashMapChaining() {
size = 0;
capacity = 4;
loadThres = 2.0 / 3.0;
extendRatio = 2;
buckets = new ArrayList<>(capacity);
for (int i = 0; i < capacity; i++) {
buckets.add(new ArrayList<>());
}
}
/* Hash function */
int hashFunc(int key) {
return key % capacity;
}
/* Load factor */
double loadFactor() {
return (double) size / capacity;
}
/* Query operation */
String get(int key) {
int index = hashFunc(key);
List<Pair> bucket = buckets.get(index);
// Traverse the bucket, if the key is found, return the corresponding val
for (Pair pair : bucket) {
if (pair.key == key) {
return pair.val;
}
}
// If key is not found, return null
return null;
}
/* Add operation */
void put(int key, String val) {
// When the load factor exceeds the threshold, perform expansion
if (loadFactor() > loadThres) {
extend();
}
int index = hashFunc(key);
List<Pair> bucket = buckets.get(index);
// Traverse the bucket, if the specified key is encountered, update the corresponding val and return
for (Pair pair : bucket) {
if (pair.key == key) {
pair.val = val;
return;
}
}
// If the key is not found, add the key-value pair to the end
Pair pair = new Pair(key, val);
bucket.add(pair);
size++;
}
/* Remove operation */
void remove(int key) {
int index = hashFunc(key);
List<Pair> bucket = buckets.get(index);
// Traverse the bucket, remove the key-value pair from it
for (Pair pair : bucket) {
if (pair.key == key) {
bucket.remove(pair);
size--;
break;
}
}
}
/* Extend hash table */
void extend() {
// Temporarily store the original hash table
List<List<Pair>> bucketsTmp = buckets;
// Initialize the extended new hash table
capacity *= extendRatio;
buckets = new ArrayList<>(capacity);
for (int i = 0; i < capacity; i++) {
buckets.add(new ArrayList<>());
}
size = 0;
// Move key-value pairs from the original hash table to the new hash table
for (List<Pair> bucket : bucketsTmp) {
for (Pair pair : bucket) {
put(pair.key, pair.val);
}
}
}
/* Print hash table */
void print() {
for (List<Pair> bucket : buckets) {
List<String> res = new ArrayList<>();
for (Pair pair : bucket) {
res.add(pair.key + " -> " + pair.val);
}
System.out.println(res);
}
}
}
public class hash_map_chaining {
public static void main(String[] args) {
/* Initialize hash table */
HashMapChaining map = new HashMapChaining();
/* Add operation */
// Add key-value pair (key, value) to the hash table
map.put(12836, "Ha");
map.put(15937, "Luo");
map.put(16750, "Suan");
map.put(13276, "Fa");
map.put(10583, "Ya");
System.out.println("\nAfter adding, the hash table is\nKey -> Value");
map.print();
/* Query operation */
// Enter key to the hash table, get value
String name = map.get(13276);
System.out.println("\nEnter student ID 13276, found name " + name);
/* Remove operation */
// Remove key-value pair (key, value) from the hash table
map.remove(12836);
System.out.println("\nAfter removing 12836, the hash table is\nKey -> Value");
map.print();
}
}

View file

@ -0,0 +1,158 @@
/**
* File: hash_map_open_addressing.java
* Created Time: 2023-06-13
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
/* Open addressing hash table */
class HashMapOpenAddressing {
private int size; // Number of key-value pairs
private int capacity = 4; // Hash table capacity
private final double loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion
private final int extendRatio = 2; // Expansion multiplier
private Pair[] buckets; // Bucket array
private final Pair TOMBSTONE = new Pair(-1, "-1"); // Removal mark
/* Constructor */
public HashMapOpenAddressing() {
size = 0;
buckets = new Pair[capacity];
}
/* Hash function */
private int hashFunc(int key) {
return key % capacity;
}
/* Load factor */
private double loadFactor() {
return (double) size / capacity;
}
/* Search for the bucket index corresponding to key */
private int findBucket(int key) {
int index = hashFunc(key);
int firstTombstone = -1;
// Linear probing, break when encountering an empty bucket
while (buckets[index] != null) {
// If the key is encountered, return the corresponding bucket index
if (buckets[index].key == key) {
// If a removal mark was encountered earlier, move the key-value pair to that index
if (firstTombstone != -1) {
buckets[firstTombstone] = buckets[index];
buckets[index] = TOMBSTONE;
return firstTombstone; // Return the moved bucket index
}
return index; // Return bucket index
}
// Record the first encountered removal mark
if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {
firstTombstone = index;
}
// Calculate the bucket index, return to the head if exceeding the tail
index = (index + 1) % capacity;
}
// If the key does not exist, return the index of the insertion point
return firstTombstone == -1 ? index : firstTombstone;
}
/* Query operation */
public String get(int key) {
// Search for the bucket index corresponding to key
int index = findBucket(key);
// If the key-value pair is found, return the corresponding val
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
return buckets[index].val;
}
// If the key-value pair does not exist, return null
return null;
}
/* Add operation */
public void put(int key, String val) {
// When the load factor exceeds the threshold, perform expansion
if (loadFactor() > loadThres) {
extend();
}
// Search for the bucket index corresponding to key
int index = findBucket(key);
// If the key-value pair is found, overwrite val and return
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index].val = val;
return;
}
// If the key-value pair does not exist, add the key-value pair
buckets[index] = new Pair(key, val);
size++;
}
/* Remove operation */
public void remove(int key) {
// Search for the bucket index corresponding to key
int index = findBucket(key);
// If the key-value pair is found, cover it with a removal mark
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index] = TOMBSTONE;
size--;
}
}
/* Extend hash table */
private void extend() {
// Temporarily store the original hash table
Pair[] bucketsTmp = buckets;
// Initialize the extended new hash table
capacity *= extendRatio;
buckets = new Pair[capacity];
size = 0;
// Move key-value pairs from the original hash table to the new hash table
for (Pair pair : bucketsTmp) {
if (pair != null && pair != TOMBSTONE) {
put(pair.key, pair.val);
}
}
}
/* Print hash table */
public void print() {
for (Pair pair : buckets) {
if (pair == null) {
System.out.println("null");
} else if (pair == TOMBSTONE) {
System.out.println("TOMBSTONE");
} else {
System.out.println(pair.key + " -> " + pair.val);
}
}
}
}
public class hash_map_open_addressing {
public static void main(String[] args) {
// Initialize hash table
HashMapOpenAddressing hashmap = new HashMapOpenAddressing();
// Add operation
// Add key-value pair (key, val) to the hash table
hashmap.put(12836, "Ha");
hashmap.put(15937, "Luo");
hashmap.put(16750, "Suan");
hashmap.put(13276, "Fa");
hashmap.put(10583, "Ya");
System.out.println("\nAfter adding, the hash table is\nKey -> Value");
hashmap.print();
// Query operation
// Enter key to the hash table, get value val
String name = hashmap.get(13276);
System.out.println("\nEnter student ID 13276, found name " + name);
// Remove operation
// Remove key-value pair (key, val) from the hash table
hashmap.remove(16750);
System.out.println("\nAfter removing 16750, the hash table is\nKey -> Value");
hashmap.print();
}
}

View file

@ -0,0 +1,65 @@
/**
* File: simple_hash.java
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
public class simple_hash {
/* Additive hash */
static int addHash(String key) {
long hash = 0;
final int MODULUS = 1000000007;
for (char c : key.toCharArray()) {
hash = (hash + (int) c) % MODULUS;
}
return (int) hash;
}
/* Multiplicative hash */
static int mulHash(String key) {
long hash = 0;
final int MODULUS = 1000000007;
for (char c : key.toCharArray()) {
hash = (31 * hash + (int) c) % MODULUS;
}
return (int) hash;
}
/* XOR hash */
static int xorHash(String key) {
int hash = 0;
final int MODULUS = 1000000007;
for (char c : key.toCharArray()) {
hash ^= (int) c;
}
return hash & MODULUS;
}
/* Rotational hash */
static int rotHash(String key) {
long hash = 0;
final int MODULUS = 1000000007;
for (char c : key.toCharArray()) {
hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;
}
return (int) hash;
}
public static void main(String[] args) {
String key = "Hello algorithm";
int hash = addHash(key);
System.out.println("Additive hash value is " + hash);
hash = mulHash(key);
System.out.println("Multiplicative hash value is " + hash);
hash = xorHash(key);
System.out.println("XOR hash value is " + hash);
hash = rotHash(key);
System.out.println("Rotational hash value is " + hash);
}
}

View file

@ -0,0 +1,66 @@
/**
* File: heap.java
* Created Time: 2023-01-07
* Author: krahets (krahets@163.com)
*/
package chapter_heap;
import utils.*;
import java.util.*;
public class heap {
public static void testPush(Queue<Integer> heap, int val) {
heap.offer(val); // Push the element into heap
System.out.format("\nAfter element %d is added to the heap\n", val);
PrintUtil.printHeap(heap);
}
public static void testPop(Queue<Integer> heap) {
int val = heap.poll(); // Pop the element at the heap top
System.out.format("\nAfter the top element %d is removed from the heap\n", val);
PrintUtil.printHeap(heap);
}
public static void main(String[] args) {
/* Initialize the heap */
// Initialize min-heap
Queue<Integer> minHeap = new PriorityQueue<>();
// Initialize the max-heap (using lambda expression to modify Comparator if necessary)
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
System.out.println("\nThe following test case is for max-heap");
/* Push the element into heap */
testPush(maxHeap, 1);
testPush(maxHeap, 3);
testPush(maxHeap, 2);
testPush(maxHeap, 5);
testPush(maxHeap, 4);
/* Access heap top element */
int peek = maxHeap.peek();
System.out.format("\nTop element of the heap is %d\n", peek);
/* Pop the element at the heap top */
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
/* Get heap size */
int size = maxHeap.size();
System.out.format("\nNumber of elements in the heap is %d\n", size);
/* Determine if heap is empty */
boolean isEmpty = maxHeap.isEmpty();
System.out.format("\nIs the heap empty %b\n", isEmpty);
/* Enter list and build heap */
// Time complexity is O(n), not O(nlogn)
minHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));
System.out.println("\nEnter list and build min-heap");
PrintUtil.printHeap(minHeap);
}
}

View file

@ -0,0 +1,159 @@
/**
* File: my_heap.java
* Created Time: 2023-01-07
* Author: krahets (krahets@163.com)
*/
package chapter_heap;
import utils.*;
import java.util.*;
/* Max-heap */
class MaxHeap {
// Use list instead of array to avoid the need for resizing
private List<Integer> maxHeap;
/* Constructor, build heap based on input list */
public MaxHeap(List<Integer> nums) {
// Add all list elements into the heap
maxHeap = new ArrayList<>(nums);
// Heapify all nodes except leaves
for (int i = parent(size() - 1); i >= 0; i--) {
siftDown(i);
}
}
/* Get index of left child node */
private int left(int i) {
return 2 * i + 1;
}
/* Get index of right child node */
private int right(int i) {
return 2 * i + 2;
}
/* Get index of parent node */
private int parent(int i) {
return (i - 1) / 2; // Integer division down
}
/* Swap elements */
private void swap(int i, int j) {
int tmp = maxHeap.get(i);
maxHeap.set(i, maxHeap.get(j));
maxHeap.set(j, tmp);
}
/* Get heap size */
public int size() {
return maxHeap.size();
}
/* Determine if heap is empty */
public boolean isEmpty() {
return size() == 0;
}
/* Access heap top element */
public int peek() {
return maxHeap.get(0);
}
/* Push the element into heap */
public void push(int val) {
// Add node
maxHeap.add(val);
// Heapify from bottom to top
siftUp(size() - 1);
}
/* Start heapifying node i, from bottom to top */
private void siftUp(int i) {
while (true) {
// Get parent node of node i
int p = parent(i);
// When "crossing the root node" or "node does not need repair", end heapification
if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))
break;
// Swap two nodes
swap(i, p);
// Loop upwards heapification
i = p;
}
}
/* Element exits heap */
public int pop() {
// Empty handling
if (isEmpty())
throw new IndexOutOfBoundsException();
// Swap the root node with the rightmost leaf node (swap the first element with the last element)
swap(0, size() - 1);
// Remove node
int val = maxHeap.remove(size() - 1);
// Heapify from top to bottom
siftDown(0);
// Return heap top element
return val;
}
/* Start heapifying node i, from top to bottom */
private void siftDown(int i) {
while (true) {
// Determine the largest node among i, l, r, noted as ma
int l = left(i), r = right(i), ma = i;
if (l < size() && maxHeap.get(l) > maxHeap.get(ma))
ma = l;
if (r < size() && maxHeap.get(r) > maxHeap.get(ma))
ma = r;
// If node i is the largest or indices l, r are out of bounds, no further heapification needed, break
if (ma == i)
break;
// Swap two nodes
swap(i, ma);
// Loop downwards heapification
i = ma;
}
}
/* Print heap (binary tree) */
public void print() {
Queue<Integer> queue = new PriorityQueue<>((a, b) -> { return b - a; });
queue.addAll(maxHeap);
PrintUtil.printHeap(queue);
}
}
public class my_heap {
public static void main(String[] args) {
/* Initialize max-heap */
MaxHeap maxHeap = new MaxHeap(Arrays.asList(9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2));
System.out.println("\nEnter list and build heap");
maxHeap.print();
/* Access heap top element */
int peek = maxHeap.peek();
System.out.format("\nTop element of the heap is %d\n", peek);
/* Push the element into heap */
int val = 7;
maxHeap.push(val);
System.out.format("\nAfter element %d is added to the heap\n", val);
maxHeap.print();
/* Pop the element at the heap top */
peek = maxHeap.pop();
System.out.format("\nAfter the top element %d is removed from the heap\n", peek);
maxHeap.print();
/* Get heap size */
int size = maxHeap.size();
System.out.format("\nNumber of elements in the heap is %d\n", size);
/* Determine if heap is empty */
boolean isEmpty = maxHeap.isEmpty();
System.out.format("\nIs the heap empty %b\n", isEmpty);
}
}

View file

@ -0,0 +1,40 @@
/**
* File: top_k.java
* Created Time: 2023-06-12
* Author: krahets (krahets@163.com)
*/
package chapter_heap;
import utils.*;
import java.util.*;
public class top_k {
/* Using heap to find the largest k elements in an array */
static Queue<Integer> topKHeap(int[] nums, int k) {
// Initialize min-heap
Queue<Integer> heap = new PriorityQueue<Integer>();
// Enter the first k elements of the array into the heap
for (int i = 0; i < k; i++) {
heap.offer(nums[i]);
}
// From the k+1th element, keep the heap length as k
for (int i = k; i < nums.length; i++) {
// If the current element is larger than the heap top element, remove the heap top element and enter the current element into the heap
if (nums[i] > heap.peek()) {
heap.poll();
heap.offer(nums[i]);
}
}
return heap;
}
public static void main(String[] args) {
int[] nums = { 1, 7, 6, 3, 2 };
int k = 3;
Queue<Integer> res = topKHeap(nums, k);
System.out.println("The largest " + k + " elements are");
PrintUtil.printHeap(res);
}
}

View file

@ -0,0 +1,58 @@
/**
* File: binary_search.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
public class binary_search {
/* Binary search (double closed interval) */
static int binarySearch(int[] nums, int target) {
// Initialize double closed interval [0, n-1], i.e., i, j point to the first element and last element of the array respectively
int i = 0, j = nums.length - 1;
// Loop until the search interval is empty (when i > j, it is empty)
while (i <= j) {
int m = i + (j - i) / 2; // Calculate midpoint index m
if (nums[m] < target) // This situation indicates that target is in the interval [m+1, j]
i = m + 1;
else if (nums[m] > target) // This situation indicates that target is in the interval [i, m-1]
j = m - 1;
else // Found the target element, thus return its index
return m;
}
// Did not find the target element, thus return -1
return -1;
}
/* Binary search (left closed right open interval) */
static int binarySearchLCRO(int[] nums, int target) {
// Initialize left closed right open interval [0, n), i.e., i, j point to the first element and the last element +1 of the array respectively
int i = 0, j = nums.length;
// Loop until the search interval is empty (when i = j, it is empty)
while (i < j) {
int m = i + (j - i) / 2; // Calculate midpoint index m
if (nums[m] < target) // This situation indicates that target is in the interval [m+1, j)
i = m + 1;
else if (nums[m] > target) // This situation indicates that target is in the interval [i, m)
j = m;
else // Found the target element, thus return its index
return m;
}
// Did not find the target element, thus return -1
return -1;
}
public static void main(String[] args) {
int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
/* Binary search (double closed interval) */
int index = binarySearch(nums, target);
System.out.println("Index of target element 6 =" + index);
/* Binary search (left closed right open interval) */
index = binarySearchLCRO(nums, target);
System.out.println("Index of target element 6 =" + index);
}
}

View file

@ -0,0 +1,49 @@
/**
* File: binary_search_edge.java
* Created Time: 2023-08-04
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
public class binary_search_edge {
/* Binary search for the leftmost target */
static int binarySearchLeftEdge(int[] nums, int target) {
// Equivalent to finding the insertion point of target
int i = binary_search_insertion.binarySearchInsertion(nums, target);
// Did not find target, thus return -1
if (i == nums.length || nums[i] != target) {
return -1;
}
// Found target, return index i
return i;
}
/* Binary search for the rightmost target */
static int binarySearchRightEdge(int[] nums, int target) {
// Convert to finding the leftmost target + 1
int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);
// j points to the rightmost target, i points to the first element greater than target
int j = i - 1;
// Did not find target, thus return -1
if (j == -1 || nums[j] != target) {
return -1;
}
// Found target, return index j
return j;
}
public static void main(String[] args) {
// Array with duplicate elements
int[] nums = { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
System.out.println("\nArray nums = " + java.util.Arrays.toString(nums));
// Binary search for left and right boundaries
for (int target : new int[] { 6, 7 }) {
int index = binarySearchLeftEdge(nums, target);
System.out.println("The leftmost index of element " + target + " is " + index);
index = binarySearchRightEdge(nums, target);
System.out.println("The rightmost index of element " + target + " is " + index);
}
}
}

View file

@ -0,0 +1,63 @@
/**
* File: binary_search_insertion.java
* Created Time: 2023-08-04
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
class binary_search_insertion {
/* Binary search for insertion point (no duplicate elements) */
static int binarySearchInsertionSimple(int[] nums, int target) {
int i = 0, j = nums.length - 1; // Initialize double closed interval [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // Calculate midpoint index m
if (nums[m] < target) {
i = m + 1; // Target is in interval [m+1, j]
} else if (nums[m] > target) {
j = m - 1; // Target is in interval [i, m-1]
} else {
return m; // Found target, return insertion point m
}
}
// Did not find target, return insertion point i
return i;
}
/* Binary search for insertion point (with duplicate elements) */
static int binarySearchInsertion(int[] nums, int target) {
int i = 0, j = nums.length - 1; // Initialize double closed interval [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // Calculate midpoint index m
if (nums[m] < target) {
i = m + 1; // Target is in interval [m+1, j]
} else if (nums[m] > target) {
j = m - 1; // Target is in interval [i, m-1]
} else {
j = m - 1; // First element less than target is in interval [i, m-1]
}
}
// Return insertion point i
return i;
}
public static void main(String[] args) {
// Array without duplicate elements
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
System.out.println("\nArray nums = " + java.util.Arrays.toString(nums));
// Binary search for insertion point
for (int target : new int[] { 6, 9 }) {
int index = binarySearchInsertionSimple(nums, target);
System.out.println("The insertion point index for element " + target + " is " + index);
}
// Array with duplicate elements
nums = new int[] { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
System.out.println("\nArray nums = " + java.util.Arrays.toString(nums));
// Binary search for insertion point
for (int target : new int[] { 2, 6, 20 }) {
int index = binarySearchInsertion(nums, target);
System.out.println("The insertion point index for element " + target + " is " + index);
}
}
}

View file

@ -0,0 +1,51 @@
/**
* File: hashing_search.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
import utils.*;
import java.util.*;
public class hashing_search {
/* Hash search (array) */
static int hashingSearchArray(Map<Integer, Integer> map, int target) {
// Hash table's key: target element, value: index
// If the hash table does not contain this key, return -1
return map.getOrDefault(target, -1);
}
/* Hash search (linked list) */
static ListNode hashingSearchLinkedList(Map<Integer, ListNode> map, int target) {
// Hash table key: target node value, value: node object
// If the key is not in the hash table, return null
return map.getOrDefault(target, null);
}
public static void main(String[] args) {
int target = 3;
/* Hash search (array) */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
// Initialize hash table
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i); // key: element, value: index
}
int index = hashingSearchArray(map, target);
System.out.println("The index of target element 3 is " + index);
/* Hash search (linked list) */
ListNode head = ListNode.arrToLinkedList(nums);
// Initialize hash table
Map<Integer, ListNode> map1 = new HashMap<>();
while (head != null) {
map1.put(head.val, head); // key: node value, value: node
head = head.next;
}
ListNode node = hashingSearchLinkedList(map1, target);
System.out.println("The corresponding node object for target node value 3 is " + node);
}
}

View file

@ -0,0 +1,50 @@
/**
* File: linear_search.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
import utils.*;
public class linear_search {
/* Linear search (array) */
static int linearSearchArray(int[] nums, int target) {
// Traverse array
for (int i = 0; i < nums.length; i++) {
// Found the target element, thus return its index
if (nums[i] == target)
return i;
}
// Did not find the target element, thus return -1
return -1;
}
/* Linear search (linked list) */
static ListNode linearSearchLinkedList(ListNode head, int target) {
// Traverse the list
while (head != null) {
// Found the target node, return it
if (head.val == target)
return head;
head = head.next;
}
// If the target node is not found, return null
return null;
}
public static void main(String[] args) {
int target = 3;
/* Perform linear search in array */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
int index = linearSearchArray(nums, target);
System.out.println("The index of target element 3 is " + index);
/* Perform linear search in linked list */
ListNode head = ListNode.arrToLinkedList(nums);
ListNode node = linearSearchLinkedList(head, target);
System.out.println("The corresponding node object for target node value 3 is " + node);
}
}

View file

@ -0,0 +1,53 @@
/**
* File: two_sum.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
import java.util.*;
public class two_sum {
/* Method one: Brute force enumeration */
static int[] twoSumBruteForce(int[] nums, int target) {
int size = nums.length;
// Two-layer loop, time complexity is O(n^2)
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (nums[i] + nums[j] == target)
return new int[] { i, j };
}
}
return new int[0];
}
/* Method two: Auxiliary hash table */
static int[] twoSumHashTable(int[] nums, int target) {
int size = nums.length;
// Auxiliary hash table, space complexity is O(n)
Map<Integer, Integer> dic = new HashMap<>();
// Single-layer loop, time complexity is O(n)
for (int i = 0; i < size; i++) {
if (dic.containsKey(target - nums[i])) {
return new int[] { dic.get(target - nums[i]), i };
}
dic.put(nums[i], i);
}
return new int[0];
}
public static void main(String[] args) {
// ======= Test Case =======
int[] nums = { 2, 7, 11, 15 };
int target = 13;
// ====== Driver Code ======
// Method one
int[] res = twoSumBruteForce(nums, target);
System.out.println("Method one res = " + Arrays.toString(res));
// Method two
res = twoSumHashTable(nums, target);
System.out.println("Method two res = " + Arrays.toString(res));
}
}

View file

@ -0,0 +1,57 @@
/**
* File: bubble_sort.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.*;
public class bubble_sort {
/* Bubble sort */
static void bubbleSort(int[] nums) {
// Outer loop: unsorted range is [0, i]
for (int i = nums.length - 1; i > 0; i--) {
// Inner loop: swap the largest element in the unsorted range [0, i] to the right end of the range
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// Swap nums[j] and nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
}
}
}
}
/* Bubble sort (optimized with flag) */
static void bubbleSortWithFlag(int[] nums) {
// Outer loop: unsorted range is [0, i]
for (int i = nums.length - 1; i > 0; i--) {
boolean flag = false; // Initialize flag
// Inner loop: swap the largest element in the unsorted range [0, i] to the right end of the range
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// Swap nums[j] and nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
flag = true; // Record swapped elements
}
}
if (!flag)
break; // If no elements were swapped in this round of "bubbling", exit
}
}
public static void main(String[] args) {
int[] nums = { 4, 1, 3, 1, 5, 2 };
bubbleSort(nums);
System.out.println("After bubble sort, nums = " + Arrays.toString(nums));
int[] nums1 = { 4, 1, 3, 1, 5, 2 };
bubbleSortWithFlag(nums1);
System.out.println("After bubble sort, nums1 = " + Arrays.toString(nums1));
}
}

View file

@ -0,0 +1,47 @@
/**
* File: bucket_sort.java
* Created Time: 2023-03-17
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.*;
public class bucket_sort {
/* Bucket sort */
static void bucketSort(float[] nums) {
// Initialize k = n/2 buckets, expected to allocate 2 elements per bucket
int k = nums.length / 2;
List<List<Float>> buckets = new ArrayList<>();
for (int i = 0; i < k; i++) {
buckets.add(new ArrayList<>());
}
// 1. Distribute array elements into various buckets
for (float num : nums) {
// Input data range is [0, 1), use num * k to map to index range [0, k-1]
int i = (int) (num * k);
// Add num to bucket i
buckets.get(i).add(num);
}
// 2. Sort each bucket
for (List<Float> bucket : buckets) {
// Use built-in sorting function, can also replace with other sorting algorithms
Collections.sort(bucket);
}
// 3. Traverse buckets to merge results
int i = 0;
for (List<Float> bucket : buckets) {
for (float num : bucket) {
nums[i++] = num;
}
}
}
public static void main(String[] args) {
// Assume input data is floating point, range [0, 1)
float[] nums = { 0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f };
bucketSort(nums);
System.out.println("After bucket sort, nums = " + Arrays.toString(nums));
}
}

View file

@ -0,0 +1,78 @@
/**
* File: counting_sort.java
* Created Time: 2023-03-17
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.*;
public class counting_sort {
/* Counting sort */
// Simple implementation, cannot be used for sorting objects
static void countingSortNaive(int[] nums) {
// 1. Count the maximum element m in the array
int m = 0;
for (int num : nums) {
m = Math.max(m, num);
}
// 2. Count the occurrence of each digit
// counter[num] represents the occurrence of num
int[] counter = new int[m + 1];
for (int num : nums) {
counter[num]++;
}
// 3. Traverse counter, filling each element back into the original array nums
int i = 0;
for (int num = 0; num < m + 1; num++) {
for (int j = 0; j < counter[num]; j++, i++) {
nums[i] = num;
}
}
}
/* Counting sort */
// Complete implementation, can sort objects and is a stable sort
static void countingSort(int[] nums) {
// 1. Count the maximum element m in the array
int m = 0;
for (int num : nums) {
m = Math.max(m, num);
}
// 2. Count the occurrence of each digit
// counter[num] represents the occurrence of num
int[] counter = new int[m + 1];
for (int num : nums) {
counter[num]++;
}
// 3. Calculate the prefix sum of counter, converting "occurrence count" to "tail index"
// counter[num]-1 is the last index where num appears in res
for (int i = 0; i < m; i++) {
counter[i + 1] += counter[i];
}
// 4. Traverse nums in reverse order, placing each element into the result array res
// Initialize the array res to record results
int n = nums.length;
int[] res = new int[n];
for (int i = n - 1; i >= 0; i--) {
int num = nums[i];
res[counter[num] - 1] = num; // Place num at the corresponding index
counter[num]--; // Decrement the prefix sum by 1, getting the next index to place num
}
// Use result array res to overwrite the original array nums
for (int i = 0; i < n; i++) {
nums[i] = res[i];
}
}
public static void main(String[] args) {
int[] nums = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
countingSortNaive(nums);
System.out.println("After count sort (unable to sort objects), nums = " + Arrays.toString(nums));
int[] nums1 = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
countingSort(nums1);
System.out.println("After count sort, nums1 = " + Arrays.toString(nums1));
}
}

View file

@ -0,0 +1,57 @@
/**
* File: heap_sort.java
* Created Time: 2023-05-26
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.Arrays;
public class heap_sort {
/* Heap length is n, start heapifying node i, from top to bottom */
public static void siftDown(int[] nums, int n, int i) {
while (true) {
// Determine the largest node among i, l, r, noted as ma
int l = 2 * i + 1;
int r = 2 * i + 2;
int ma = i;
if (l < n && nums[l] > nums[ma])
ma = l;
if (r < n && nums[r] > nums[ma])
ma = r;
// If node i is the largest or indices l, r are out of bounds, no further heapification needed, break
if (ma == i)
break;
// Swap two nodes
int temp = nums[i];
nums[i] = nums[ma];
nums[ma] = temp;
// Loop downwards heapification
i = ma;
}
}
/* Heap sort */
public static void heapSort(int[] nums) {
// Build heap operation: heapify all nodes except leaves
for (int i = nums.length / 2 - 1; i >= 0; i--) {
siftDown(nums, nums.length, i);
}
// Extract the largest element from the heap and repeat for n-1 rounds
for (int i = nums.length - 1; i > 0; i--) {
// Swap the root node with the rightmost leaf node (swap the first element with the last element)
int tmp = nums[0];
nums[0] = nums[i];
nums[i] = tmp;
// Start heapifying the root node, from top to bottom
siftDown(nums, i, 0);
}
}
public static void main(String[] args) {
int[] nums = { 4, 1, 3, 1, 5, 2 };
heapSort(nums);
System.out.println("After heap sort, nums = " + Arrays.toString(nums));
}
}

View file

@ -0,0 +1,31 @@
/**
* File: insertion_sort.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.*;
public class insertion_sort {
/* Insertion sort */
static void insertionSort(int[] nums) {
// Outer loop: sorted range is [0, i-1]
for (int i = 1; i < nums.length; i++) {
int base = nums[i], j = i - 1;
// Inner loop: insert base into the correct position within the sorted range [0, i-1]
while (j >= 0 && nums[j] > base) {
nums[j + 1] = nums[j]; // Move nums[j] to the right by one position
j--;
}
nums[j + 1] = base; // Assign base to the correct position
}
}
public static void main(String[] args) {
int[] nums = { 4, 1, 3, 1, 5, 2 };
insertionSort(nums);
System.out.println("After insertion sort, nums = " + Arrays.toString(nums));
}
}

View file

@ -0,0 +1,58 @@
/**
* File: merge_sort.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.*;
public class merge_sort {
/* Merge left subarray and right subarray */
static void merge(int[] nums, int left, int mid, int right) {
// Left subarray interval is [left, mid], right subarray interval is [mid+1, right]
// Create a temporary array tmp to store the merged results
int[] tmp = new int[right - left + 1];
// Initialize the start indices of the left and right subarrays
int i = left, j = mid + 1, k = 0;
// While both subarrays still have elements, compare and copy the smaller element into the temporary array
while (i <= mid && j <= right) {
if (nums[i] <= nums[j])
tmp[k++] = nums[i++];
else
tmp[k++] = nums[j++];
}
// Copy the remaining elements of the left and right subarrays into the temporary array
while (i <= mid) {
tmp[k++] = nums[i++];
}
while (j <= right) {
tmp[k++] = nums[j++];
}
// Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval
for (k = 0; k < tmp.length; k++) {
nums[left + k] = tmp[k];
}
}
/* Merge sort */
static void mergeSort(int[] nums, int left, int right) {
// Termination condition
if (left >= right)
return; // Terminate recursion when subarray length is 1
// Partition stage
int mid = (left + right) / 2; // Calculate midpoint
mergeSort(nums, left, mid); // Recursively process the left subarray
mergeSort(nums, mid + 1, right); // Recursively process the right subarray
// Merge stage
merge(nums, left, mid, right);
}
public static void main(String[] args) {
/* Merge sort */
int[] nums = { 7, 3, 2, 6, 0, 1, 5, 4 };
mergeSort(nums, 0, nums.length - 1);
System.out.println("After merge sort, nums = " + Arrays.toString(nums));
}
}

View file

@ -0,0 +1,158 @@
/**
* File: quick_sort.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.*;
/* Quick sort class */
class QuickSort {
/* Swap elements */
static void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* Partition */
static int partition(int[] nums, int left, int right) {
// Use nums[left] as the pivot
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
j--; // Search from right to left for the first element smaller than the pivot
while (i < j && nums[i] <= nums[left])
i++; // Search from left to right for the first element greater than the pivot
swap(nums, i, j); // Swap these two elements
}
swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
return i; // Return the index of the pivot
}
/* Quick sort */
public static void quickSort(int[] nums, int left, int right) {
// Terminate recursion when subarray length is 1
if (left >= right)
return;
// Partition
int pivot = partition(nums, left, right);
// Recursively process the left subarray and right subarray
quickSort(nums, left, pivot - 1);
quickSort(nums, pivot + 1, right);
}
}
/* Quick sort class (median pivot optimization) */
class QuickSortMedian {
/* Swap elements */
static void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* Select the median of three candidate elements */
static int medianThree(int[] nums, int left, int mid, int right) {
int l = nums[left], m = nums[mid], r = nums[right];
if ((l <= m && m <= r) || (r <= m && m <= l))
return mid; // m is between l and r
if ((m <= l && l <= r) || (r <= l && l <= m))
return left; // l is between m and r
return right;
}
/* Partition (median of three) */
static int partition(int[] nums, int left, int right) {
// Select the median of three candidate elements
int med = medianThree(nums, left, (left + right) / 2, right);
// Swap the median to the array's leftmost position
swap(nums, left, med);
// Use nums[left] as the pivot
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
j--; // Search from right to left for the first element smaller than the pivot
while (i < j && nums[i] <= nums[left])
i++; // Search from left to right for the first element greater than the pivot
swap(nums, i, j); // Swap these two elements
}
swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
return i; // Return the index of the pivot
}
/* Quick sort */
public static void quickSort(int[] nums, int left, int right) {
// Terminate recursion when subarray length is 1
if (left >= right)
return;
// Partition
int pivot = partition(nums, left, right);
// Recursively process the left subarray and right subarray
quickSort(nums, left, pivot - 1);
quickSort(nums, pivot + 1, right);
}
}
/* Quick sort class (tail recursion optimization) */
class QuickSortTailCall {
/* Swap elements */
static void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* Partition */
static int partition(int[] nums, int left, int right) {
// Use nums[left] as the pivot
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
j--; // Search from right to left for the first element smaller than the pivot
while (i < j && nums[i] <= nums[left])
i++; // Search from left to right for the first element greater than the pivot
swap(nums, i, j); // Swap these two elements
}
swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
return i; // Return the index of the pivot
}
/* Quick sort (tail recursion optimization) */
public static void quickSort(int[] nums, int left, int right) {
// Terminate when subarray length is 1
while (left < right) {
// Partition operation
int pivot = partition(nums, left, right);
// Perform quick sort on the shorter of the two subarrays
if (pivot - left < right - pivot) {
quickSort(nums, left, pivot - 1); // Recursively sort the left subarray
left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]
} else {
quickSort(nums, pivot + 1, right); // Recursively sort the right subarray
right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]
}
}
}
}
public class quick_sort {
public static void main(String[] args) {
/* Quick sort */
int[] nums = { 2, 4, 1, 0, 3, 5 };
QuickSort.quickSort(nums, 0, nums.length - 1);
System.out.println("After quick sort, nums = " + Arrays.toString(nums));
/* Quick sort (median pivot optimization) */
int[] nums1 = { 2, 4, 1, 0, 3, 5 };
QuickSortMedian.quickSort(nums1, 0, nums1.length - 1);
System.out.println("After quick sort with median pivot optimization, nums1 = " + Arrays.toString(nums1));
/* Quick sort (tail recursion optimization) */
int[] nums2 = { 2, 4, 1, 0, 3, 5 };
QuickSortTailCall.quickSort(nums2, 0, nums2.length - 1);
System.out.println("After quick sort with tail recursion optimization, nums2 = " + Arrays.toString(nums2));
}
}

View file

@ -0,0 +1,69 @@
/**
* File: radix_sort.java
* Created Time: 2023-01-17
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.*;
public class radix_sort {
/* Get the k-th digit of element num, where exp = 10^(k-1) */
static int digit(int num, int exp) {
// Passing exp instead of k can avoid repeated expensive exponentiation here
return (num / exp) % 10;
}
/* Counting sort (based on nums k-th digit) */
static void countingSortDigit(int[] nums, int exp) {
// Decimal digit range is 0~9, therefore need a bucket array of length 10
int[] counter = new int[10];
int n = nums.length;
// Count the occurrence of digits 0~9
for (int i = 0; i < n; i++) {
int d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d
counter[d]++; // Count the occurrence of digit d
}
// Calculate prefix sum, converting "occurrence count" into "array index"
for (int i = 1; i < 10; i++) {
counter[i] += counter[i - 1];
}
// Traverse in reverse, based on bucket statistics, place each element into res
int[] res = new int[n];
for (int i = n - 1; i >= 0; i--) {
int d = digit(nums[i], exp);
int j = counter[d] - 1; // Get the index j for d in the array
res[j] = nums[i]; // Place the current element at index j
counter[d]--; // Decrease the count of d by 1
}
// Use result to overwrite the original array nums
for (int i = 0; i < n; i++)
nums[i] = res[i];
}
/* Radix sort */
static void radixSort(int[] nums) {
// Get the maximum element of the array, used to determine the maximum number of digits
int m = Integer.MIN_VALUE;
for (int num : nums)
if (num > m)
m = num;
// Traverse from the lowest to the highest digit
for (int exp = 1; exp <= m; exp *= 10) {
// Perform counting sort on the k-th digit of array elements
// k = 1 -> exp = 1
// k = 2 -> exp = 10
// i.e., exp = 10^(k-1)
countingSortDigit(nums, exp);
}
}
public static void main(String[] args) {
// Radix sort
int[] nums = { 10546151, 35663510, 42865989, 34862445, 81883077,
88906420, 72429244, 30524779, 82060337, 63832996 };
radixSort(nums);
System.out.println("After radix sort, nums = " + Arrays.toString(nums));
}
}

View file

@ -0,0 +1,35 @@
/**
* File: selection_sort.java
* Created Time: 2023-05-23
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.Arrays;
public class selection_sort {
/* Selection sort */
public static void selectionSort(int[] nums) {
int n = nums.length;
// Outer loop: unsorted range is [i, n-1]
for (int i = 0; i < n - 1; i++) {
// Inner loop: find the smallest element within the unsorted range
int k = i;
for (int j = i + 1; j < n; j++) {
if (nums[j] < nums[k])
k = j; // Record the index of the smallest element
}
// Swap the smallest element with the first element of the unsorted range
int temp = nums[i];
nums[i] = nums[k];
nums[k] = temp;
}
}
public static void main(String[] args) {
int[] nums = { 4, 1, 3, 1, 5, 2 };
selectionSort(nums);
System.out.println("After selection sort, nums = " + Arrays.toString(nums));
}
}

View file

@ -0,0 +1,151 @@
/**
* File: array_deque.java
* Created Time: 2023-02-16
* Author: krahets (krahets@163.com), FangYuan33 (374072213@qq.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* Double-ended queue class based on circular array */
class ArrayDeque {
private int[] nums; // Array used to store elements of the double-ended queue
private int front; // Front pointer, pointing to the front element
private int queSize; // Length of the double-ended queue
/* Constructor */
public ArrayDeque(int capacity) {
this.nums = new int[capacity];
front = queSize = 0;
}
/* Get the capacity of the double-ended queue */
public int capacity() {
return nums.length;
}
/* Get the length of the double-ended queue */
public int size() {
return queSize;
}
/* Determine if the double-ended queue is empty */
public boolean isEmpty() {
return queSize == 0;
}
/* Calculate circular array index */
private int index(int i) {
// Implement circular array by modulo operation
// When i exceeds the tail of the array, return to the head
// When i exceeds the head of the array, return to the tail
return (i + capacity()) % capacity();
}
/* Front enqueue */
public void pushFirst(int num) {
if (queSize == capacity()) {
System.out.println("Double-ended queue is full");
return;
}
// Move the front pointer one position to the left
// Implement front crossing the head of the array to return to the tail by modulo operation
front = index(front - 1);
// Add num to the front
nums[front] = num;
queSize++;
}
/* Rear enqueue */
public void pushLast(int num) {
if (queSize == capacity()) {
System.out.println("Double-ended queue is full");
return;
}
// Calculate rear pointer, pointing to rear index + 1
int rear = index(front + queSize);
// Add num to the rear
nums[rear] = num;
queSize++;
}
/* Front dequeue */
public int popFirst() {
int num = peekFirst();
// Move front pointer one position backward
front = index(front + 1);
queSize--;
return num;
}
/* Rear dequeue */
public int popLast() {
int num = peekLast();
queSize--;
return num;
}
/* Access front element */
public int peekFirst() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return nums[front];
}
/* Access rear element */
public int peekLast() {
if (isEmpty())
throw new IndexOutOfBoundsException();
// Calculate rear element index
int last = index(front + queSize - 1);
return nums[last];
}
/* Return array for printing */
public int[] toArray() {
// Only convert elements within valid length range
int[] res = new int[queSize];
for (int i = 0, j = front; i < queSize; i++, j++) {
res[i] = nums[index(j)];
}
return res;
}
}
public class array_deque {
public static void main(String[] args) {
/* Initialize double-ended queue */
ArrayDeque deque = new ArrayDeque(10);
deque.pushLast(3);
deque.pushLast(2);
deque.pushLast(5);
System.out.println("Double-ended queue deque = " + Arrays.toString(deque.toArray()));
/* Access element */
int peekFirst = deque.peekFirst();
System.out.println("Front element peekFirst = " + peekFirst);
int peekLast = deque.peekLast();
System.out.println("Back element peekLast = " + peekLast);
/* Element enqueue */
deque.pushLast(4);
System.out.println("Element 4 enqueued at the tail, deque = " + Arrays.toString(deque.toArray()));
deque.pushFirst(1);
System.out.println("Element 1 enqueued at the head, deque = " + Arrays.toString(deque.toArray()));
/* Element dequeue */
int popLast = deque.popLast();
System.out.println("Deque tail element = " + popLast + ", after dequeuing from the tail" + Arrays.toString(deque.toArray()));
int popFirst = deque.popFirst();
System.out.println("Deque front element = " + popFirst + ", after dequeuing from the front" + Arrays.toString(deque.toArray()));
/* Get the length of the double-ended queue */
int size = deque.size();
System.out.println("Length of the double-ended queue size = " + size);
/* Determine if the double-ended queue is empty */
boolean isEmpty = deque.isEmpty();
System.out.println("Is the double-ended queue empty = " + isEmpty);
}
}

View file

@ -0,0 +1,115 @@
/**
* File: array_queue.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* Queue class based on circular array */
class ArrayQueue {
private int[] nums; // Array for storing queue elements
private int front; // Front pointer, pointing to the front element
private int queSize; // Queue length
public ArrayQueue(int capacity) {
nums = new int[capacity];
front = queSize = 0;
}
/* Get the capacity of the queue */
public int capacity() {
return nums.length;
}
/* Get the length of the queue */
public int size() {
return queSize;
}
/* Determine if the queue is empty */
public boolean isEmpty() {
return queSize == 0;
}
/* Enqueue */
public void push(int num) {
if (queSize == capacity()) {
System.out.println("Queue is full");
return;
}
// Calculate rear pointer, pointing to rear index + 1
// Use modulo operation to wrap the rear pointer from the end of the array back to the start
int rear = (front + queSize) % capacity();
// Add num to the rear
nums[rear] = num;
queSize++;
}
/* Dequeue */
public int pop() {
int num = peek();
// Move front pointer one position backward, returning to the head of the array if it exceeds the tail
front = (front + 1) % capacity();
queSize--;
return num;
}
/* Access front element */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return nums[front];
}
/* Return array */
public int[] toArray() {
// Only convert elements within valid length range
int[] res = new int[queSize];
for (int i = 0, j = front; i < queSize; i++, j++) {
res[i] = nums[j % capacity()];
}
return res;
}
}
public class array_queue {
public static void main(String[] args) {
/* Initialize queue */
int capacity = 10;
ArrayQueue queue = new ArrayQueue(capacity);
/* Element enqueue */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
System.out.println("Queue queue = " + Arrays.toString(queue.toArray()));
/* Access front element */
int peek = queue.peek();
System.out.println("Front element peek = " + peek);
/* Element dequeue */
int pop = queue.pop();
System.out.println("Dequeued element = " + pop + ", after dequeuing" + Arrays.toString(queue.toArray()));
/* Get the length of the queue */
int size = queue.size();
System.out.println("Length of the queue size = " + size);
/* Determine if the queue is empty */
boolean isEmpty = queue.isEmpty();
System.out.println("Is the queue empty = " + isEmpty);
/* Test circular array */
for (int i = 0; i < 10; i++) {
queue.push(i);
queue.pop();
System.out.println("After the " + i + "th round of enqueueing + dequeuing, queue = " + Arrays.toString(queue.toArray()));
}
}
}

View file

@ -0,0 +1,84 @@
/**
* File: array_stack.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* Stack class based on array */
class ArrayStack {
private ArrayList<Integer> stack;
public ArrayStack() {
// Initialize the list (dynamic array)
stack = new ArrayList<>();
}
/* Get the length of the stack */
public int size() {
return stack.size();
}
/* Determine if the stack is empty */
public boolean isEmpty() {
return size() == 0;
}
/* Push */
public void push(int num) {
stack.add(num);
}
/* Pop */
public int pop() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return stack.remove(size() - 1);
}
/* Access stack top element */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return stack.get(size() - 1);
}
/* Convert the List to Array and return */
public Object[] toArray() {
return stack.toArray();
}
}
public class array_stack {
public static void main(String[] args) {
/* Initialize stack */
ArrayStack stack = new ArrayStack();
/* Element push */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
System.out.println("Stack stack = " + Arrays.toString(stack.toArray()));
/* Access stack top element */
int peek = stack.peek();
System.out.println("Top element peek = " + peek);
/* Element pop */
int pop = stack.pop();
System.out.println("Popped element = " + pop + ", after popping" + Arrays.toString(stack.toArray()));
/* Get the length of the stack */
int size = stack.size();
System.out.println("Length of the stack size = " + size);
/* Determine if it's empty */
boolean isEmpty = stack.isEmpty();
System.out.println("Is the stack empty = " + isEmpty);
}
}

View file

@ -0,0 +1,46 @@
/**
* File: deque.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
public class deque {
public static void main(String[] args) {
/* Initialize double-ended queue */
Deque<Integer> deque = new LinkedList<>();
deque.offerLast(3);
deque.offerLast(2);
deque.offerLast(5);
System.out.println("Double-ended queue deque = " + deque);
/* Access element */
int peekFirst = deque.peekFirst();
System.out.println("Front element peekFirst = " + peekFirst);
int peekLast = deque.peekLast();
System.out.println("Back element peekLast = " + peekLast);
/* Element enqueue */
deque.offerLast(4);
System.out.println("Element 4 enqueued at the tail, deque = " + deque);
deque.offerFirst(1);
System.out.println("Element 1 enqueued at the head, deque = " + deque);
/* Element dequeue */
int popLast = deque.pollLast();
System.out.println("Deque tail element = " + popLast + ", after dequeuing from the tail" + deque);
int popFirst = deque.pollFirst();
System.out.println("Deque front element = " + popFirst + ", after dequeuing from the front" + deque);
/* Get the length of the double-ended queue */
int size = deque.size();
System.out.println("Length of the double-ended queue size = " + size);
/* Determine if the double-ended queue is empty */
boolean isEmpty = deque.isEmpty();
System.out.println("Is the double-ended queue empty = " + isEmpty);
}
}

View file

@ -0,0 +1,175 @@
/**
* File: linkedlist_deque.java
* Created Time: 2023-01-20
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* Double-linked list node */
class ListNode {
int val; // Node value
ListNode next; // Reference to the next node
ListNode prev; // Reference to predecessor node
ListNode(int val) {
this.val = val;
prev = next = null;
}
}
/* Double-ended queue class based on double-linked list */
class LinkedListDeque {
private ListNode front, rear; // Front node front, back node rear
private int queSize = 0; // Length of the double-ended queue
public LinkedListDeque() {
front = rear = null;
}
/* Get the length of the double-ended queue */
public int size() {
return queSize;
}
/* Determine if the double-ended queue is empty */
public boolean isEmpty() {
return size() == 0;
}
/* Enqueue operation */
private void push(int num, boolean isFront) {
ListNode node = new ListNode(num);
// If the list is empty, make front and rear both point to node
if (isEmpty())
front = rear = node;
// Front enqueue operation
else if (isFront) {
// Add node to the head of the list
front.prev = node;
node.next = front;
front = node; // Update head node
// Rear enqueue operation
} else {
// Add node to the tail of the list
rear.next = node;
node.prev = rear;
rear = node; // Update tail node
}
queSize++; // Update queue length
}
/* Front enqueue */
public void pushFirst(int num) {
push(num, true);
}
/* Rear enqueue */
public void pushLast(int num) {
push(num, false);
}
/* Dequeue operation */
private int pop(boolean isFront) {
if (isEmpty())
throw new IndexOutOfBoundsException();
int val;
// Front dequeue operation
if (isFront) {
val = front.val; // Temporarily store the head node value
// Remove head node
ListNode fNext = front.next;
if (fNext != null) {
fNext.prev = null;
front.next = null;
}
front = fNext; // Update head node
// Rear dequeue operation
} else {
val = rear.val; // Temporarily store the tail node value
// Remove tail node
ListNode rPrev = rear.prev;
if (rPrev != null) {
rPrev.next = null;
rear.prev = null;
}
rear = rPrev; // Update tail node
}
queSize--; // Update queue length
return val;
}
/* Front dequeue */
public int popFirst() {
return pop(true);
}
/* Rear dequeue */
public int popLast() {
return pop(false);
}
/* Access front element */
public int peekFirst() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return front.val;
}
/* Access rear element */
public int peekLast() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return rear.val;
}
/* Return array for printing */
public int[] toArray() {
ListNode node = front;
int[] res = new int[size()];
for (int i = 0; i < res.length; i++) {
res[i] = node.val;
node = node.next;
}
return res;
}
}
public class linkedlist_deque {
public static void main(String[] args) {
/* Initialize double-ended queue */
LinkedListDeque deque = new LinkedListDeque();
deque.pushLast(3);
deque.pushLast(2);
deque.pushLast(5);
System.out.println("Double-ended queue deque = " + Arrays.toString(deque.toArray()));
/* Access element */
int peekFirst = deque.peekFirst();
System.out.println("Front element peekFirst = " + peekFirst);
int peekLast = deque.peekLast();
System.out.println("Back element peekLast = " + peekLast);
/* Element enqueue */
deque.pushLast(4);
System.out.println("Element 4 enqueued at the tail, deque = " + Arrays.toString(deque.toArray()));
deque.pushFirst(1);
System.out.println("Element 1 enqueued at the head, deque = " + Arrays.toString(deque.toArray()));
/* Element dequeue */
int popLast = deque.popLast();
System.out.println("Deque tail element = " + popLast + ", after dequeuing from the tail" + Arrays.toString(deque.toArray()));
int popFirst = deque.popFirst();
System.out.println("Deque front element = " + popFirst + ", after dequeuing from the front" + Arrays.toString(deque.toArray()));
/* Get the length of the double-ended queue */
int size = deque.size();
System.out.println("Length of the double-ended queue size = " + size);
/* Determine if the double-ended queue is empty */
boolean isEmpty = deque.isEmpty();
System.out.println("Is the double-ended queue empty = " + isEmpty);
}
}

View file

@ -0,0 +1,104 @@
/**
* File: linkedlist_queue.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* Queue class based on linked list */
class LinkedListQueue {
private ListNode front, rear; // Front node front, back node rear
private int queSize = 0;
public LinkedListQueue() {
front = null;
rear = null;
}
/* Get the length of the queue */
public int size() {
return queSize;
}
/* Determine if the queue is empty */
public boolean isEmpty() {
return size() == 0;
}
/* Enqueue */
public void push(int num) {
// Add num behind the tail node
ListNode node = new ListNode(num);
// If the queue is empty, make the head and tail nodes both point to that node
if (front == null) {
front = node;
rear = node;
// If the queue is not empty, add that node behind the tail node
} else {
rear.next = node;
rear = node;
}
queSize++;
}
/* Dequeue */
public int pop() {
int num = peek();
// Remove head node
front = front.next;
queSize--;
return num;
}
/* Access front element */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return front.val;
}
/* Convert the linked list to Array and return */
public int[] toArray() {
ListNode node = front;
int[] res = new int[size()];
for (int i = 0; i < res.length; i++) {
res[i] = node.val;
node = node.next;
}
return res;
}
}
public class linkedlist_queue {
public static void main(String[] args) {
/* Initialize queue */
LinkedListQueue queue = new LinkedListQueue();
/* Element enqueue */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
System.out.println("Queue queue = " + Arrays.toString(queue.toArray()));
/* Access front element */
int peek = queue.peek();
System.out.println("Front element peek = " + peek);
/* Element dequeue */
int pop = queue.pop();
System.out.println("Dequeued element = " + pop + ", after dequeuing" + Arrays.toString(queue.toArray()));
/* Get the length of the queue */
int size = queue.size();
System.out.println("Length of the queue size = " + size);
/* Determine if the queue is empty */
boolean isEmpty = queue.isEmpty();
System.out.println("Is the queue empty = " + isEmpty);
}
}

View file

@ -0,0 +1,95 @@
/**
* File: linkedlist_stack.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
import utils.*;
/* Stack class based on linked list */
class LinkedListStack {
private ListNode stackPeek; // Use the head node as the top of the stack
private int stkSize = 0; // Length of the stack
public LinkedListStack() {
stackPeek = null;
}
/* Get the length of the stack */
public int size() {
return stkSize;
}
/* Determine if the stack is empty */
public boolean isEmpty() {
return size() == 0;
}
/* Push */
public void push(int num) {
ListNode node = new ListNode(num);
node.next = stackPeek;
stackPeek = node;
stkSize++;
}
/* Pop */
public int pop() {
int num = peek();
stackPeek = stackPeek.next;
stkSize--;
return num;
}
/* Access stack top element */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return stackPeek.val;
}
/* Convert the List to Array and return */
public int[] toArray() {
ListNode node = stackPeek;
int[] res = new int[size()];
for (int i = res.length - 1; i >= 0; i--) {
res[i] = node.val;
node = node.next;
}
return res;
}
}
public class linkedlist_stack {
public static void main(String[] args) {
/* Initialize stack */
LinkedListStack stack = new LinkedListStack();
/* Element push */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
System.out.println("Stack stack = " + Arrays.toString(stack.toArray()));
/* Access stack top element */
int peek = stack.peek();
System.out.println("Top element peek = " + peek);
/* Element pop */
int pop = stack.pop();
System.out.println("Popped element = " + pop + ", after popping" + Arrays.toString(stack.toArray()));
/* Get the length of the stack */
int size = stack.size();
System.out.println("Length of the stack size = " + size);
/* Determine if it's empty */
boolean isEmpty = stack.isEmpty();
System.out.println("Is the stack empty = " + isEmpty);
}
}

View file

@ -0,0 +1,40 @@
/**
* File: queue.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
public class queue {
public static void main(String[] args) {
/* Initialize queue */
Queue<Integer> queue = new LinkedList<>();
/* Element enqueue */
queue.offer(1);
queue.offer(3);
queue.offer(2);
queue.offer(5);
queue.offer(4);
System.out.println("Queue queue = " + queue);
/* Access front element */
int peek = queue.peek();
System.out.println("Front element peek = " + peek);
/* Element dequeue */
int pop = queue.poll();
System.out.println("Dequeued element = " + pop + ", after dequeuing" + queue);
/* Get the length of the queue */
int size = queue.size();
System.out.println("Length of the queue size = " + size);
/* Determine if the queue is empty */
boolean isEmpty = queue.isEmpty();
System.out.println("Is the queue empty = " + isEmpty);
}
}

View file

@ -0,0 +1,40 @@
/**
* File: stack.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
public class stack {
public static void main(String[] args) {
/* Initialize stack */
Stack<Integer> stack = new Stack<>();
/* Element push */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
System.out.println("Stack stack = " + stack);
/* Access stack top element */
int peek = stack.peek();
System.out.println("Top element peek = " + peek);
/* Element pop */
int pop = stack.pop();
System.out.println("Popped element = " + pop + ", after popping" + stack);
/* Get the length of the stack */
int size = stack.size();
System.out.println("Length of the stack size = " + size);
/* Determine if it's empty */
boolean isEmpty = stack.isEmpty();
System.out.println("Is the stack empty = " + isEmpty);
}
}

View file

@ -0,0 +1,136 @@
/**
* File: array_binary_tree.java
* Created Time: 2023-07-19
* Author: krahets (krahets@163.com)
*/
package chapter_tree;
import utils.*;
import java.util.*;
/* Array-based binary tree class */
class ArrayBinaryTree {
private List<Integer> tree;
/* Constructor */
public ArrayBinaryTree(List<Integer> arr) {
tree = new ArrayList<>(arr);
}
/* List capacity */
public int size() {
return tree.size();
}
/* Get the value of the node at index i */
public Integer val(int i) {
// If the index is out of bounds, return null, representing an empty spot
if (i < 0 || i >= size())
return null;
return tree.get(i);
}
/* Get the index of the left child of the node at index i */
public Integer left(int i) {
return 2 * i + 1;
}
/* Get the index of the right child of the node at index i */
public Integer right(int i) {
return 2 * i + 2;
}
/* Get the index of the parent of the node at index i */
public Integer parent(int i) {
return (i - 1) / 2;
}
/* Level-order traversal */
public List<Integer> levelOrder() {
List<Integer> res = new ArrayList<>();
// Traverse array
for (int i = 0; i < size(); i++) {
if (val(i) != null)
res.add(val(i));
}
return res;
}
/* Depth-first traversal */
private void dfs(Integer i, String order, List<Integer> res) {
// If it is an empty spot, return
if (val(i) == null)
return;
// Pre-order traversal
if ("pre".equals(order))
res.add(val(i));
dfs(left(i), order, res);
// In-order traversal
if ("in".equals(order))
res.add(val(i));
dfs(right(i), order, res);
// Post-order traversal
if ("post".equals(order))
res.add(val(i));
}
/* Pre-order traversal */
public List<Integer> preOrder() {
List<Integer> res = new ArrayList<>();
dfs(0, "pre", res);
return res;
}
/* In-order traversal */
public List<Integer> inOrder() {
List<Integer> res = new ArrayList<>();
dfs(0, "in", res);
return res;
}
/* Post-order traversal */
public List<Integer> postOrder() {
List<Integer> res = new ArrayList<>();
dfs(0, "post", res);
return res;
}
}
public class array_binary_tree {
public static void main(String[] args) {
// Initialize binary tree
// Use a specific function to convert an array into a binary tree
List<Integer> arr = Arrays.asList(1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15);
TreeNode root = TreeNode.listToTree(arr);
System.out.println("\nInitialize binary tree\n");
System.out.println("Array representation of the binary tree:");
System.out.println(arr);
System.out.println("Linked list representation of the binary tree:");
PrintUtil.printTree(root);
// Array-based binary tree class
ArrayBinaryTree abt = new ArrayBinaryTree(arr);
// Access node
int i = 1;
Integer l = abt.left(i);
Integer r = abt.right(i);
Integer p = abt.parent(i);
System.out.println("\nThe current node's index is " + i + ", value = " + abt.val(i));
System.out.println("Its left child's index is " + l + ", value = " + (l == null ? "null" : abt.val(l)));
System.out.println("Its right child's index is " + r + ", value = " + (r == null ? "null" : abt.val(r)));
System.out.println("Its parent's index is " + p + ", value = " + (p == null ? "null" : abt.val(p)));
// Traverse tree
List<Integer> res = abt.levelOrder();
System.out.println("\nLevel-order traversal is:" + res);
res = abt.preOrder();
System.out.println("Pre-order traversal is:" + res);
res = abt.inOrder();
System.out.println("In-order traversal is:" + res);
res = abt.postOrder();
System.out.println("Post-order traversal is:" + res);
}
}

View file

@ -0,0 +1,220 @@
/**
* File: avl_tree.java
* Created Time: 2022-12-10
* Author: krahets (krahets@163.com)
*/
package chapter_tree;
import utils.*;
/* AVL tree */
class AVLTree {
TreeNode root; // Root node
/* Get node height */
public int height(TreeNode node) {
// Empty node height is -1, leaf node height is 0
return node == null ? -1 : node.height;
}
/* Update node height */
private void updateHeight(TreeNode node) {
// Node height equals the height of the tallest subtree + 1
node.height = Math.max(height(node.left), height(node.right)) + 1;
}
/* Get balance factor */
public int balanceFactor(TreeNode node) {
// Empty node balance factor is 0
if (node == null)
return 0;
// Node balance factor = left subtree height - right subtree height
return height(node.left) - height(node.right);
}
/* Right rotation operation */
private TreeNode rightRotate(TreeNode node) {
TreeNode child = node.left;
TreeNode grandChild = child.right;
// Rotate node to the right around child
child.right = node;
node.left = grandChild;
// Update node height
updateHeight(node);
updateHeight(child);
// Return the root of the subtree after rotation
return child;
}
/* Left rotation operation */
private TreeNode leftRotate(TreeNode node) {
TreeNode child = node.right;
TreeNode grandChild = child.left;
// Rotate node to the left around child
child.left = node;
node.right = grandChild;
// Update node height
updateHeight(node);
updateHeight(child);
// Return the root of the subtree after rotation
return child;
}
/* Perform rotation operation to restore balance to the subtree */
private TreeNode rotate(TreeNode node) {
// Get the balance factor of node
int balanceFactor = balanceFactor(node);
// Left-leaning tree
if (balanceFactor > 1) {
if (balanceFactor(node.left) >= 0) {
// Right rotation
return rightRotate(node);
} else {
// First left rotation then right rotation
node.left = leftRotate(node.left);
return rightRotate(node);
}
}
// Right-leaning tree
if (balanceFactor < -1) {
if (balanceFactor(node.right) <= 0) {
// Left rotation
return leftRotate(node);
} else {
// First right rotation then left rotation
node.right = rightRotate(node.right);
return leftRotate(node);
}
}
// Balanced tree, no rotation needed, return
return node;
}
/* Insert node */
public void insert(int val) {
root = insertHelper(root, val);
}
/* Recursively insert node (helper method) */
private TreeNode insertHelper(TreeNode node, int val) {
if (node == null)
return new TreeNode(val);
/* 1. Find insertion position and insert node */
if (val < node.val)
node.left = insertHelper(node.left, val);
else if (val > node.val)
node.right = insertHelper(node.right, val);
else
return node; // Do not insert duplicate nodes, return
updateHeight(node); // Update node height
/* 2. Perform rotation operation to restore balance to the subtree */
node = rotate(node);
// Return the root node of the subtree
return node;
}
/* Remove node */
public void remove(int val) {
root = removeHelper(root, val);
}
/* Recursively remove node (helper method) */
private TreeNode removeHelper(TreeNode node, int val) {
if (node == null)
return null;
/* 1. Find and remove the node */
if (val < node.val)
node.left = removeHelper(node.left, val);
else if (val > node.val)
node.right = removeHelper(node.right, val);
else {
if (node.left == null || node.right == null) {
TreeNode child = node.left != null ? node.left : node.right;
// Number of child nodes = 0, remove node and return
if (child == null)
return null;
// Number of child nodes = 1, remove node
else
node = child;
} else {
// Number of child nodes = 2, remove the next node in in-order traversal and replace the current node with it
TreeNode temp = node.right;
while (temp.left != null) {
temp = temp.left;
}
node.right = removeHelper(node.right, temp.val);
node.val = temp.val;
}
}
updateHeight(node); // Update node height
/* 2. Perform rotation operation to restore balance to the subtree */
node = rotate(node);
// Return the root node of the subtree
return node;
}
/* Search node */
public TreeNode search(int val) {
TreeNode cur = root;
// Loop find, break after passing leaf nodes
while (cur != null) {
// Target node is in cur's right subtree
if (cur.val < val)
cur = cur.right;
// Target node is in cur's left subtree
else if (cur.val > val)
cur = cur.left;
// Found target node, break loop
else
break;
}
// Return target node
return cur;
}
}
public class avl_tree {
static void testInsert(AVLTree tree, int val) {
tree.insert(val);
System.out.println("\nAfter inserting node " + val + ", the AVL tree is ");
PrintUtil.printTree(tree.root);
}
static void testRemove(AVLTree tree, int val) {
tree.remove(val);
System.out.println("\nAfter removing node " + val + ", the AVL tree is ");
PrintUtil.printTree(tree.root);
}
public static void main(String[] args) {
/* Initialize empty AVL tree */
AVLTree avlTree = new AVLTree();
/* Insert node */
// Notice how the AVL tree maintains balance after inserting nodes
testInsert(avlTree, 1);
testInsert(avlTree, 2);
testInsert(avlTree, 3);
testInsert(avlTree, 4);
testInsert(avlTree, 5);
testInsert(avlTree, 8);
testInsert(avlTree, 7);
testInsert(avlTree, 9);
testInsert(avlTree, 10);
testInsert(avlTree, 6);
/* Insert duplicate node */
testInsert(avlTree, 7);
/* Remove node */
// Notice how the AVL tree maintains balance after removing nodes
testRemove(avlTree, 8); // Remove node with degree 0
testRemove(avlTree, 5); // Remove node with degree 1
testRemove(avlTree, 4); // Remove node with degree 2
/* Search node */
TreeNode node = avlTree.search(7);
System.out.println("\nThe found node object is " + node + ", node value = " + node.val);
}
}

View file

@ -0,0 +1,158 @@
/**
* File: binary_search_tree.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_tree;
import utils.*;
/* Binary search tree */
class BinarySearchTree {
private TreeNode root;
/* Constructor */
public BinarySearchTree() {
// Initialize empty tree
root = null;
}
/* Get binary tree root node */
public TreeNode getRoot() {
return root;
}
/* Search node */
public TreeNode search(int num) {
TreeNode cur = root;
// Loop find, break after passing leaf nodes
while (cur != null) {
// Target node is in cur's right subtree
if (cur.val < num)
cur = cur.right;
// Target node is in cur's left subtree
else if (cur.val > num)
cur = cur.left;
// Found target node, break loop
else
break;
}
// Return target node
return cur;
}
/* Insert node */
public void insert(int num) {
// If tree is empty, initialize root node
if (root == null) {
root = new TreeNode(num);
return;
}
TreeNode cur = root, pre = null;
// Loop find, break after passing leaf nodes
while (cur != null) {
// Found duplicate node, thus return
if (cur.val == num)
return;
pre = cur;
// Insertion position is in cur's right subtree
if (cur.val < num)
cur = cur.right;
// Insertion position is in cur's left subtree
else
cur = cur.left;
}
// Insert node
TreeNode node = new TreeNode(num);
if (pre.val < num)
pre.right = node;
else
pre.left = node;
}
/* Remove node */
public void remove(int num) {
// If tree is empty, return
if (root == null)
return;
TreeNode cur = root, pre = null;
// Loop find, break after passing leaf nodes
while (cur != null) {
// Found node to be removed, break loop
if (cur.val == num)
break;
pre = cur;
// Node to be removed is in cur's right subtree
if (cur.val < num)
cur = cur.right;
// Node to be removed is in cur's left subtree
else
cur = cur.left;
}
// If no node to be removed, return
if (cur == null)
return;
// Number of child nodes = 0 or 1
if (cur.left == null || cur.right == null) {
// When the number of child nodes = 0/1, child = null/that child node
TreeNode child = cur.left != null ? cur.left : cur.right;
// Remove node cur
if (cur != root) {
if (pre.left == cur)
pre.left = child;
else
pre.right = child;
} else {
// If the removed node is the root, reassign the root
root = child;
}
}
// Number of child nodes = 2
else {
// Get the next node in in-order traversal of cur
TreeNode tmp = cur.right;
while (tmp.left != null) {
tmp = tmp.left;
}
// Recursively remove node tmp
remove(tmp.val);
// Replace cur with tmp
cur.val = tmp.val;
}
}
}
public class binary_search_tree {
public static void main(String[] args) {
/* Initialize binary search tree */
BinarySearchTree bst = new BinarySearchTree();
// Note that different insertion orders can result in various tree structures. This particular sequence creates a perfect binary tree
int[] nums = { 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 };
for (int num : nums) {
bst.insert(num);
}
System.out.println("\nInitialized binary tree is\n");
PrintUtil.printTree(bst.getRoot());
/* Search node */
TreeNode node = bst.search(7);
System.out.println("\nThe found node object is " + node + ", node value = " + node.val);
/* Insert node */
bst.insert(16);
System.out.println("\nAfter inserting node 16, the binary tree is\n");
PrintUtil.printTree(bst.getRoot());
/* Remove node */
bst.remove(1);
System.out.println("\nAfter removing node 1, the binary tree is\n");
PrintUtil.printTree(bst.getRoot());
bst.remove(2);
System.out.println("\nAfter removing node 2, the binary tree is\n");
PrintUtil.printTree(bst.getRoot());
bst.remove(4);
System.out.println("\nAfter removing node 4, the binary tree is\n");
PrintUtil.printTree(bst.getRoot());
}
}

View file

@ -0,0 +1,40 @@
/**
* File: binary_tree.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_tree;
import utils.*;
public class binary_tree {
public static void main(String[] args) {
/* Initialize binary tree */
// Initialize node
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(4);
TreeNode n5 = new TreeNode(5);
// Construct node references (pointers)
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
System.out.println("\nInitialize binary tree\n");
PrintUtil.printTree(n1);
/* Insert and remove nodes */
TreeNode P = new TreeNode(0);
// Insert node P between n1 -> n2
n1.left = P;
P.left = n2;
System.out.println("\nAfter inserting node P\n");
PrintUtil.printTree(n1);
// Remove node P
n1.left = n2;
System.out.println("\nAfter removing node P\n");
PrintUtil.printTree(n1);
}
}

View file

@ -0,0 +1,42 @@
/**
* File: binary_tree_bfs.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_tree;
import utils.*;
import java.util.*;
public class binary_tree_bfs {
/* Level-order traversal */
static List<Integer> levelOrder(TreeNode root) {
// Initialize queue, add root node
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
// Initialize a list to store the traversal sequence
List<Integer> list = new ArrayList<>();
while (!queue.isEmpty()) {
TreeNode node = queue.poll(); // Queue dequeues
list.add(node.val); // Save node value
if (node.left != null)
queue.offer(node.left); // Left child node enqueues
if (node.right != null)
queue.offer(node.right); // Right child node enqueues
}
return list;
}
public static void main(String[] args) {
/* Initialize binary tree */
// Use a specific function to convert an array into a binary tree
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
System.out.println("\nInitialize binary tree\n");
PrintUtil.printTree(root);
/* Level-order traversal */
List<Integer> list = levelOrder(root);
System.out.println("\nPrint sequence of nodes from level-order traversal = " + list);
}
}

View file

@ -0,0 +1,68 @@
/**
* File: binary_tree_dfs.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_tree;
import utils.*;
import java.util.*;
public class binary_tree_dfs {
// Initialize the list for storing traversal sequences
static ArrayList<Integer> list = new ArrayList<>();
/* Pre-order traversal */
static void preOrder(TreeNode root) {
if (root == null)
return;
// Visit priority: root node -> left subtree -> right subtree
list.add(root.val);
preOrder(root.left);
preOrder(root.right);
}
/* In-order traversal */
static void inOrder(TreeNode root) {
if (root == null)
return;
// Visit priority: left subtree -> root node -> right subtree
inOrder(root.left);
list.add(root.val);
inOrder(root.right);
}
/* Post-order traversal */
static void postOrder(TreeNode root) {
if (root == null)
return;
// Visit priority: left subtree -> right subtree -> root node
postOrder(root.left);
postOrder(root.right);
list.add(root.val);
}
public static void main(String[] args) {
/* Initialize binary tree */
// Use a specific function to convert an array into a binary tree
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
System.out.println("\nInitialize binary tree\n");
PrintUtil.printTree(root);
/* Pre-order traversal */
list.clear();
preOrder(root);
System.out.println("\nPrint sequence of nodes from pre-order traversal = " + list);
/* In-order traversal */
list.clear();
inOrder(root);
System.out.println("\nPrint sequence of nodes from in-order traversal = " + list);
/* Post-order traversal */
list.clear();
postOrder(root);
System.out.println("\nPrint sequence of nodes from post-order traversal = " + list);
}
}

View file

@ -0,0 +1,28 @@
/**
* File: ListNode.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package utils;
/* Linked list node */
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) {
val = x;
}
/* Deserialize a list into a linked list */
public static ListNode arrToLinkedList(int[] arr) {
ListNode dum = new ListNode(0);
ListNode head = dum;
for (int val : arr) {
head.next = new ListNode(val);
head = head.next;
}
return dum.next;
}
}

View file

@ -0,0 +1,116 @@
/**
* File: PrintUtil.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package utils;
import java.util.*;
class Trunk {
Trunk prev;
String str;
Trunk(Trunk prev, String str) {
this.prev = prev;
this.str = str;
}
};
public class PrintUtil {
/* Print matrix (Array) */
public static <T> void printMatrix(T[][] matrix) {
System.out.println("[");
for (T[] row : matrix) {
System.out.println(" " + row + ",");
}
System.out.println("]");
}
/* Print matrix (List) */
public static <T> void printMatrix(List<List<T>> matrix) {
System.out.println("[");
for (List<T> row : matrix) {
System.out.println(" " + row + ",");
}
System.out.println("]");
}
/* Print linked list */
public static void printLinkedList(ListNode head) {
List<String> list = new ArrayList<>();
while (head != null) {
list.add(String.valueOf(head.val));
head = head.next;
}
System.out.println(String.join(" -> ", list));
}
/* Print binary tree */
public static void printTree(TreeNode root) {
printTree(root, null, false);
}
/**
* Print binary tree
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
*/
public static void printTree(TreeNode root, Trunk prev, boolean isRight) {
if (root == null) {
return;
}
String prev_str = " ";
Trunk trunk = new Trunk(prev, prev_str);
printTree(root.right, trunk, true);
if (prev == null) {
trunk.str = "———";
} else if (isRight) {
trunk.str = "/———";
prev_str = " |";
} else {
trunk.str = "\\———";
prev.str = prev_str;
}
showTrunks(trunk);
System.out.println(" " + root.val);
if (prev != null) {
prev.str = prev_str;
}
trunk.str = " |";
printTree(root.left, trunk, false);
}
public static void showTrunks(Trunk p) {
if (p == null) {
return;
}
showTrunks(p.prev);
System.out.print(p.str);
}
/* Print hash table */
public static <K, V> void printHashMap(Map<K, V> map) {
for (Map.Entry<K, V> kv : map.entrySet()) {
System.out.println(kv.getKey() + " -> " + kv.getValue());
}
}
/* Print heap (Priority queue) */
public static void printHeap(Queue<Integer> queue) {
List<Integer> list = new ArrayList<>(queue);
System.out.print("Array representation of the heap:");
System.out.println(list);
System.out.println("Tree representation of the heap:");
TreeNode root = TreeNode.listToTree(list);
printTree(root);
}
}

View file

@ -0,0 +1,73 @@
/**
* File: TreeNode.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package utils;
import java.util.*;
/* Binary tree node class */
public class TreeNode {
public int val; // Node value
public int height; // Node height
public TreeNode left; // Reference to the left child node
public TreeNode right; // Reference to the right child node
/* Constructor */
public TreeNode(int x) {
val = x;
}
// For serialization encoding rules, refer to:
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
// Array representation of the binary tree:
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
// Linked list representation of the binary tree:
// / 15
// / 7
// / 3
// | \ 6
// | \ 12
// 1
// \ 2
// | / 9
// \ 4
// \ 8
/* Deserialize a list into a binary tree: Recursively */
private static TreeNode listToTreeDFS(List<Integer> arr, int i) {
if (i < 0 || i >= arr.size() || arr.get(i) == null) {
return null;
}
TreeNode root = new TreeNode(arr.get(i));
root.left = listToTreeDFS(arr, 2 * i + 1);
root.right = listToTreeDFS(arr, 2 * i + 2);
return root;
}
/* Deserialize a list into a binary tree */
public static TreeNode listToTree(List<Integer> arr) {
return listToTreeDFS(arr, 0);
}
/* Serialize a binary tree into a list: Recursively */
private static void treeToListDFS(TreeNode root, int i, List<Integer> res) {
if (root == null)
return;
while (i >= res.size()) {
res.add(null);
}
res.set(i, root.val);
treeToListDFS(root.left, 2 * i + 1, res);
treeToListDFS(root.right, 2 * i + 2, res);
}
/* Serialize a binary tree into a list */
public static List<Integer> treeToList(TreeNode root) {
List<Integer> res = new ArrayList<>();
treeToListDFS(root, 0, res);
return res;
}
}

View file

@ -0,0 +1,36 @@
/**
* File: Vertex.java
* Created Time: 2023-02-15
* Author: krahets (krahets@163.com)
*/
package utils;
import java.util.*;
/* Vertex class */
public class Vertex {
public int val;
public Vertex(int val) {
this.val = val;
}
/* Input a list of values vals, return a list of vertices vets */
public static Vertex[] valsToVets(int[] vals) {
Vertex[] vets = new Vertex[vals.length];
for (int i = 0; i < vals.length; i++) {
vets[i] = new Vertex(vals[i]);
}
return vets;
}
/* Input a list of vertices vets, return a list of values vals */
public static List<Integer> vetsToVals(List<Vertex> vets) {
List<Integer> vals = new ArrayList<>();
for (Vertex vet : vets) {
vals.add(vet.val);
}
return vals;
}
}

1
en/codes/python/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
__pycache__

View file

@ -0,0 +1,100 @@
"""
File: array.py
Created Time: 2022-11-25
Author: krahets (krahets@163.com)
"""
import random
def random_access(nums: list[int]) -> int:
"""Random access to elements"""
# Randomly select a number from the interval [0, len(nums)-1]
random_index = random.randint(0, len(nums) - 1)
# Retrieve and return a random element
random_num = nums[random_index]
return random_num
# Note that Python's list is a dynamic array that can be extended
# For ease of learning, this function treats the list as a static array
def extend(nums: list[int], enlarge: int) -> list[int]:
"""Extend array length"""
# Initialize an extended length array
res = [0] * (len(nums) + enlarge)
# Copy all elements from the original array to the new array
for i in range(len(nums)):
res[i] = nums[i]
# Return the new array after expansion
return res
def insert(nums: list[int], num: int, index: int):
"""Insert element num at `index`"""
# Move all elements after `index` one position backward
for i in range(len(nums) - 1, index, -1):
nums[i] = nums[i - 1]
# Assign num to the element at index
nums[index] = num
def remove(nums: list[int], index: int):
"""Remove the element at `index`"""
# Move all elements after `index` one position forward
for i in range(index, len(nums) - 1):
nums[i] = nums[i + 1]
def traverse(nums: list[int]):
"""Traverse array"""
count = 0
# Traverse array by index
for i in range(len(nums)):
count += nums[i]
# Traverse array elements
for num in nums:
count += num
# Traverse both data index and elements
for i, num in enumerate(nums):
count += nums[i]
count += num
def find(nums: list[int], target: int) -> int:
"""Search for a specified element in the array"""
for i in range(len(nums)):
if nums[i] == target:
return i
return -1
"""Driver Code"""
if __name__ == "__main__":
# Initialize an array
arr = [0] * 5
print("Array arr =", arr)
nums = [1, 3, 2, 5, 4]
print("Array nums =", nums)
# Random access
random_num: int = random_access(nums)
print("Retrieve a random element in nums", random_num)
# Length extension
nums: list[int] = extend(nums, 3)
print("Extend the array length to 8, resulting in nums =", nums)
# Insert element
insert(nums, 6, 3)
print("Insert number 6 at index 3, resulting in nums =", nums)
# Remove element
remove(nums, 2)
print("Remove the element at index 2, resulting in nums =", nums)
# Traverse array
traverse(nums)
# Search for elements
index: int = find(nums, 3)
print("Search for element 3 in nums, resulting in index =", index)

View file

@ -0,0 +1,85 @@
"""
File: linked_list.py
Created Time: 2022-11-25
Author: krahets (krahets@163.com)
"""
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
from modules import ListNode, print_linked_list
def insert(n0: ListNode, P: ListNode):
"""Insert node P after node n0 in the linked list"""
n1 = n0.next
P.next = n1
n0.next = P
def remove(n0: ListNode):
"""Remove the first node after node n0 in the linked list"""
if not n0.next:
return
# n0 -> P -> n1
P = n0.next
n1 = P.next
n0.next = n1
def access(head: ListNode, index: int) -> ListNode | None:
"""Access the node at `index` in the linked list"""
for _ in range(index):
if not head:
return None
head = head.next
return head
def find(head: ListNode, target: int) -> int:
"""Search for the first node with value target in the linked list"""
index = 0
while head:
if head.val == target:
return index
head = head.next
index += 1
return -1
"""Driver Code"""
if __name__ == "__main__":
# Initialize linked list
# Initialize each node
n0 = ListNode(1)
n1 = ListNode(3)
n2 = ListNode(2)
n3 = ListNode(5)
n4 = ListNode(4)
# Build references between nodes
n0.next = n1
n1.next = n2
n2.next = n3
n3.next = n4
print("The initialized linked list is")
print_linked_list(n0)
# Insert node
p = ListNode(0)
insert(n0, p)
print("Linked list after inserting the node is")
print_linked_list(n0)
# Remove node
remove(n0)
print("Linked list after removing the node is")
print_linked_list(n0)
# Access node
node: ListNode = access(n0, 3)
print("The value of the node at index 3 in the linked list = {}".format(node.val))
# Search node
index: int = find(n0, 2)
print("The index of the node with value 2 in the linked list = {}".format(index))

View file

@ -0,0 +1,56 @@
"""
File: list.py
Created Time: 2022-11-25
Author: krahets (krahets@163.com)
"""
"""Driver Code"""
if __name__ == "__main__":
# Initialize list
nums: list[int] = [1, 3, 2, 5, 4]
print("\nList nums =", nums)
# Access element
x: int = nums[1]
print("\nAccess the element at index 1, resulting in x =", x)
# Update element
nums[1] = 0
print("\nUpdate the element at index 1 to 0, resulting in nums =", nums)
# Clear list
nums.clear()
print("\nAfter clearing the list, nums =", nums)
# Add element at the end
nums.append(1)
nums.append(3)
nums.append(2)
nums.append(5)
nums.append(4)
print("\nAfter adding the element, nums =", nums)
# Insert element in the middle
nums.insert(3, 6)
print("\nInsert number 6 at index 3, resulting in nums =", nums)
# Remove element
nums.pop(3)
print("\nRemove the element at index 3, resulting in nums =", nums)
# Traverse the list by index
count = 0
for i in range(len(nums)):
count += nums[i]
# Traverse the list elements
for num in nums:
count += num
# Concatenate two lists
nums1 = [6, 8, 7, 10, 9]
nums += nums1
print("\nConcatenate list nums1 to nums, resulting in nums =", nums)
# Sort list
nums.sort()
print("\nAfter sorting the list, nums =", nums)

View file

@ -0,0 +1,118 @@
"""
File: my_list.py
Created Time: 2022-11-25
Author: krahets (krahets@163.com)
"""
class MyList:
"""List class"""
def __init__(self):
"""Constructor"""
self._capacity: int = 10 # List capacity
self._arr: list[int] = [0] * self._capacity # Array (stores list elements)
self._size: int = 0 # List length (current number of elements)
self._extend_ratio: int = 2 # Multiple for each list expansion
def size(self) -> int:
"""Get list length (current number of elements)"""
return self._size
def capacity(self) -> int:
"""Get list capacity"""
return self._capacity
def get(self, index: int) -> int:
"""Access element"""
# If the index is out of bounds, throw an exception, as below
if index < 0 or index >= self._size:
raise IndexError("Index out of bounds")
return self._arr[index]
def set(self, num: int, index: int):
"""Update element"""
if index < 0 or index >= self._size:
raise IndexError("Index out of bounds")
self._arr[index] = num
def add(self, num: int):
"""Add element at the end"""
# When the number of elements exceeds capacity, trigger the expansion mechanism
if self.size() == self.capacity():
self.extend_capacity()
self._arr[self._size] = num
self._size += 1
def insert(self, num: int, index: int):
"""Insert element in the middle"""
if index < 0 or index >= self._size:
raise IndexError("Index out of bounds")
# When the number of elements exceeds capacity, trigger the expansion mechanism
if self._size == self.capacity():
self.extend_capacity()
# Move all elements after `index` one position backward
for j in range(self._size - 1, index - 1, -1):
self._arr[j + 1] = self._arr[j]
self._arr[index] = num
# Update the number of elements
self._size += 1
def remove(self, index: int) -> int:
"""Remove element"""
if index < 0 or index >= self._size:
raise IndexError("Index out of bounds")
num = self._arr[index]
# Move all elements after `index` one position forward
for j in range(index, self._size - 1):
self._arr[j] = self._arr[j + 1]
# Update the number of elements
self._size -= 1
# Return the removed element
return num
def extend_capacity(self):
"""Extend list"""
# Create a new array of _extend_ratio times the length of the original array and copy the original array to the new array
self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)
# Update list capacity
self._capacity = len(self._arr)
def to_array(self) -> list[int]:
"""Return a list of valid lengths"""
return self._arr[: self._size]
"""Driver Code"""
if __name__ == "__main__":
# Initialize list
nums = MyList()
# Add element at the end
nums.add(1)
nums.add(3)
nums.add(2)
nums.add(5)
nums.add(4)
print(f"List nums = {nums.to_array()} capacity = {nums.capacity()} length = {nums.size()}")
# Insert element in the middle
nums.insert(6, index=3)
print("Insert number 6 at index 3, resulting in nums =", nums.to_array())
# Remove element
nums.remove(3)
print("Remove the element at index 3, resulting in nums =", nums.to_array())
# Access element
num = nums.get(1)
print("Access the element at index 1, resulting in num =", num)
# Update element
nums.set(0, 1)
print("Update the element at index 1 to 0, resulting in nums =", nums.to_array())
# Test expansion mechanism
for i in range(10):
# At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism at this time
nums.add(i)
print(f"After expansion, the list {nums.to_array()} capacity = {nums.capacity()} length = {nums.size()}")

View file

@ -0,0 +1,62 @@
"""
File: n_queens.py
Created Time: 2023-04-26
Author: krahets (krahets@163.com)
"""
def backtrack(
row: int,
n: int,
state: list[list[str]],
res: list[list[list[str]]],
cols: list[bool],
diags1: list[bool],
diags2: list[bool],
):
"""Backtracking algorithm: n queens"""
# When all rows are placed, record the solution
if row == n:
res.append([list(row) for row in state])
return
# Traverse all columns
for col in range(n):
# Calculate the main and minor diagonals corresponding to the cell
diag1 = row - col + n - 1
diag2 = row + col
# Pruning: do not allow queens on the column, main diagonal, or minor diagonal of the cell
if not cols[col] and not diags1[diag1] and not diags2[diag2]:
# Attempt: place the queen in the cell
state[row][col] = "Q"
cols[col] = diags1[diag1] = diags2[diag2] = True
# Place the next row
backtrack(row + 1, n, state, res, cols, diags1, diags2)
# Retract: restore the cell to an empty spot
state[row][col] = "#"
cols[col] = diags1[diag1] = diags2[diag2] = False
def n_queens(n: int) -> list[list[list[str]]]:
"""Solve n queens"""
# Initialize an n*n size chessboard, where 'Q' represents the queen and '#' represents an empty spot
state = [["#" for _ in range(n)] for _ in range(n)]
cols = [False] * n # Record columns with queens
diags1 = [False] * (2 * n - 1) # Record main diagonals with queens
diags2 = [False] * (2 * n - 1) # Record minor diagonals with queens
res = []
backtrack(0, n, state, res, cols, diags1, diags2)
return res
"""Driver Code"""
if __name__ == "__main__":
n = 4
res = n_queens(n)
print(f"Input chessboard dimensions as {n}")
print(f"The total number of queen placement solutions is {len(res)}")
for state in res:
print("--------------------")
for row in state:
print(row)

View file

@ -0,0 +1,44 @@
"""
File: permutations_i.py
Created Time: 2023-04-15
Author: krahets (krahets@163.com)
"""
def backtrack(
state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]
):
"""Backtracking algorithm: Permutation I"""
# When the state length equals the number of elements, record the solution
if len(state) == len(choices):
res.append(list(state))
return
# Traverse all choices
for i, choice in enumerate(choices):
# Pruning: do not allow repeated selection of elements
if not selected[i]:
# Attempt: make a choice, update the state
selected[i] = True
state.append(choice)
# Proceed to the next round of selection
backtrack(state, choices, selected, res)
# Retract: undo the choice, restore to the previous state
selected[i] = False
state.pop()
def permutations_i(nums: list[int]) -> list[list[int]]:
"""Permutation I"""
res = []
backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)
return res
"""Driver Code"""
if __name__ == "__main__":
nums = [1, 2, 3]
res = permutations_i(nums)
print(f"Input array nums = {nums}")
print(f"All permutations res = {res}")

View file

@ -0,0 +1,46 @@
"""
File: permutations_ii.py
Created Time: 2023-04-15
Author: krahets (krahets@163.com)
"""
def backtrack(
state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]
):
"""Backtracking algorithm: Permutation II"""
# When the state length equals the number of elements, record the solution
if len(state) == len(choices):
res.append(list(state))
return
# Traverse all choices
duplicated = set[int]()
for i, choice in enumerate(choices):
# Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements
if not selected[i] and choice not in duplicated:
# Attempt: make a choice, update the state
duplicated.add(choice) # Record selected element values
selected[i] = True
state.append(choice)
# Proceed to the next round of selection
backtrack(state, choices, selected, res)
# Retract: undo the choice, restore to the previous state
selected[i] = False
state.pop()
def permutations_ii(nums: list[int]) -> list[list[int]]:
"""Permutation II"""
res = []
backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)
return res
"""Driver Code"""
if __name__ == "__main__":
nums = [1, 2, 2]
res = permutations_ii(nums)
print(f"Input array nums = {nums}")
print(f"All permutations res = {res}")

View file

@ -0,0 +1,36 @@
"""
File: preorder_traversal_i_compact.py
Created Time: 2023-04-15
Author: krahets (krahets@163.com)
"""
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
from modules import TreeNode, print_tree, list_to_tree
def pre_order(root: TreeNode):
"""Pre-order traversal: Example one"""
if root is None:
return
if root.val == 7:
# Record solution
res.append(root)
pre_order(root.left)
pre_order(root.right)
"""Driver Code"""
if __name__ == "__main__":
root = list_to_tree([1, 7, 3, 4, 5, 6, 7])
print("\nInitialize binary tree")
print_tree(root)
# Pre-order traversal
res = list[TreeNode]()
pre_order(root)
print("\nOutput all nodes with value 7")
print([node.val for node in res])

View file

@ -0,0 +1,42 @@
"""
File: preorder_traversal_ii_compact.py
Created Time: 2023-04-15
Author: krahets (krahets@163.com)
"""
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
from modules import TreeNode, print_tree, list_to_tree
def pre_order(root: TreeNode):
"""Pre-order traversal: Example two"""
if root is None:
return
# Attempt
path.append(root)
if root.val == 7:
# Record solution
res.append(list(path))
pre_order(root.left)
pre_order(root.right)
# Retract
path.pop()
"""Driver Code"""
if __name__ == "__main__":
root = list_to_tree([1, 7, 3, 4, 5, 6, 7])
print("\nInitialize binary tree")
print_tree(root)
# Pre-order traversal
path = list[TreeNode]()
res = list[list[TreeNode]]()
pre_order(root)
print("\nOutput all root-to-node 7 paths")
for path in res:
print([node.val for node in path])

View file

@ -0,0 +1,43 @@
"""
File: preorder_traversal_iii_compact.py
Created Time: 2023-04-15
Author: krahets (krahets@163.com)
"""
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
from modules import TreeNode, print_tree, list_to_tree
def pre_order(root: TreeNode):
"""Pre-order traversal: Example three"""
# Pruning
if root is None or root.val == 3:
return
# Attempt
path.append(root)
if root.val == 7:
# Record solution
res.append(list(path))
pre_order(root.left)
pre_order(root.right)
# Retract
path.pop()
"""Driver Code"""
if __name__ == "__main__":
root = list_to_tree([1, 7, 3, 4, 5, 6, 7])
print("\nInitialize binary tree")
print_tree(root)
# Pre-order traversal
path = list[TreeNode]()
res = list[list[TreeNode]]()
pre_order(root)
print("\nOutput all root-to-node 7 paths, not including nodes with value 3")
for path in res:
print([node.val for node in path])

View file

@ -0,0 +1,71 @@
"""
File: preorder_traversal_iii_template.py
Created Time: 2023-04-15
Author: krahets (krahets@163.com)
"""
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
from modules import TreeNode, print_tree, list_to_tree
def is_solution(state: list[TreeNode]) -> bool:
"""Determine if the current state is a solution"""
return state and state[-1].val == 7
def record_solution(state: list[TreeNode], res: list[list[TreeNode]]):
"""Record solution"""
res.append(list(state))
def is_valid(state: list[TreeNode], choice: TreeNode) -> bool:
"""Determine if the choice is legal under the current state"""
return choice is not None and choice.val != 3
def make_choice(state: list[TreeNode], choice: TreeNode):
"""Update state"""
state.append(choice)
def undo_choice(state: list[TreeNode], choice: TreeNode):
"""Restore state"""
state.pop()
def backtrack(
state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]
):
"""Backtracking algorithm: Example three"""
# Check if it's a solution
if is_solution(state):
# Record solution
record_solution(state, res)
# Traverse all choices
for choice in choices:
# Pruning: check if the choice is legal
if is_valid(state, choice):
# Attempt: make a choice, update the state
make_choice(state, choice)
# Proceed to the next round of selection
backtrack(state, [choice.left, choice.right], res)
# Retract: undo the choice, restore to the previous state
undo_choice(state, choice)
"""Driver Code"""
if __name__ == "__main__":
root = list_to_tree([1, 7, 3, 4, 5, 6, 7])
print("\nInitialize binary tree")
print_tree(root)
# Backtracking algorithm
res = []
backtrack(state=[], choices=[root], res=res)
print("\nOutput all root-to-node 7 paths, requiring paths not to include nodes with value 3")
for path in res:
print([node.val for node in path])

View file

@ -0,0 +1,48 @@
"""
File: subset_sum_i.py
Created Time: 2023-06-17
Author: krahets (krahets@163.com)
"""
def backtrack(
state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]
):
"""Backtracking algorithm: Subset Sum I"""
# When the subset sum equals target, record the solution
if target == 0:
res.append(list(state))
return
# Traverse all choices
# Pruning two: start traversing from start to avoid generating duplicate subsets
for i in range(start, len(choices)):
# Pruning one: if the subset sum exceeds target, end the loop immediately
# This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target
if target - choices[i] < 0:
break
# Attempt: make a choice, update target, start
state.append(choices[i])
# Proceed to the next round of selection
backtrack(state, target - choices[i], choices, i, res)
# Retract: undo the choice, restore to the previous state
state.pop()
def subset_sum_i(nums: list[int], target: int) -> list[list[int]]:
"""Solve Subset Sum I"""
state = [] # State (subset)
nums.sort() # Sort nums
start = 0 # Start point for traversal
res = [] # Result list (subset list)
backtrack(state, target, nums, start, res)
return res
"""Driver Code"""
if __name__ == "__main__":
nums = [3, 4, 5]
target = 9
res = subset_sum_i(nums, target)
print(f"Input array nums = {nums}, target = {target}")
print(f"All subsets equal to {target} res = {res}")

View file

@ -0,0 +1,50 @@
"""
File: subset_sum_i_naive.py
Created Time: 2023-06-17
Author: krahets (krahets@163.com)
"""
def backtrack(
state: list[int],
target: int,
total: int,
choices: list[int],
res: list[list[int]],
):
"""Backtracking algorithm: Subset Sum I"""
# When the subset sum equals target, record the solution
if total == target:
res.append(list(state))
return
# Traverse all choices
for i in range(len(choices)):
# Pruning: if the subset sum exceeds target, skip that choice
if total + choices[i] > target:
continue
# Attempt: make a choice, update elements and total
state.append(choices[i])
# Proceed to the next round of selection
backtrack(state, target, total + choices[i], choices, res)
# Retract: undo the choice, restore to the previous state
state.pop()
def subset_sum_i_naive(nums: list[int], target: int) -> list[list[int]]:
"""Solve Subset Sum I (including duplicate subsets)"""
state = [] # State (subset)
total = 0 # Subset sum
res = [] # Result list (subset list)
backtrack(state, target, total, nums, res)
return res
"""Driver Code"""
if __name__ == "__main__":
nums = [3, 4, 5]
target = 9
res = subset_sum_i_naive(nums, target)
print(f"Input array nums = {nums}, target = {target}")
print(f"All subsets equal to {target} res = {res}")
print(f"Please note that the result of this method includes duplicate sets")

Some files were not shown because too many files have changed in this diff Show more