Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/main/java/com/thealgorithms/backtracking/NQueens.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

/**
* Problem statement: Given a N x N chess board. Return all arrangements in
Expand Down Expand Up @@ -41,6 +42,8 @@
*/
public final class NQueens {

private static final Logger LOGGER = Logger.getLogger(NQueens.class.getName());

// Store occupied rows for constant time safety check
private static final Set<Integer> OCROWS = new HashSet<>();

Expand All @@ -63,13 +66,13 @@ public static void placeQueens(final int queens) {
List<List<String>> arrangements = new ArrayList<>();
getSolution(queens, arrangements, new int[queens], 0);
if (arrangements.isEmpty()) {
System.out.println(" no way to place " + queens + " queens on board of size " + queens + "x" + queens);
LOGGER.info(" no way to place " + queens + " queens on board of size " + queens + "x" + queens);
} else {
System.out.println("Arrangement for placing " + queens + " queens");
LOGGER.info("Arrangement for placing " + queens + " queens");
}
for (List<String> arrangement : arrangements) {
arrangement.forEach(System.out::println);
System.out.println();
arrangement.forEach(row -> LOGGER.info(row));
LOGGER.info("");
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/thealgorithms/backtracking/SudokuSolver.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thealgorithms.backtracking;

import java.util.logging.Logger;

/**
* Sudoku Solver using Backtracking Algorithm
* Solves a 9x9 Sudoku puzzle by filling empty cells with valid digits (1-9)
Expand All @@ -8,6 +10,7 @@
*/
public final class SudokuSolver {

private static final Logger LOGGER = Logger.getLogger(SudokuSolver.class.getName());
private static final int GRID_SIZE = 9;
private static final int SUBGRID_SIZE = 3;
private static final int EMPTY_CELL = 0;
Expand Down Expand Up @@ -141,17 +144,19 @@ private static boolean isNumberInSubgrid(int[][] board, int row, int col, int nu
* @param board the Sudoku board
*/
public static void printBoard(int[][] board) {
StringBuilder sb = new StringBuilder();
for (int row = 0; row < GRID_SIZE; row++) {
if (row % SUBGRID_SIZE == 0 && row != 0) {
System.out.println("-----------");
sb.append("-----------\n");
}
for (int col = 0; col < GRID_SIZE; col++) {
if (col % SUBGRID_SIZE == 0 && col != 0) {
System.out.print("|");
sb.append("|");
}
System.out.print(board[row][col]);
sb.append(board[row][col]);
}
System.out.println();
sb.append("\n");
}
LOGGER.info(sb.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* A thread-safe generic cache implementation using the First-In-First-Out eviction policy.
Expand All @@ -35,6 +37,8 @@
*/
public final class FIFOCache<K, V> {

private static final Logger LOGGER = Logger.getLogger(FIFOCache.class.getName());

private final int capacity;
private final long defaultTTL;
private final Map<K, CacheEntry<V>> cache;
Expand Down Expand Up @@ -256,7 +260,7 @@ private void notifyEviction(K key, V value) {
try {
evictionListener.accept(key, value);
} catch (Exception e) {
System.err.println("Eviction listener failed: " + e.getMessage());
LOGGER.log(Level.WARNING, "Eviction listener failed", e);
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.thealgorithms.stacks;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
import java.util.Stack;
import java.util.function.BiFunction;

/**
Expand Down Expand Up @@ -35,24 +36,23 @@ private static BiFunction<Integer, Integer, Integer> getOperator(final String op
}
}

private static void performOperation(Stack<Integer> s, final String operationSymbol) {
private static void performOperation(Deque<Integer> s, final String operationSymbol) {
if (s.size() < 2) {
throw new IllegalArgumentException("exp is not a proper postfix expression (too few arguments).");
}
s.push(getOperator(operationSymbol).apply(s.pop(), s.pop()));
}

private static void consumeExpression(Stack<Integer> s, final String exp) {
Scanner tokens = new Scanner(exp);

while (tokens.hasNext()) {
if (tokens.hasNextInt()) {
s.push(tokens.nextInt());
} else {
performOperation(s, tokens.next());
private static void consumeExpression(Deque<Integer> s, final String exp) {
try (Scanner tokens = new Scanner(exp)) {
while (tokens.hasNext()) {
if (tokens.hasNextInt()) {
s.push(tokens.nextInt());
} else {
performOperation(s, tokens.next());
}
}
}
tokens.close();
}

/**
Expand All @@ -62,7 +62,7 @@ private static void consumeExpression(Stack<Integer> s, final String exp) {
* @exception IllegalArgumentException exp is not a valid postix expression.
*/
public static int postfixEvaluate(final String exp) {
Stack<Integer> s = new Stack<>();
Deque<Integer> s = new ArrayDeque<>();
consumeExpression(s, exp);
if (s.size() != 1) {
throw new IllegalArgumentException("exp is not a proper postfix expression.");
Expand Down
Loading