316 questions
0
votes
1
answer
65
views
Recursive Permutations JavaScript problem returning empty array
A permutations problem (LeetCode), which finds the permutations of an array [1,2,3], is returning an empty array through a backtracking recursive function. The console prints out the following:
Input
...
0
votes
0
answers
122
views
How do I solve this domino game using recursion in C#?
I tried updating my code to this version but still, it doesn t work:
using System;
namespace RecursiveDomino
{
class Program
{
static void Main()
{
int n = Convert....
1
vote
1
answer
79
views
Stack overflow when finding all paths through a grid with recursion and backtracking
I have been following a playlist of Java for data structures and algorithms and I have come across the topic of Backtracking in which a board is given to you along with starting point (0,0) and end ...
-2
votes
2
answers
181
views
Can't understand this backtracking question from book
Currently I am studying Data Structure and Algorithmic thinking with python by Narasimha Karumanchi.
But I can't understand one question which is as follows:
Q. Generate all the binary strings with n-...
-1
votes
1
answer
48
views
Java program is not showing output even though it compiles with no error
I'm new to Java and I'm trying to build a sudoku solver programm using backtracking and an int[][] matrix.
Function usage:
isSafeSudoku()
this function checks whether the digit (from 1 to 9) is safe ...
0
votes
1
answer
65
views
Differencce between the given codes for combination sum problem
Ok, I have been trying to find the mistake in my code or the place where it changes from the given code.
There are two codes here
code #1-
class Solution {
public List<List<Integer>> ...
-3
votes
1
answer
84
views
Backtracking task got TRAGIC
I was solving this task:
Let us consider the problem of calculating the number of paths in an n × n grid from the upper-left corner to the lower-right corner such that the path visits each square ...
0
votes
2
answers
110
views
Can anyone figure out the error in this DFS solution to a problem
Problem :
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.
Each number in ...
1
vote
1
answer
252
views
Leetcode 1601. Maximum Number of Achievable Transfer Requests
I am trying to solve LeetCode problem 1601. Maximum Number of Achievable Transfer Requests
:
We have n buildings numbered from 0 to n - 1. Each building has a number of employees. It's transfer ...
0
votes
1
answer
630
views
Understanding the for loop in Python permutation algorithm (Back-Tracking)
I'm currently working on a permutation algorithm in Python, and I'm having trouble understanding how the for loop inside the backtrack function works. I would appreciate it if someone could provide a ...
1
vote
1
answer
160
views
Recursive approach with and without manual backtracking
In some cases, like the "hasPathSum" problem, where we are checking for a specific condition (e.g., targetSum == 0) at leaf nodes, we don't need manual backtracking. The recursion naturally ...
0
votes
1
answer
63
views
Can someone please explain me the backtracking of this recursion, I don't know why but it is adding current vector to ans vector 2 times
#include <iostream>
#include <vector>
using namespace std;
void getAns(char s, vector<char> current, vector<vector<char>> &ans, int n, int i){
if (i >= n) {
...
1
vote
2
answers
2k
views
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order?
I am working on LeetCode problem 46. Permutations:
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
I thought to solve this ...
0
votes
1
answer
2k
views
Understanding solution to the Optimal Account Balancing (Leetcode 465) in python
The problem can be accessed from this link
https://leetcode.com/problems/optimal-account-balancing/
*** Didn't realize this was only accessible by premium members, here is the problem statement and ...
1
vote
1
answer
118
views
How Backtracking works in programming languages?
I am using recursion with backtracking, i have used multiple recursion call
My code snippet : -
func test(currentIndex: Int, op: inout [Int]) {
if currentIndex > 1 {
print("exit ...
0
votes
0
answers
26
views
why it grid become zero after one travers
In a gold mine grid of size m x n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the ...
0
votes
0
answers
89
views
How can I implement recursive backtracking for this sudoku solver without using any for loops?
I am trying to create a sudoku solver that uses recursive backtracking, however, I am not using any for loops in my program. This makes it difficult since when using the backtracking method, I need to ...
0
votes
2
answers
232
views
Need help "Sudoku Solver Backtracking" Java
I'm having trouble writing a Backtracking algorithm (Sudoku Solver) for my Sudoku project. My idea is to create a 2D array variable "initial" as an initializer array to push into the player'...
0
votes
3
answers
45
views
Getting unknown error in SudokuSolver Backtracking problem
I was trying a Backtracking problem (SudokuSolver) I am getting an error which no-one could resolve or even understand. Hence I am seeking help here.
The problem is as follows:
And the main function/...
-2
votes
1
answer
39
views
How to return 'count' of some recursions in python?
How to update and return count variable to outside of fucntion in python.
for input : [[0,0,0],[0,1,0],[0,0,0]]
std out : 1 1 #as count is incrementing inside function but in each recursion it's ...
1
vote
1
answer
231
views
list value remains changed after returning from the backtracking function
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
ans = []
def backtrack(S, left, right):
if len(S) == 2 * n:
ans.append(''.join(S))...
1
vote
1
answer
656
views
Why do we need to copy array element inside backtracking
This is a classic backtracking problem , i dont know why we need to copy arr element inside recursive function in order for it to work correctly. If i dont copy. It will return blank array
def subsets(...
0
votes
1
answer
231
views
How to print the costs of all possible paths from a node to another?
I want to print all paths from source node to destination node, and the costs of these paths.
So far, I have the following code:
// Find all paths from source to destination.
void search(Graph* graph, ...
2
votes
0
answers
233
views
Recursive backtracking algorithm for the 'coin change problem'
I'm trying to solve the "Coin Change Problem" by implementing a recursive backtracking algorithm which will analyze all possible outcomes and show the optimal combination of coins. ...
0
votes
2
answers
147
views
How do I output the quickest path of a maze via backtracking?
I'm trying to solve a backtracking problem in python. Not only should my code be able to identify and reach the goal, but also output the quickest path to it. The maze is looks like this:
xxxxxxx
xSx ...